PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Group-I/jobeet/lib/vendor/symfony/lib/config/sfAutoloadConfigHandler.class.php

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