Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 200 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 200 Revision 1294
Zeile 9... Zeile 9...
9
 * file that was distributed with this source code.
9
 * file that was distributed with this source code.
10
 */
10
 */
Zeile 11... Zeile 11...
11
 
11
 
Zeile -... Zeile 12...
-
 
12
namespace Psy\Command;
-
 
13
 
12
namespace Psy\Command;
14
use PhpParser\NodeTraverser;
13
 
15
use PhpParser\PrettyPrinter\Standard as Printer;
14
use Psy\CodeCleaner\NoReturnValue;
16
use Psy\CodeCleaner\NoReturnValue;
15
use Psy\Context;
17
use Psy\Context;
16
use Psy\ContextAware;
18
use Psy\ContextAware;
17
use Psy\Exception\ErrorException;
19
use Psy\Exception\ErrorException;
-
 
20
use Psy\Exception\RuntimeException;
18
use Psy\Exception\RuntimeException;
21
use Psy\Exception\UnexpectedTargetException;
19
use Psy\Exception\UnexpectedTargetException;
22
use Psy\ParserFactory;
-
 
23
use Psy\Reflection\ReflectionClassConstant;
20
use Psy\Reflection\ReflectionClassConstant;
24
use Psy\Reflection\ReflectionConstant_;
Zeile 21... Zeile 25...
21
use Psy\Reflection\ReflectionConstant_;
25
use Psy\Sudo\SudoVisitor;
22
use Psy\Util\Mirror;
26
use Psy\Util\Mirror;
23
 
27
 
Zeile 36... Zeile 40...
36
     *
40
     *
37
     * @var Context
41
     * @var Context
38
     */
42
     */
39
    protected $context;
43
    protected $context;
Zeile -... Zeile 44...
-
 
44
 
-
 
45
    private $parser;
-
 
46
    private $traverser;
-
 
47
    private $printer;
-
 
48
 
-
 
49
    /**
-
 
50
     * {@inheritdoc}
-
 
51
     */
-
 
52
    public function __construct($name = null)
-
 
53
    {
-
 
54
        $parserFactory = new ParserFactory();
-
 
55
        $this->parser = $parserFactory->createParser();
-
 
56
 
-
 
57
        $this->traverser = new NodeTraverser();
-
 
58
        $this->traverser->addVisitor(new SudoVisitor());
-
 
59
 
-
 
60
        $this->printer = new Printer();
-
 
61
 
-
 
62
        parent::__construct($name);
-
 
63
    }
40
 
64
 
41
    /**
65
    /**
42
     * ContextAware interface.
66
     * ContextAware interface.
43
     *
67
     *
44
     * @param Context $context
68
     * @param Context $context
Zeile 168... Zeile 192...
168
     * @return mixed Variable value
192
     * @return mixed Variable value
169
     */
193
     */
170
    protected function resolveCode(string $code)
194
    protected function resolveCode(string $code)
171
    {
195
    {
172
        try {
196
        try {
-
 
197
            // Add an implicit `sudo` to target resolution.
-
 
198
            $nodes = $this->traverser->traverse($this->parse($code));
-
 
199
            $sudoCode = $this->printer->prettyPrint($nodes);
173
            $value = $this->getApplication()->execute($code, true);
200
            $value = $this->getApplication()->execute($sudoCode, true);
174
        } catch (\Throwable $e) {
201
        } catch (\Throwable $e) {
175
            // Swallow all exceptions?
202
            // Swallow all exceptions?
176
        }
203
        }
Zeile 177... Zeile 204...
177
 
204
 
Zeile 181... Zeile 208...
181
 
208
 
182
        return $value;
209
        return $value;
Zeile 183... Zeile 210...
183
    }
210
    }
-
 
211
 
-
 
212
    /**
-
 
213
     * Lex and parse a string of code into statements.
-
 
214
     *
-
 
215
     * @param string $code
-
 
216
     *
-
 
217
     * @return array Statements
-
 
218
     */
-
 
219
    private function parse($code)
-
 
220
    {
-
 
221
        $code = '<?php '.$code;
-
 
222
 
-
 
223
        try {
-
 
224
            return $this->parser->parse($code);
-
 
225
        } catch (\PhpParser\Error $e) {
-
 
226
            if (\strpos($e->getMessage(), 'unexpected EOF') === false) {
-
 
227
                throw $e;
-
 
228
            }
-
 
229
 
-
 
230
            // If we got an unexpected EOF, let's try it again with a semicolon.
-
 
231
            return $this->parser->parse($code.';');
-
 
232
        }
-
 
233
    }
184
 
234
 
185
    /**
235
    /**
186
     * Resolve code to an object in the current scope.
236
     * Resolve code to an object in the current scope.
187
     *
237
     *
188
     * @throws UnexpectedTargetException when the code resolves to a non-object value
238
     * @throws UnexpectedTargetException when the code resolves to a non-object value