截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PhpFile.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (https://nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. declare(strict_types=1);
  7. namespace Nette\PhpGenerator;
  8. use Nette;
  9. /**
  10. * Instance of PHP file.
  11. *
  12. * Generates:
  13. * - opening tag (<?php)
  14. * - doc comments
  15. * - one or more namespaces
  16. */
  17. final class PhpFile
  18. {
  19. use Nette\SmartObject;
  20. use Traits\CommentAware;
  21. /** @var PhpNamespace[] */
  22. private $namespaces = [];
  23. /** @var bool */
  24. private $strictTypes = false;
  25. public function addClass(string $name): ClassType
  26. {
  27. return $this
  28. ->addNamespace(Helpers::extractNamespace($name))
  29. ->addClass(Helpers::extractShortName($name));
  30. }
  31. public function addInterface(string $name): ClassType
  32. {
  33. return $this
  34. ->addNamespace(Helpers::extractNamespace($name))
  35. ->addInterface(Helpers::extractShortName($name));
  36. }
  37. public function addTrait(string $name): ClassType
  38. {
  39. return $this
  40. ->addNamespace(Helpers::extractNamespace($name))
  41. ->addTrait(Helpers::extractShortName($name));
  42. }
  43. /** @param string|PhpNamespace $namespace */
  44. public function addNamespace($namespace): PhpNamespace
  45. {
  46. if ($namespace instanceof PhpNamespace) {
  47. $res = $this->namespaces[$namespace->getName()] = $namespace;
  48. } elseif (is_string($namespace)) {
  49. $res = $this->namespaces[$namespace] = $this->namespaces[$namespace] ?? new PhpNamespace($namespace);
  50. } else {
  51. throw new Nette\InvalidArgumentException('Argument must be string|PhpNamespace.');
  52. }
  53. foreach ($this->namespaces as $namespace) {
  54. $namespace->setBracketedSyntax(count($this->namespaces) > 1 && isset($this->namespaces['']));
  55. }
  56. return $res;
  57. }
  58. /** @return PhpNamespace[] */
  59. public function getNamespaces(): array
  60. {
  61. return $this->namespaces;
  62. }
  63. /** @return static */
  64. public function addUse(string $name, string $alias = null): self
  65. {
  66. $this->addNamespace('')->addUse($name, $alias);
  67. return $this;
  68. }
  69. /**
  70. * Adds declare(strict_types=1) to output.
  71. * @return static
  72. */
  73. public function setStrictTypes(bool $on = true): self
  74. {
  75. $this->strictTypes = $on;
  76. return $this;
  77. }
  78. public function hasStrictTypes(): bool
  79. {
  80. return $this->strictTypes;
  81. }
  82. /** @deprecated use hasStrictTypes() */
  83. public function getStrictTypes(): bool
  84. {
  85. return $this->strictTypes;
  86. }
  87. public function __toString(): string
  88. {
  89. try {
  90. return (new Printer)->printFile($this);
  91. } catch (\Throwable $e) {
  92. if (PHP_VERSION_ID >= 70400) {
  93. throw $e;
  94. }
  95. trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
  96. return '';
  97. }
  98. }
  99. }