1: <?php declare(strict_types=1);
2:
3: namespace PhpParser;
4:
5: class ParserFactory
6: {
7: const PREFER_PHP7 = 1;
8: const PREFER_PHP5 = 2;
9: const ONLY_PHP7 = 3;
10: const ONLY_PHP5 = 4;
11:
12: /**
13: * Creates a Parser instance, according to the provided kind.
14: *
15: * @param int $kind One of ::PREFER_PHP7, ::PREFER_PHP5, ::ONLY_PHP7 or ::ONLY_PHP5
16: * @param Lexer|null $lexer Lexer to use. Defaults to emulative lexer when not specified
17: * @param array $parserOptions Parser options. See ParserAbstract::__construct() argument
18: *
19: * @return Parser The parser instance
20: */
21: public function create(int $kind, Lexer $lexer = null, array $parserOptions = []) : Parser {
22: if (null === $lexer) {
23: $lexer = new Lexer\Emulative();
24: }
25: switch ($kind) {
26: case self::PREFER_PHP7:
27: return new Parser\Multiple([
28: new Parser\Php7($lexer, $parserOptions), new Parser\Php5($lexer, $parserOptions)
29: ]);
30: case self::PREFER_PHP5:
31: return new Parser\Multiple([
32: new Parser\Php5($lexer, $parserOptions), new Parser\Php7($lexer, $parserOptions)
33: ]);
34: case self::ONLY_PHP7:
35: return new Parser\Php7($lexer, $parserOptions);
36: case self::ONLY_PHP5:
37: return new Parser\Php5($lexer, $parserOptions);
38: default:
39: throw new \LogicException(
40: 'Kind must be one of ::PREFER_PHP7, ::PREFER_PHP5, ::ONLY_PHP7 or ::ONLY_PHP5'
41: );
42: }
43: }
44: }
45: