Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 136 lines (113 sloc) 3.602 kb
0245ee6 @thesalla [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
25 * @param string $file The name of the class map file
26 */
27 static public function dump($dirs, $file)
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 */
46 static public function createMap($dir)
47 {
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 }
64
65 $classes = self::findClasses($path);
66
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 */
83 static private function findClasses($path)
84 {
85 $contents = file_get_contents($path);
86 $tokens = token_get_all($contents);
87
88 $classes = array();
89
90 $namespace = '';
91 for ($i = 0, $max = count($tokens); $i < $max; $i++) {
92 $token = $tokens[$i];
93
94 if (is_string($token)) {
95 continue;
96 }
97
98 $class = '';
99
100 switch ($token[0]) {
101 case T_NAMESPACE:
102 $namespace = '';
103 // If there is a namespace, extract it
104 while (($t = $tokens[++$i]) && is_array($t)) {
105 if (in_array($t[0], array(T_STRING, T_NS_SEPARATOR))) {
106 $namespace .= $t[1];
107 }
108 }
109 $namespace .= '\\';
110 break;
111 case T_CLASS:
112 case T_INTERFACE:
113 // Find the classname
114 while (($t = $tokens[++$i]) && is_array($t)) {
115 if (T_STRING === $t[0]) {
116 $class .= $t[1];
117 } else if ($class !== '' && T_WHITESPACE == $t[0]) {
118 break;
119 }
120 }
121
122 if (empty($namespace)) {
123 $classes[] = $class;
124 } else {
125 $classes[] = $namespace . $class;
126 }
127 break;
128 default:
129 break;
130 }
131 }
132
133 return $classes;
134 }
135 }
Something went wrong with that request. Please try again.