Development

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

You must first sign up to be able to contribute.

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

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