| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
class sfAutoloadConfigHandler extends sfYamlConfigHandler |
|---|
| 21 |
{ |
|---|
| 22 |
|
|---|
| 23 |
* Executes this configuration handler. |
|---|
| 24 |
* |
|---|
| 25 |
* @param array $configFiles An array of absolute filesystem path to a configuration file |
|---|
| 26 |
* |
|---|
| 27 |
* @return string Data to be written to a cache file |
|---|
| 28 |
* |
|---|
| 29 |
* @throws sfConfigurationException If a requested configuration file does not exist or is not readable |
|---|
| 30 |
* @throws sfParseException If a requested configuration file is improperly formatted |
|---|
| 31 |
*/ |
|---|
| 32 |
public function execute($configFiles) |
|---|
| 33 |
{ |
|---|
| 34 |
|
|---|
| 35 |
$this->initialize(array('required_categories' => array('autoload'))); |
|---|
| 36 |
|
|---|
| 37 |
$data = array(); |
|---|
| 38 |
foreach ($this->parse($configFiles) as $name => $mapping) |
|---|
| 39 |
{ |
|---|
| 40 |
$data[] = sprintf("\n // %s", $name); |
|---|
| 41 |
|
|---|
| 42 |
foreach ($mapping as $class => $file) |
|---|
| 43 |
{ |
|---|
| 44 |
$data[] = sprintf(" '%s' => '%s',", $class, str_replace('\\', '\\\\', $file)); |
|---|
| 45 |
} |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
return sprintf("<?php\n". |
|---|
| 50 |
"// auto-generated by sfAutoloadConfigHandler\n". |
|---|
| 51 |
"// date: %s\nreturn array(\n%s\n);\n", |
|---|
| 52 |
date('Y/m/d H:i:s'), implode("\n", $data)); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
public function evaluate($configFiles) |
|---|
| 56 |
{ |
|---|
| 57 |
$mappings = array(); |
|---|
| 58 |
foreach ($this->parse($configFiles) as $mapping) |
|---|
| 59 |
{ |
|---|
| 60 |
foreach ($mapping as $class => $file) |
|---|
| 61 |
{ |
|---|
| 62 |
$mappings[$class] = $file; |
|---|
| 63 |
} |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
return $mappings; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
protected function parse(array $configFiles) |
|---|
| 70 |
{ |
|---|
| 71 |
|
|---|
| 72 |
$config = self::getConfiguration($configFiles); |
|---|
| 73 |
|
|---|
| 74 |
$mappings = array(); |
|---|
| 75 |
foreach ($config['autoload'] as $name => $entry) |
|---|
| 76 |
{ |
|---|
| 77 |
$mapping = array(); |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
if (isset($entry['files'])) |
|---|
| 81 |
{ |
|---|
| 82 |
|
|---|
| 83 |
foreach ($entry['files'] as $class => $file) |
|---|
| 84 |
{ |
|---|
| 85 |
$mapping[strtolower($class)] = $file; |
|---|
| 86 |
} |
|---|
| 87 |
} |
|---|
| 88 |
else |
|---|
| 89 |
{ |
|---|
| 90 |
|
|---|
| 91 |
$ext = isset($entry['ext']) ? $entry['ext'] : '.php'; |
|---|
| 92 |
$path = $entry['path']; |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
require_once(sfConfig::get('sf_symfony_lib_dir').'/util/sfFinder.class.php'); |
|---|
| 96 |
$finder = sfFinder::type('file')->name('*'.$ext)->follow_link(); |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
$recursive = isset($entry['recursive']) ? $entry['recursive'] : false; |
|---|
| 100 |
if (!$recursive) |
|---|
| 101 |
{ |
|---|
| 102 |
$finder->maxdepth(0); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
if (isset($entry['exclude']) && is_array($entry['exclude'])) |
|---|
| 107 |
{ |
|---|
| 108 |
$finder->prune($entry['exclude'])->discard($entry['exclude']); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
if ($matches = glob($path)) |
|---|
| 112 |
{ |
|---|
| 113 |
foreach ($finder->in($matches) as $file) |
|---|
| 114 |
{ |
|---|
| 115 |
$mapping = array_merge($mapping, $this->parseFile($path, $file, isset($entry['prefix']) ? $entry['prefix'] : '')); |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
$mappings[$name] = $mapping; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
return $mappings; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
static public function parseFile($path, $file, $prefix) |
|---|
| 127 |
{ |
|---|
| 128 |
$mapping = array(); |
|---|
| 129 |
preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes); |
|---|
| 130 |
foreach ($classes[1] as $class) |
|---|
| 131 |
{ |
|---|
| 132 |
$localPrefix = ''; |
|---|
| 133 |
if ($prefix) |
|---|
| 134 |
{ |
|---|
| 135 |
|
|---|
| 136 |
preg_match('~^'.str_replace('\*', '(.+?)', preg_quote(str_replace('/', DIRECTORY_SEPARATOR, $path), '~')).'~', str_replace('/', DIRECTORY_SEPARATOR, $file), $match); |
|---|
| 137 |
if (isset($match[$prefix])) |
|---|
| 138 |
{ |
|---|
| 139 |
$localPrefix = $match[$prefix].'/'; |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
$mapping[$localPrefix.strtolower($class)] = $file; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
return $mapping; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
* @see sfConfigHandler |
|---|
| 151 |
*/ |
|---|
| 152 |
static public function getConfiguration(array $configFiles) |
|---|
| 153 |
{ |
|---|
| 154 |
$configuration = sfProjectConfiguration::getActive(); |
|---|
| 155 |
|
|---|
| 156 |
$pluginPaths = $configuration->getPluginPaths(); |
|---|
| 157 |
$pluginConfigFiles = array(); |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
foreach ($configFiles as $i => $configFile) |
|---|
| 161 |
{ |
|---|
| 162 |
$configFilePath = str_replace(DIRECTORY_SEPARATOR, '/', $configFile); |
|---|
| 163 |
$path = str_replace(DIRECTORY_SEPARATOR, '/', realpath(join('/', array_slice(explode('/', $configFilePath), 0, -2)))); |
|---|
| 164 |
if (in_array($path, $pluginPaths)) |
|---|
| 165 |
{ |
|---|
| 166 |
$pluginConfigFiles[] = $configFile; |
|---|
| 167 |
unset($configFiles[$i]); |
|---|
| 168 |
} |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|
| 171 |
$configFiles = array_merge($pluginConfigFiles, $configFiles); |
|---|
| 172 |
|
|---|
| 173 |
$config = self::replaceConstants(self::parseYamls($configFiles)); |
|---|
| 174 |
|
|---|
| 175 |
foreach ($config['autoload'] as $name => $values) |
|---|
| 176 |
{ |
|---|
| 177 |
if (isset($values['path'])) |
|---|
| 178 |
{ |
|---|
| 179 |
$config['autoload'][$name]['path'] = self::replacePath($values['path']); |
|---|
| 180 |
} |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
$event = $configuration->getEventDispatcher()->filter(new sfEvent(__CLASS__, 'autoload.filter_config'), $config); |
|---|
| 184 |
$config = $event->getReturnValue(); |
|---|
| 185 |
|
|---|
| 186 |
return $config; |
|---|
| 187 |
} |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|