Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
6
 * (c) 2012-2022 Justin Hileman
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Psy;
13
 
14
use PhpParser\Parser;
15
use PhpParser\ParserFactory as OriginalParserFactory;
16
 
17
/**
18
 * Parser factory to abstract over PHP parser library versions.
19
 */
20
class ParserFactory
21
{
22
    const ONLY_PHP5 = 'ONLY_PHP5';
23
    const ONLY_PHP7 = 'ONLY_PHP7';
24
    const PREFER_PHP5 = 'PREFER_PHP5';
25
    const PREFER_PHP7 = 'PREFER_PHP7';
26
 
27
    /**
28
     * Possible kinds of parsers for the factory, from PHP parser library.
29
     *
30
     * @return array
31
     */
32
    public static function getPossibleKinds(): array
33
    {
34
        return ['ONLY_PHP5', 'ONLY_PHP7', 'PREFER_PHP5', 'PREFER_PHP7'];
35
    }
36
 
37
    /**
38
     * Default kind (if supported, based on current interpreter's version).
39
     *
40
     * @return string|null
41
     */
42
    public function getDefaultKind()
43
    {
44
        return static::ONLY_PHP7;
45
    }
46
 
47
    /**
48
     * New parser instance with given kind.
49
     *
50
     * @param string|null $kind One of class constants (only for PHP parser 2.0 and above)
51
     *
52
     * @return Parser
53
     */
54
    public function createParser($kind = null): Parser
55
    {
56
        $originalFactory = new OriginalParserFactory();
57
 
58
        $kind = $kind ?: $this->getDefaultKind();
59
 
60
        if (!\in_array($kind, static::getPossibleKinds())) {
61
            throw new \InvalidArgumentException('Unknown parser kind');
62
        }
63
 
64
        $parser = $originalFactory->create(\constant(OriginalParserFactory::class.'::'.$kind));
65
 
66
        return $parser;
67
    }
68
}