1 | <?php |
||
15 | class SS_ConfigStaticManifest { |
||
16 | |||
17 | protected $base; |
||
18 | protected $tests; |
||
19 | |||
20 | protected $cache; |
||
21 | protected $key; |
||
22 | |||
23 | protected $index; |
||
24 | protected $statics; |
||
25 | |||
26 | static protected $initial_classes = array( |
||
27 | 'Object', 'ViewableData', 'Injector', 'Director' |
||
28 | ); |
||
29 | |||
30 | /** |
||
31 | * Constructs and initialises a new config static manifest, either loading the data |
||
32 | * from the cache or re-scanning for classes. |
||
33 | * |
||
34 | * @param string $base The manifest base path. |
||
35 | * @param bool $includeTests Include the contents of "tests" directories. |
||
36 | * @param bool $forceRegen Force the manifest to be regenerated. |
||
37 | * @param bool $cache If the manifest is regenerated, cache it. |
||
38 | */ |
||
39 | public function __construct($base, $includeTests = false, $forceRegen = false, $cache = true) { |
||
40 | $this->base = $base; |
||
41 | $this->tests = $includeTests; |
||
42 | |||
43 | $cacheClass = defined('SS_MANIFESTCACHE') ? SS_MANIFESTCACHE : 'ManifestCache_File'; |
||
44 | |||
45 | $this->cache = new $cacheClass('staticmanifest'.($includeTests ? '_tests' : '')); |
||
46 | $this->key = sha1($base); |
||
47 | |||
48 | if(!$forceRegen) { |
||
49 | $this->index = $this->cache->load($this->key); |
||
50 | } |
||
51 | |||
52 | if($this->index) { |
||
53 | $this->statics = $this->index['$statics']; |
||
54 | } |
||
55 | else { |
||
56 | $this->regenerate($cache); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | public function get($class, $name, $default) { |
||
61 | if (!isset($this->statics[$class])) { |
||
62 | if (isset($this->index[$class])) { |
||
63 | $info = $this->index[$class]; |
||
64 | |||
65 | if (isset($info['key']) && $details = $this->cache->load($this->key.'_'.$info['key'])) { |
||
66 | $this->statics += $details; |
||
67 | } |
||
68 | |||
69 | if (!isset($this->statics[$class])) { |
||
70 | $this->handleFile(null, $info['path'], null); |
||
71 | } |
||
72 | } |
||
73 | else { |
||
74 | $this->statics[$class] = false; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | if (isset($this->statics[$class][$name])) { |
||
79 | $static = $this->statics[$class][$name]; |
||
80 | |||
81 | if ($static['access'] != T_PRIVATE) { |
||
82 | Deprecation::notice('4.0', "Config static $class::\$$name must be marked as private", |
||
83 | Deprecation::SCOPE_GLOBAL); |
||
84 | // Don't warn more than once per static |
||
85 | $this->statics[$class][$name]['access'] = T_PRIVATE; |
||
86 | } |
||
87 | |||
88 | return $static['value']; |
||
89 | } |
||
90 | |||
91 | return $default; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Completely regenerates the manifest file. |
||
96 | */ |
||
97 | public function regenerate($cache = true) { |
||
98 | $this->index = array('$statics' => array()); |
||
99 | $this->statics = array(); |
||
100 | |||
101 | $finder = new ManifestFileFinder(); |
||
102 | $finder->setOptions(array( |
||
103 | 'name_regex' => '/^([^_].*\.php)$/', |
||
104 | 'ignore_files' => array('index.php', 'main.php', 'cli-script.php', 'SSTemplateParser.php'), |
||
105 | 'ignore_tests' => !$this->tests, |
||
106 | 'file_callback' => array($this, 'handleFile') |
||
107 | )); |
||
108 | |||
109 | $finder->find($this->base); |
||
110 | |||
111 | if($cache) { |
||
112 | $keysets = array(); |
||
113 | |||
114 | foreach ($this->statics as $class => $details) { |
||
115 | if (in_array($class, self::$initial_classes)) { |
||
116 | $this->index['$statics'][$class] = $details; |
||
117 | } |
||
118 | else { |
||
119 | $key = sha1($class); |
||
120 | $this->index[$class]['key'] = $key; |
||
121 | |||
122 | $keysets[$key][$class] = $details; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | foreach ($keysets as $key => $details) { |
||
127 | $this->cache->save($details, $this->key.'_'.$key); |
||
128 | } |
||
129 | |||
130 | $this->cache->save($this->index, $this->key); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | public function handleFile($basename, $pathname, $depth) { |
||
|
|||
135 | $parser = new SS_ConfigStaticManifest_Parser($pathname); |
||
136 | $parser->parse(); |
||
137 | |||
138 | $this->index = array_merge($this->index, $parser->getInfo()); |
||
139 | $this->statics = array_merge($this->statics, $parser->getStatics()); |
||
140 | } |
||
141 | |||
142 | public function getStatics() { |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * A parser that processes a PHP file, using PHP's built in parser to get a string of tokens, |
||
149 | * then processing them to find the static class variables, their access levels & values |
||
386 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.