PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/oiclient/data/symfony/config/sfAutoloadConfigHandler.class.php

http://openirudi.googlecode.com/
PHP | 141 lines | 83 code | 17 blank | 41 comment | 9 complexity | 1bae95970df9976250e00130c8ed7491 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-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 9777 2008-06-23 05:32:26Z dwhittle $
  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. // parse the yaml
  35. $config = self::getConfiguration($configFiles);
  36. // init our data array
  37. $data = array();
  38. // let's do our fancy work
  39. foreach ($config['autoload'] as $name => $entry)
  40. {
  41. if (isset($entry['name']))
  42. {
  43. $data[] = sprintf("\n// %s", $entry['name']);
  44. }
  45. // file mapping or directory mapping?
  46. if (isset($entry['files']))
  47. {
  48. // file mapping
  49. foreach ($entry['files'] as $class => $path)
  50. {
  51. $data[] = sprintf("'%s' => '%s',", $class, $path);
  52. }
  53. }
  54. else
  55. {
  56. // directory mapping
  57. $ext = isset($entry['ext']) ? $entry['ext'] : '.php';
  58. $path = $entry['path'];
  59. // we automatically add our php classes
  60. require_once(sfConfig::get('sf_symfony_lib_dir').'/util/sfFinder.class.php');
  61. $finder = sfFinder::type('file')->name('*'.$ext)->follow_link();
  62. // recursive mapping?
  63. $recursive = isset($entry['recursive']) ? $entry['recursive'] : false;
  64. if (!$recursive)
  65. {
  66. $finder->maxdepth(1);
  67. }
  68. // exclude files or directories?
  69. if (isset($entry['exclude']) && is_array($entry['exclude']))
  70. {
  71. $finder->prune($entry['exclude'])->discard($entry['exclude']);
  72. }
  73. if ($matches = glob($path))
  74. {
  75. $files = $finder->in($matches);
  76. }
  77. else
  78. {
  79. $files = array();
  80. }
  81. $regex = '~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi';
  82. foreach ($files as $file)
  83. {
  84. preg_match_all($regex, file_get_contents($file), $classes);
  85. foreach ($classes[1] as $class)
  86. {
  87. $prefix = '';
  88. if (isset($entry['prefix']))
  89. {
  90. // FIXME: does not work for plugins installed with a symlink
  91. preg_match('~^'.str_replace('\*', '(.+?)', preg_quote(str_replace('/', DIRECTORY_SEPARATOR, $path), '~')).'~', $file, $match);
  92. if (isset($match[$entry['prefix']]))
  93. {
  94. $prefix = $match[$entry['prefix']].'/';
  95. }
  96. }
  97. $data[] = sprintf("'%s%s' => '%s',", $prefix, $class, str_replace('\\', '\\\\', $file));
  98. }
  99. }
  100. }
  101. }
  102. // compile data
  103. $retval = sprintf("<?php\n".
  104. "// auto-generated by sfAutoloadConfigHandler\n".
  105. "// date: %s\nreturn array(\n%s\n);\n",
  106. date('Y/m/d H:i:s'), implode("\n", $data));
  107. return $retval;
  108. }
  109. /**
  110. * @see sfConfigHandler
  111. */
  112. static public function getConfiguration(array $configFiles)
  113. {
  114. $config = self::replaceConstants(self::parseYamls($configFiles));
  115. foreach ($config['autoload'] as $name => $values)
  116. {
  117. if (isset($values['path']))
  118. {
  119. $config['autoload'][$name]['path'] = self::replacePath($values['path']);
  120. }
  121. }
  122. return $config;
  123. }
  124. }