|
1
|
|
|
<?php |
|
2
|
|
|
namespace TwigJs\Compiler\ModuleCompiler; |
|
3
|
|
|
|
|
4
|
|
|
use Twig_NodeInterface; |
|
5
|
|
|
use TwigJs\JsCompiler; |
|
6
|
|
|
use TwigJs\Compiler\ModuleCompiler; |
|
7
|
|
|
use TwigJs\TypeCompilerInterface; |
|
8
|
|
|
|
|
9
|
|
|
class AmdCompiler extends ModuleCompiler implements TypeCompilerInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var boolean |
|
13
|
|
|
*/ |
|
14
|
|
|
private $explicitName; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct($explicitName = true) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->explicitName = $explicitName; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
protected function compileClassHeader(JsCompiler $compiler, Twig_NodeInterface $node) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->functionName = $functionName = $compiler->templateFunctionName |
|
24
|
|
|
= $compiler->getFunctionName($node); |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
$parts = explode('.', $functionName); |
|
27
|
|
|
array_pop($parts); |
|
28
|
|
|
|
|
29
|
|
|
$filename = $node->getAttribute('filename'); |
|
30
|
|
View Code Duplication |
if (!empty($filename) |
|
|
|
|
|
|
31
|
|
|
&& false !== strpos($filename, DIRECTORY_SEPARATOR)) { |
|
32
|
|
|
$parts = explode(DIRECTORY_SEPARATOR, realpath($filename)); |
|
33
|
|
|
$filename = implode(DIRECTORY_SEPARATOR, array_splice($parts, -4)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$compiler |
|
37
|
|
|
->write("/**\n") |
|
38
|
|
|
->write(" * @fileoverview Compiled template for file\n") |
|
39
|
|
|
->write(" *\n") |
|
40
|
|
|
->write(" * ".str_replace('*/', '*\\/', $filename)."\n") |
|
41
|
|
|
->write(" *\n") |
|
42
|
|
|
->write(" * @suppress {checkTypes|fileoverviewTags}\n") |
|
43
|
|
|
->write(" */\n") |
|
44
|
|
|
->write("\n") |
|
45
|
|
|
; |
|
46
|
|
|
|
|
47
|
|
|
if ($this->explicitName) { |
|
48
|
|
|
$compiler->write("define('$functionName.twig', ['twig'], function (Twig) {\n"); |
|
49
|
|
|
} else { |
|
50
|
|
|
$compiler->write("define(['twig'], function (Twig) {\n"); |
|
51
|
|
|
} |
|
52
|
|
|
$compiler |
|
53
|
|
|
->indent() |
|
54
|
|
|
->write("\n") |
|
55
|
|
|
->write( |
|
56
|
|
|
"/**\n", |
|
57
|
|
|
" * @constructor\n", |
|
58
|
|
|
" * @param {twig.Environment} env\n", |
|
59
|
|
|
" * @extends {twig.Template}\n", |
|
60
|
|
|
" */\n" |
|
61
|
|
|
) |
|
62
|
|
|
->write("$functionName = function (env) {\n") |
|
63
|
|
|
->indent() |
|
64
|
|
|
->write("twig.Template.call(this, env);\n") |
|
65
|
|
|
; |
|
66
|
|
|
|
|
67
|
|
View Code Duplication |
if (count($node->getNode('blocks')) || count($node->getNode('traits'))) { |
|
|
|
|
|
|
68
|
|
|
$this->compileConstructor($compiler, $node); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$compiler |
|
72
|
|
|
->outdent() |
|
73
|
|
|
->write("};\n") |
|
74
|
|
|
->write("twig.inherits($functionName, twig.Template);\n\n") |
|
75
|
|
|
; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function compileClassFooter(JsCompiler $compiler, \Twig_NodeInterface $node) |
|
79
|
|
|
{ |
|
80
|
|
|
$compiler |
|
81
|
|
|
->write("return ".$this->functionName.";\n") |
|
82
|
|
|
->outdent() |
|
83
|
|
|
->write("});"); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.