1 | <?php declare(strict_types=1); |
||
11 | class ScriptLoader |
||
12 | { |
||
13 | const TOKEN_MODIFIER_TTY = 'TTY: '; |
||
14 | |||
15 | const TOKEN_MODIFIER_IGNORE_ERROR = 'I: '; |
||
16 | |||
17 | const TOKEN_MODIFIER_DEFERRED = 'D: '; |
||
18 | |||
19 | const TOKEN_INCLUDE = 'INCLUDE: '; |
||
20 | |||
21 | const TOKEN_WAIT = 'WAIT:'; |
||
22 | |||
23 | const TOKEN_TEMPLATE = 'TEMPLATE: '; |
||
24 | |||
25 | const CONCATENATE_PREFIX = ' '; |
||
26 | const TOKEN_WILDCARD = '*'; |
||
27 | |||
28 | /** |
||
29 | * @var CommandBuilder |
||
30 | */ |
||
31 | private $commandBuilder; |
||
32 | |||
33 | /** |
||
34 | * @param CommandBuilder $commandBuilder |
||
35 | */ |
||
36 | public function __construct(CommandBuilder $commandBuilder) |
||
40 | |||
41 | /** |
||
42 | * @param Script $script |
||
43 | * @return Command[] |
||
44 | */ |
||
45 | public function loadScript(Script $script): array |
||
65 | |||
66 | public function createTokenHandler(): array |
||
67 | { |
||
68 | return [ |
||
69 | self::TOKEN_INCLUDE => function (string $currentLine, int $lineNumber, Script $script): string { |
||
70 | $path = $this->findInclude($script, $this->removeFromStart(self::TOKEN_INCLUDE, $currentLine)); |
||
71 | $includeScript = new Script(pathinfo($path, PATHINFO_DIRNAME), pathinfo($path, PATHINFO_BASENAME)); |
||
72 | |||
73 | $commands = $this->loadScript($includeScript); |
||
74 | $this->commandBuilder->replaceCommands($commands); |
||
75 | |||
76 | return ''; |
||
77 | }, |
||
78 | |||
79 | self::TOKEN_TEMPLATE => function (string $currentLine, int $lineNumber, Script $script): string { |
||
80 | $definition = $this->removeFromStart(self::TOKEN_TEMPLATE, $currentLine); |
||
81 | list($rawSource, $rawDestination) = explode(':', $definition); |
||
82 | |||
83 | $source = $script->getDirectory() . '/' . $rawSource; |
||
84 | $destination = $script->getDirectory() . '/' . $rawDestination; |
||
85 | |||
86 | $this->commandBuilder |
||
87 | ->addTemplateCommand($source, $destination, $lineNumber); |
||
88 | |||
89 | return ''; |
||
90 | }, |
||
91 | |||
92 | self::TOKEN_WAIT => function (string $currentLine, int $lineNumber): string { |
||
93 | $this->commandBuilder |
||
94 | ->addWaitCommand($lineNumber); |
||
95 | |||
96 | |||
97 | return ''; |
||
98 | }, |
||
99 | |||
100 | self::TOKEN_MODIFIER_IGNORE_ERROR => function (string $currentLine): string { |
||
101 | $this->commandBuilder->setIgnoreError(); |
||
102 | |||
103 | return $this->removeFromStart(self::TOKEN_MODIFIER_IGNORE_ERROR, $currentLine); |
||
104 | }, |
||
105 | |||
106 | self::TOKEN_MODIFIER_TTY => function (string $currentLine): string { |
||
107 | $this->commandBuilder->setTty(); |
||
108 | |||
109 | return $this->removeFromStart(self::TOKEN_MODIFIER_TTY, $currentLine); |
||
110 | }, |
||
111 | |||
112 | self::TOKEN_MODIFIER_DEFERRED => function (string $currentLine): string { |
||
113 | $this->commandBuilder->setDeferredExecution(); |
||
114 | |||
115 | return $this->removeFromStart(self::TOKEN_MODIFIER_DEFERRED, $currentLine); |
||
116 | }, |
||
117 | |||
118 | self::TOKEN_WILDCARD => function (string $currentLine, int $lineNumber): string { |
||
119 | $this->commandBuilder |
||
120 | ->addProcessCommand($currentLine, $lineNumber); |
||
121 | |||
122 | return ''; |
||
123 | }, |
||
124 | ]; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param Script $fromScript |
||
129 | * @param string $includeStatement |
||
130 | * @return string |
||
131 | */ |
||
132 | private function findInclude(Script $fromScript, string $includeStatement): string |
||
144 | |||
145 | /** |
||
146 | * @param string $command |
||
147 | * @return bool |
||
148 | */ |
||
149 | private function isExecutableLine(string $command): bool |
||
163 | |||
164 | /** |
||
165 | * @param string $needle |
||
166 | * @param string $haystack |
||
167 | * @return string |
||
168 | */ |
||
169 | private function removeFromStart(string $needle, string $haystack): string |
||
173 | |||
174 | /** |
||
175 | * @param string $needle |
||
176 | * @param string $haystack |
||
177 | * @return bool |
||
178 | */ |
||
179 | private function startsWith(string $needle, string $haystack): bool |
||
183 | |||
184 | /** |
||
185 | * @param string $file |
||
186 | * @return string |
||
187 | */ |
||
188 | protected function loadFileContents(string $file): string |
||
192 | |||
193 | /** |
||
194 | * @param string $contents |
||
195 | * @return string[] |
||
196 | */ |
||
197 | private function splitIntoLines(string $contents): array |
||
221 | } |
||
222 |