Development

/branches/1.4/lib/config/sfAutoloadConfigHandler.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/config/sfAutoloadConfigHandler.class.php

Revision 24062, 5.3 kB (checked in by FabianLange, 5 years ago)

[1.3, 1.4] fixed issue with autoloading not correctly sorted in windows (fixes #7226)

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