1 | <?php |
||
28 | class Generator |
||
29 | { |
||
30 | /** string All generated view classes will end with this suffix */ |
||
31 | const VIEW_CLASSNAME_SUFFIX = 'View'; |
||
32 | |||
33 | /** @var array Collection of PHP reserved words */ |
||
34 | protected static $reservedWords = array( |
||
35 | 'list' |
||
36 | ); |
||
37 | |||
38 | /** @var Metadata[] Collection of view metadata */ |
||
39 | protected $metadata = array(); |
||
40 | |||
41 | /** @var \samsonphp\generator\Generator */ |
||
42 | protected $generator; |
||
43 | |||
44 | /** @var string Generated classes namespace prefix */ |
||
45 | protected $namespacePrefix; |
||
46 | |||
47 | /** |
||
48 | * Generator constructor. |
||
49 | * |
||
50 | * @param \samsonphp\generator\Generator $generator |
||
51 | * @param string $namespacePrefix |
||
52 | */ |
||
53 | 1 | public function __construct(\samsonphp\generator\Generator $generator, $namespacePrefix) |
|
58 | |||
59 | /** |
||
60 | * Recursively scan path for files with specified extensions. |
||
61 | * |
||
62 | * @param string $sourcepath Entry point path |
||
63 | * @param string $path Entry path for scanning |
||
64 | * @param array $extensions Collection of file extensions without dot |
||
65 | */ |
||
66 | 1 | public function scan($sourcepath, array $extensions = array(View::DEFAULT_EXT), $path = null) |
|
86 | |||
87 | /** |
||
88 | * Analyze view file and create its metadata. |
||
89 | * |
||
90 | * @param string $file Path to view file |
||
91 | * |
||
92 | * @return Metadata View file metadata |
||
93 | */ |
||
94 | 1 | public function analyze($file) |
|
95 | { |
||
96 | 1 | $metadata = new Metadata(); |
|
97 | // Use PHP tokenizer to find variables |
||
98 | 1 | foreach ($tokens = token_get_all(file_get_contents($file)) as $idx => $token) { |
|
99 | 1 | if (!is_string($token) && $token[0] === T_VARIABLE) { |
|
100 | // Store variable |
||
101 | 1 | $variableText = $token[1]; |
|
102 | // Store variable name |
||
103 | 1 | $variableName = ltrim($token[1], '$'); |
|
104 | // If next token is object operator |
||
105 | 1 | if ($tokens[$idx + 1][0] === T_OBJECT_OPERATOR) { |
|
106 | $variableName = $tokens[$idx + 2][1]; |
||
107 | // And two more tokens |
||
108 | $variableText .= $tokens[$idx + 1][1] . $variableName; |
||
109 | } |
||
110 | // Store view variable key - actual object name => full varaible usage |
||
111 | 1 | $metadata->variables[$variableName] = $variableText; |
|
112 | 1 | } |
|
113 | 1 | } |
|
114 | |||
115 | 1 | return $metadata; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Generic class name and its name space generator. |
||
120 | * |
||
121 | * @param string $file Full path to view file |
||
122 | * @param string $entryPath Entry path |
||
123 | * |
||
124 | * @return array Class name[0] and namespace[1] |
||
125 | */ |
||
126 | 1 | protected function generateClassName($file, $entryPath) |
|
151 | |||
152 | 1 | public function generate($path = __DIR__) |
|
158 | |||
159 | 1 | protected function generateViewClass(Metadata $metadata, $path) |
|
183 | |||
184 | /** |
||
185 | * Generate constructor for application class. |
||
186 | * |
||
187 | * @param string $variable View variable name |
||
188 | * |
||
189 | * @return string View variable setter method |
||
190 | */ |
||
191 | 1 | protected function generateViewVariableSetter($variable) |
|
206 | } |
||
207 |
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.