Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 134 lines (111 sloc) 3.539 kb
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
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 namespace Symfony\Component\ClassLoader;
13
14 /**
15 * ClassMapGenerator
16 *
17 * @author Gyula Sallai <salla016@gmail.com>
18 */
19 class ClassMapGenerator
20 {
21 /**
22 * Generate a class map file
23 *
24 * @param array|string $dirs Directories or a single path to search in
4e509e3 Włodzimierz Gajda [2.1][Component][ClassLoader] cs
gajdaw authored
25 * @param string $file The name of the class map file
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
26 */
5256043 Fabien Potencier fixed CS
fabpot authored
27 public static function dump($dirs, $file)
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
28 {
29 $dirs = (array) $dirs;
30 $maps = array();
31
32 foreach ($dirs as $dir) {
33 $maps = array_merge($maps, static::createMap($dir));
34 }
35
36 file_put_contents($file, sprintf('<?php return %s;', var_export($maps, true)));
37 }
38
39 /**
40 * Iterate over all files in the given directory searching for classes
41 *
42 * @param Iterator|string $dir The directory to search in or an iterator
43 *
44 * @return array A class map array
45 */
5256043 Fabien Potencier fixed CS
fabpot authored
46 public static function createMap($dir)
3c683c9 Fabien Potencier fixed CS
fabpot authored
47 {
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
48 if (is_string($dir)) {
49 $dir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
50 }
51
52 $map = array();
53
54 foreach ($dir as $file) {
55 if (!$file->isFile()) {
56 continue;
57 }
58
59 $path = $file->getRealPath();
60
61 if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
62 continue;
63 }
3c683c9 Fabien Potencier fixed CS
fabpot authored
64
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
65 $classes = self::findClasses($path);
3c683c9 Fabien Potencier fixed CS
fabpot authored
66
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
67 foreach ($classes as $class) {
68 $map[$class] = $path;
69 }
70
71 }
72
73 return $map;
74 }
75
76 /**
77 * Extract the classes in the given file
78 *
79 * @param string $path The file to check
80 *
81 * @return array The found classes
82 */
5256043 Fabien Potencier fixed CS
fabpot authored
83 private static function findClasses($path)
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
84 {
85 $contents = file_get_contents($path);
86 $tokens = token_get_all($contents);
da83c40 Martin Hasoň [ClassLoader] Fixed version compare
hason authored
87 $T_TRAIT = version_compare(PHP_VERSION, '5.4', '<') ? -1 : T_TRAIT;
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
88
89 $classes = array();
90
91 $namespace = '';
92 for ($i = 0, $max = count($tokens); $i < $max; $i++) {
93 $token = $tokens[$i];
3c683c9 Fabien Potencier fixed CS
fabpot authored
94
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
95 if (is_string($token)) {
96 continue;
97 }
98
99 $class = '';
100
101 switch ($token[0]) {
102 case T_NAMESPACE:
103 $namespace = '';
104 // If there is a namespace, extract it
105 while (($t = $tokens[++$i]) && is_array($t)) {
106 if (in_array($t[0], array(T_STRING, T_NS_SEPARATOR))) {
107 $namespace .= $t[1];
108 }
109 }
110 $namespace .= '\\';
111 break;
112 case T_CLASS:
113 case T_INTERFACE:
cf6a149 Martin Hasoň [ClassLoader] Fixed ClassMapGenerator and added suport for traits
hason authored
114 case $T_TRAIT:
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
115 // Find the classname
116 while (($t = $tokens[++$i]) && is_array($t)) {
117 if (T_STRING === $t[0]) {
118 $class .= $t[1];
3c683c9 Fabien Potencier fixed CS
fabpot authored
119 } elseif ($class !== '' && T_WHITESPACE == $t[0]) {
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
120 break;
121 }
122 }
123
cf6a149 Martin Hasoň [ClassLoader] Fixed ClassMapGenerator and added suport for traits
hason authored
124 $classes[] = ltrim($namespace . $class, '\\');
0245ee6 Gyula Sallai [ClassLoader] Added a class map file generator utility
thesalla authored
125 break;
126 default:
127 break;
128 }
129 }
130
131 return $classes;
132 }
133 }
Something went wrong with that request. Please try again.