PageRenderTime 155ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/config/sfAutoloadConfigHandler.class.php

https://github.com/theodo/symfony1.0-backports
PHP | 130 lines | 75 code | 17 blank | 38 comment | 8 complexity | 89a9cad858e8be6b82e7d406cbbdc955 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, BSD-3-Clause
  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$
  17. */
  18. class sfAutoloadConfigHandler extends sfYamlConfigHandler
  19. {
  20. /**
  21. * Executes this configuration handler.
  22. *
  23. * @param array 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. $categories = array('required_categories' => array('autoload'));
  34. $this->initialize($categories);
  35. // parse the yaml
  36. $myConfig = $this->parseYamls($configFiles);
  37. // init our data array
  38. $data = array();
  39. // let's do our fancy work
  40. foreach ($myConfig['autoload'] as $name => $entry)
  41. {
  42. if (isset($entry['name']))
  43. {
  44. $data[] = sprintf("\n// %s", $entry['name']);
  45. }
  46. // file mapping or directory mapping?
  47. if (isset($entry['files']))
  48. {
  49. // file mapping
  50. foreach ($entry['files'] as $class => $path)
  51. {
  52. $path = $this->replaceConstants($path);
  53. $data[] = sprintf("'%s' => '%s',", $class, $path);
  54. }
  55. }
  56. else
  57. {
  58. // directory mapping
  59. $ext = isset($entry['ext']) ? $entry['ext'] : '.php';
  60. $path = $entry['path'];
  61. $path = $this->replaceConstants($path);
  62. $path = $this->replacePath($path);
  63. // we automatically add our php classes
  64. require_once(sfConfig::get('sf_symfony_lib_dir').'/util/sfFinder.class.php');
  65. $finder = sfFinder::type('file')->ignore_version_control()->name('*'.$ext);
  66. // recursive mapping?
  67. $recursive = ((isset($entry['recursive'])) ? $entry['recursive'] : false);
  68. if (!$recursive)
  69. {
  70. $finder->maxdepth(0);
  71. }
  72. // exclude files or directories?
  73. if (isset($entry['exclude']) && is_array($entry['exclude']))
  74. {
  75. $finder->prune($entry['exclude'])->discard($entry['exclude']);
  76. }
  77. if ($matches = glob($path))
  78. {
  79. $files = $finder->in($matches);
  80. }
  81. else
  82. {
  83. $files = array();
  84. }
  85. $regex = '~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi';
  86. foreach ($files as $file)
  87. {
  88. preg_match_all($regex, file_get_contents($file), $classes);
  89. foreach ($classes[1] as $class)
  90. {
  91. $prefix = '';
  92. if (isset($entry['prefix']))
  93. {
  94. // FIXME: does not work for plugins installed with a symlink
  95. preg_match('~^'.str_replace('\*', '(.+?)', preg_quote(str_replace('/', DIRECTORY_SEPARATOR, $path), '~')).'~', str_replace('/', DIRECTORY_SEPARATOR, $file), $match);
  96. if (isset($match[$entry['prefix']]))
  97. {
  98. $prefix = $match[$entry['prefix']].'/';
  99. }
  100. }
  101. $data[] = sprintf("'%s%s' => '%s',", $prefix, $class, str_replace('\\', '\\\\', $file));
  102. }
  103. }
  104. }
  105. }
  106. // compile data
  107. $retval = sprintf("<?php\n".
  108. "// auto-generated by sfAutoloadConfigHandler\n".
  109. "// date: %s\nreturn array(\n%s\n);\n",
  110. date('Y/m/d H:i:s'), implode("\n", $data));
  111. return $retval;
  112. }
  113. }