sfAutoloadConfigHandler.class.php 5.41 KB
Newer Older
Игорь's avatar
init    
Игорь committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 * (c) 2004-2006 Sean Kerr <sean@code-box.org>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * @package    symfony
 * @subpackage config
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @author     Sean Kerr <sean@code-box.org>
 * @version    SVN: $Id: sfAutoloadConfigHandler.class.php 24062 2009-11-16 23:31:25Z FabianLange $
 */
class sfAutoloadConfigHandler extends sfYamlConfigHandler
{
  /**
   * Executes this configuration handler.
   *
   * @param array $configFiles An array of absolute filesystem path to a configuration file
   *
   * @return string Data to be written to a cache file
   *
   * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
   * @throws sfParseException If a requested configuration file is improperly formatted
   */
  public function execute($configFiles)
  {
    // set our required categories list and initialize our handler
    $this->initialize(array('required_categories' => array('autoload')));

    $data = array();
    foreach ($this->parse($configFiles) as $name => $mapping)
    {
      $data[] = sprintf("\n  // %s", $name);

      foreach ($mapping as $class => $file)
      {
        $data[] = sprintf("  '%s' => '%s',", $class, str_replace('\\', '\\\\', $file));
      }
    }

    // compile data
    return sprintf("<?php\n".
                      "// auto-generated by sfAutoloadConfigHandler\n".
                      "// date: %s\nreturn array(\n%s\n);\n",
                      date('Y/m/d H:i:s'), implode("\n", $data));
  }

  public function evaluate($configFiles)
  {
    $mappings = array();
    foreach ($this->parse($configFiles) as $mapping)
    {
      foreach ($mapping as $class => $file)
      {
        $mappings[$class] = $file;
      }
    }

    return $mappings;
  }

  protected function parse(array $configFiles)
  {
    // parse the yaml
    $config = self::getConfiguration($configFiles);

    $mappings = array();
    foreach ($config['autoload'] as $name => $entry)
    {
      $mapping = array();

      // file mapping or directory mapping?
      if (isset($entry['files']))
      {
        // file mapping
        foreach ($entry['files'] as $class => $file)
        {
          $mapping[strtolower($class)] = $file;
        }
      }
      else
      {
        // directory mapping
        $ext  = isset($entry['ext']) ? $entry['ext'] : '.php';
        $path = $entry['path'];

        // we automatically add our php classes
        require_once(sfConfig::get('sf_symfony_lib_dir').'/util/sfFinder.class.php');
        $finder = sfFinder::type('file')->name('*'.$ext)->follow_link();

        // recursive mapping?
        $recursive = isset($entry['recursive']) ? $entry['recursive'] : false;
        if (!$recursive)
        {
          $finder->maxdepth(0);
        }

        // exclude files or directories?
        if (isset($entry['exclude']) && is_array($entry['exclude']))
        {
          $finder->prune($entry['exclude'])->discard($entry['exclude']);
        }

        if ($matches = glob($path))
        {
          foreach ($finder->in($matches) as $file)
          {
            $mapping = array_merge($mapping, $this->parseFile($path, $file, isset($entry['prefix']) ? $entry['prefix'] : ''));
          }
        }
      }

      $mappings[$name] = $mapping;
    }

    return $mappings;
  }

  static public function parseFile($path, $file, $prefix)
  {
    $mapping = array();
    preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes);
    foreach ($classes[1] as $class)
    {
      $localPrefix = '';
      if ($prefix)
      {
        // FIXME: does not work for plugins installed with a symlink
        preg_match('~^'.str_replace('\*', '(.+?)', preg_quote(str_replace('/', DIRECTORY_SEPARATOR, $path), '~')).'~', str_replace('/', DIRECTORY_SEPARATOR, $file), $match);
        if (isset($match[$prefix]))
        {
          $localPrefix = $match[$prefix].'/';
        }
      }

      $mapping[$localPrefix.strtolower($class)] = $file;
    }

    return $mapping;
  }

  /**
   * @see sfConfigHandler
   */
  static public function getConfiguration(array $configFiles)
  {
    $configuration = sfProjectConfiguration::getActive();

    $pluginPaths = $configuration->getPluginPaths();
    $pluginConfigFiles = array();

    // move plugin files to front
    foreach ($configFiles as $i => $configFile)
    {
      $configFilePath = str_replace(DIRECTORY_SEPARATOR, '/', $configFile);
      $path = str_replace(DIRECTORY_SEPARATOR, '/', realpath(join('/', array_slice(explode('/', $configFilePath), 0, -2))));
      if (in_array($path, $pluginPaths))
      {
        $pluginConfigFiles[] = $configFile;
        unset($configFiles[$i]);
      }
    }

    $configFiles = array_merge($pluginConfigFiles, $configFiles);

    $config = self::replaceConstants(self::parseYamls($configFiles));

    foreach ($config['autoload'] as $name => $values)
    {
      if (isset($values['path']))
      {
        $config['autoload'][$name]['path'] = self::replacePath($values['path']);
      }
    }

    $event = $configuration->getEventDispatcher()->filter(new sfEvent(__CLASS__, 'autoload.filter_config'), $config);
    $config = $event->getReturnValue();

    return $config;
  }
}