nystudio107 /
craft-minify
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Minify plugin for Craft CMS |
||
| 4 | * |
||
| 5 | * @link https://nystudio107.com/ |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 6 | * @copyright Copyright (c) nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 7 | * @license MIT License https://opensource.org/licenses/MIT |
||
| 8 | */ |
||
|
0 ignored issues
–
show
|
|||
| 9 | |||
| 10 | namespace nystudio107\minify\twigextensions; |
||
| 11 | |||
| 12 | use Twig\Error\SyntaxError; |
||
| 13 | use Twig\Token; |
||
| 14 | use Twig\TokenParser\AbstractTokenParser; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Minify twig token parser |
||
| 18 | * |
||
| 19 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 20 | * @package Minify |
||
|
0 ignored issues
–
show
|
|||
| 21 | * @since 1.2.0 |
||
|
0 ignored issues
–
show
|
|||
| 22 | */ |
||
|
0 ignored issues
–
show
|
|||
| 23 | class MinifyTokenParser extends AbstractTokenParser |
||
| 24 | { |
||
| 25 | // Public Methods |
||
| 26 | // ========================================================================= |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Parses {% minify %}...{% endminify %} tags |
||
| 30 | * |
||
| 31 | * @param Token $token |
||
|
0 ignored issues
–
show
|
|||
| 32 | * |
||
| 33 | * @return MinifyNode |
||
| 34 | * @throws SyntaxError |
||
| 35 | */ |
||
| 36 | public function parse(Token $token): MinifyNode |
||
| 37 | { |
||
| 38 | $lineNo = $token->getLine(); |
||
| 39 | $stream = $this->parser->getStream(); |
||
| 40 | |||
| 41 | $attributes = [ |
||
| 42 | 'html' => false, |
||
| 43 | 'css' => false, |
||
| 44 | 'js' => false, |
||
| 45 | ]; |
||
| 46 | |||
| 47 | if ($stream->test(Token::NAME_TYPE, 'html')) { |
||
| 48 | $attributes['html'] = true; |
||
| 49 | $stream->next(); |
||
| 50 | } |
||
| 51 | |||
| 52 | if ($stream->test(Token::NAME_TYPE, 'css')) { |
||
| 53 | $attributes['css'] = true; |
||
| 54 | $stream->next(); |
||
| 55 | } |
||
| 56 | |||
| 57 | if ($stream->test(Token::NAME_TYPE, 'js')) { |
||
| 58 | $attributes['js'] = true; |
||
| 59 | $stream->next(); |
||
| 60 | } |
||
| 61 | |||
| 62 | $stream->expect(Token::BLOCK_END_TYPE); |
||
| 63 | $nodes['body'] = $this->parser->subparse([$this, 'decideMinifyEnd'], true); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 64 | $stream->expect(Token::BLOCK_END_TYPE); |
||
| 65 | |||
| 66 | return new MinifyNode($nodes, $attributes, $lineNo, $this->getTag()); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
|
0 ignored issues
–
show
|
|||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | public function getTag(): string |
||
| 73 | { |
||
| 74 | return 'minify'; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
|
0 ignored issues
–
show
|
|||
| 78 | * @param Token $token |
||
|
0 ignored issues
–
show
|
|||
| 79 | * |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | public function decideMinifyEnd(Token $token): bool |
||
| 83 | { |
||
| 84 | return $token->test('endminify'); |
||
| 85 | } |
||
| 86 | } |
||
| 87 |