Development

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

You must first sign up to be able to contribute.

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

Revision 17047, 4.1 kB (checked in by fabien, 6 years ago)

[1.0, 1.1, 1.2, 1.3] fixed autoloading configuration cache behaves differently on Windows and Linux (closes #6232)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
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     // parse the yaml
38     $config = self::getConfiguration($configFiles);
39
40     // init our data array
41     $data = array();
42
43     // let's do our fancy work
44     foreach ($config['autoload'] as $name => $entry)
45     {
46       if (isset($entry['name']))
47       {
48         $data[] = sprintf("\n// %s", $entry['name']);
49       }
50
51       // file mapping or directory mapping?
52       if (isset($entry['files']))
53       {
54         // file mapping
55         foreach ($entry['files'] as $class => $path)
56         {
57           $data[] = sprintf("'%s' => '%s',", $class, $path);
58         }
59       }
60       else
61       {
62         // directory mapping
63         $ext  = isset($entry['ext']) ? $entry['ext'] : '.php';
64         $path = $entry['path'];
65
66         // we automatically add our php classes
67         require_once(sfConfig::get('sf_symfony_lib_dir').'/util/sfFinder.class.php');
68         $finder = sfFinder::type('file')->name('*'.$ext)->follow_link();
69
70         // recursive mapping?
71         $recursive = isset($entry['recursive']) ? $entry['recursive'] : false;
72         if (!$recursive)
73         {
74           $finder->maxdepth(0);
75         }
76
77         // exclude files or directories?
78         if (isset($entry['exclude']) && is_array($entry['exclude']))
79         {
80           $finder->prune($entry['exclude'])->discard($entry['exclude']);
81         }
82
83         if ($matches = glob($path))
84         {
85           $files = $finder->in($matches);
86         }
87         else
88         {
89           $files = array();
90         }
91
92         $regex = '~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi';
93         foreach ($files as $file)
94         {
95           preg_match_all($regex, file_get_contents($file), $classes);
96           foreach ($classes[1] as $class)
97           {
98             $prefix = '';
99             if (isset($entry['prefix']))
100             {
101               // FIXME: does not work for plugins installed with a symlink
102               preg_match('~^'.str_replace('\*', '(.+?)', preg_quote(str_replace('/', DIRECTORY_SEPARATOR, $path), '~')).'~', str_replace('/', DIRECTORY_SEPARATOR, $file), $match);
103               if (isset($match[$entry['prefix']]))
104               {
105                 $prefix = $match[$entry['prefix']].'/';
106               }
107             }
108
109             $data[] = sprintf("'%s%s' => '%s',", $prefix, $class, str_replace('\\', '\\\\', $file));
110           }
111         }
112       }
113     }
114
115     // compile data
116     $retval = sprintf("<?php\n".
117                       "// auto-generated by sfAutoloadConfigHandler\n".
118                       "// date: %s\nreturn array(\n%s\n);\n",
119                       date('Y/m/d H:i:s'), implode("\n", $data));
120
121     return $retval;
122   }
123
124   /**
125    * @see sfConfigHandler
126    */
127   static public function getConfiguration(array $configFiles)
128   {
129     $config = self::replaceConstants(self::parseYamls($configFiles));
130
131     foreach ($config['autoload'] as $name => $values)
132     {
133       if (isset($values['path']))
134       {
135         $config['autoload'][$name]['path'] = self::replacePath($values['path']);
136       }
137     }
138
139     return $config;
140   }
141 }
142
Note: See TracBrowser for help on using the browser.