1. sfAutoloadConfigHandler.class.php
  2. /** * * @package symfony * @subpackage config * @author Fabien Potencier * @author Sean Kerr * @version SVN: $Id: sfAutoloadConfigHandler.class.php 24062 2009-11-16 23:31:25Z FabianLange $ */
  3. class sfAutoloadConfigHandler extends sfYamlConfigHandler
  4. {
  5. /**
  6. * Executes this configuration handler.
  7. *
  8. * @param array $configFiles An array of absolute filesystem path to a configuration file
  9. *
  10. * @return string Data to be written to a cache file
  11. *
  12. * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
  13. * @throws sfParseException If a requested configuration file is improperly formatted
  14. */
  15. public function execute($configFiles)
  16. {
  17. // set our required categories list and initialize our handler
  18. $this->initialize(array('required_categories' => array('autoload')));
  19. $data = array();
  20. foreach ($this->parse($configFiles) as $name => $mapping)
  21. {
  22. $data[] = sprintf("\n // %s", $name);
  23. foreach ($mapping as $class => $file)
  24. {
  25. $data[] = sprintf(" '%s' => '%s',", $class, str_replace('\\', '\\\\', $file));
  26. }
  27. }
  28. // compile data
  29. return sprintf("<?php\n".
  30. "// auto-generated by sfAutoloadConfigHandler\n".
  31. "// date: %s\nreturn array(\n%s\n);\n",
  32. date('Y/m/d H:i:s'), implode("\n", $data));
  33. }
  34. public function evaluate($configFiles)
  35. {
  36. $mappings = array();
  37. foreach ($this->parse($configFiles) as $mapping)
  38. {
  39. foreach ($mapping as $class => $file)
  40. {
  41. $mappings[$class] = $file;
  42. }
  43. }
  44. return $mappings;
  45. }
  46. protected function parse(array $configFiles)
  47. {
  48. // parse the yaml
  49. $config = self::getConfiguration($configFiles);
  50. $mappings = array();
  51. foreach ($config['autoload'] as $name => $entry)
  52. {
  53. $mapping = array();
  54. // file mapping or directory mapping?
  55. if (isset($entry['files']))
  56. {
  57. // file mapping
  58. foreach ($entry['files'] as $class => $file)
  59. {
  60. $mapping[strtolower($class)] = $file;
  61. }
  62. }
  63. else
  64. {
  65. // directory mapping
  66. $ext = isset($entry['ext']) ? $entry['ext'] : '.php';
  67. $path = $entry['path'];
  68. // we automatically add our php classes
  69. require_once(sfConfig::get('sf_symfony_lib_dir').'/util/sfFinder.class.php');
  70. $finder = sfFinder::type('file')->name('*'.$ext)->follow_link();
  71. // recursive mapping?
  72. $recursive = isset($entry['recursive']) ? $entry['recursive'] : false;
  73. if (!$recursive)
  74. {
  75. $finder->maxdepth(0);
  76. }
  77. // exclude files or directories?
  78. if (isset($entry['exclude']) && is_array($entry['exclude']))
  79. {
  80. $finder->prune($entry['exclude'])->discard($entry['exclude']);
  81. }
  82. if ($matches = glob($path))
  83. {
  84. foreach ($finder->in($matches) as $file)
  85. {
  86. $mapping = array_merge($mapping, $this->parseFile($path, $file, isset($entry['prefix']) ? $entry['prefix'] : ''));
  87. }
  88. }
  89. }
  90. $mappings[$name] = $mapping;
  91. }
  92. return $mappings;
  93. }
  94. static public function parseFile($path, $file, $prefix)
  95. {
  96. $mapping = array();
  97. preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes);
  98. foreach ($classes[1] as $class)
  99. {
  100. $localPrefix = '';
  101. if ($prefix)
  102. {
  103. // FIXME: does not work for plugins installed with a symlink
  104. preg_match('~^'.str_replace('\*', '(.+?)', preg_quote(str_replace('/', DIRECTORY_SEPARATOR, $path), '~')).'~', str_replace('/', DIRECTORY_SEPARATOR, $file), $match);
  105. if (isset($match[$prefix]))
  106. {
  107. $localPrefix = $match[$prefix].'/';
  108. }
  109. }
  110. $mapping[$localPrefix.strtolower($class)] = $file;
  111. }
  112. return $mapping;
  113. }
  114. /**
  115. * @see sfConfigHandler
  116. */
  117. static public function getConfiguration(array $configFiles)
  118. {
  119. $configuration = sfProjectConfiguration::getActive();
  120. $pluginPaths = $configuration->getPluginPaths();
  121. $pluginConfigFiles = array();
  122. // move plugin files to front
  123. foreach ($configFiles as $i => $configFile)
  124. {
  125. $configFilePath = str_replace(DIRECTORY_SEPARATOR, '/', $configFile);
  126. $path = str_replace(DIRECTORY_SEPARATOR, '/', realpath(join('/', array_slice(explode('/', $configFilePath), 0, -2))));
  127. if (in_array($path, $pluginPaths))
  128. {
  129. $pluginConfigFiles[] = $configFile;
  130. unset($configFiles[$i]);
  131. }
  132. }
  133. $configFiles = array_merge($pluginConfigFiles, $configFiles);
  134. $config = self::replaceConstants(self::parseYamls($configFiles));
  135. foreach ($config['autoload'] as $name => $values)
  136. {
  137. if (isset($values['path']))
  138. {
  139. $config['autoload'][$name]['path'] = self::replacePath($values['path']);
  140. }
  141. }
  142. $event = $configuration->getEventDispatcher()->filter(new sfEvent(__CLASS__, 'autoload.filter_config'), $config);
  143. $config = $event->getReturnValue();
  144. return $config;
  145. }
  146. }

Debug toolbar