1 | <?php |
||
25 | class Application extends Symfony |
||
26 | { |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | public const APP_NAME = 'Railt'; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | public const APP_VERSION = '1.2'; |
||
36 | |||
37 | /** |
||
38 | * @var ExceptionRenderer |
||
39 | */ |
||
40 | private $renderer; |
||
41 | |||
42 | /** |
||
43 | * @var Highlighter |
||
44 | */ |
||
45 | private $highlighter; |
||
46 | |||
47 | /** |
||
48 | * Application constructor. |
||
49 | * @throws \InvalidArgumentException |
||
50 | * @throws \Railt\Lexer\Exception\BadLexemeException |
||
51 | */ |
||
52 | public function __construct() |
||
59 | |||
60 | /** |
||
61 | * @param string ...$paths |
||
62 | * @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
||
63 | */ |
||
64 | public function tryReadManifest(string ...$paths): void |
||
65 | { |
||
66 | foreach ($paths as $path) { |
||
67 | if (\is_file($path) && \is_readable($path)) { |
||
68 | $this->readManifest($path); |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param string $path |
||
75 | * @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
||
76 | */ |
||
77 | public function readManifest(string $path): void |
||
78 | { |
||
79 | $manifest = \json_decode(\file_get_contents($path), true); |
||
80 | |||
81 | try { |
||
82 | foreach ($manifest['commands'] ?? [] as $command) { |
||
83 | $this->add(new $command()); |
||
84 | } |
||
85 | } catch (\Throwable $e) { |
||
86 | $this->throw($e); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return Highlighter |
||
92 | * @throws \InvalidArgumentException |
||
93 | * @throws \Railt\Lexer\Exception\BadLexemeException |
||
94 | */ |
||
95 | private function bootHighlight(): Highlighter |
||
96 | { |
||
97 | $hl = new Highlighter(); |
||
98 | |||
99 | $hl->add(new PHPLanguage()); |
||
100 | $hl->add(new GraphQLLanguage()); |
||
101 | |||
102 | return $hl; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param LanguageInterface $language |
||
107 | * @return Application |
||
108 | */ |
||
109 | public function addLanguage(LanguageInterface $language): self |
||
110 | { |
||
111 | $this->highlighter->add($language); |
||
112 | |||
113 | return $this; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param InputInterface|null $input |
||
118 | * @param OutputInterface|null $output |
||
119 | * @throws \Throwable |
||
120 | */ |
||
121 | public function gracefulRun(InputInterface $input = null, OutputInterface $output = null): void |
||
122 | { |
||
123 | [$input, $output] = $this->bootPipes($input, $output); |
||
124 | |||
125 | try { |
||
126 | $this->setCatchExceptions(false); |
||
127 | $code = parent::run($input, $output); |
||
128 | } catch (\Throwable $e) { |
||
129 | $code = $this->exitCode($e); |
||
130 | $this->throw($e); |
||
131 | } finally { |
||
132 | exit($code); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param InputInterface|null $input |
||
138 | * @param OutputInterface|null $output |
||
139 | * @return array |
||
140 | * @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
||
141 | * @throws \Symfony\Component\Console\Exception\RuntimeException |
||
142 | */ |
||
143 | private function bootPipes(InputInterface $input = null, OutputInterface $output = null): array |
||
144 | { |
||
145 | $input = $input ?? new ArgvInput(); |
||
146 | $output = $output ?? new ConsoleOutput(); |
||
147 | |||
148 | return [$input, $output]; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param \Throwable $e |
||
153 | * @return int |
||
154 | */ |
||
155 | private function exitCode(\Throwable $e): int |
||
159 | |||
160 | /** |
||
161 | * @param Command $command |
||
162 | * @return Command |
||
163 | * @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
||
164 | */ |
||
165 | public function add(Command $command): Command |
||
175 | |||
176 | /** |
||
177 | * @param \Throwable $e |
||
178 | * @param ConsoleOutput|null $output |
||
179 | * @return void |
||
180 | * @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
||
181 | */ |
||
182 | public function throw(\Throwable $e, ConsoleOutput $output = null): void |
||
194 | } |
||
195 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: