Skip to content

Commit 7fe8af2

Browse files
committed
PSR-4 support based on work by @bangpound
1 parent 673c006 commit 7fe8af2

File tree

1 file changed

+87
-5
lines changed

1 file changed

+87
-5
lines changed

src/PatternLab/InstallerUtil.php

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,88 @@ public static function excludeStarterKit($var) {
8989

9090
}
9191

92+
/**
93+
* Extract the classes in the given file.
94+
*
95+
* This method taken from Symfony ClassLoader component.
96+
*
97+
* @see \Symfony\Component\ClassLoader\ClassMapGenerator::findClasses()
98+
* @license http://symfony.com/doc/current/contributing/code/license.html MIT license
99+
*
100+
* @param string $path The file to check
101+
*
102+
* @return array The found classes
103+
*/
104+
private static function findClasses($path) {
105+
106+
$contents = file_get_contents($path);
107+
$tokens = token_get_all($contents);
108+
109+
$classes = array();
110+
111+
$namespace = '';
112+
for ($i = 0; isset($tokens[$i]); ++$i) {
113+
$token = $tokens[$i];
114+
115+
if (!isset($token[1])) {
116+
continue;
117+
}
118+
119+
$class = '';
120+
121+
switch ($token[0]) {
122+
case T_NAMESPACE:
123+
$namespace = '';
124+
// If there is a namespace, extract it
125+
while (isset($tokens[++$i][1])) {
126+
if (in_array($tokens[$i][0], array(T_STRING, T_NS_SEPARATOR))) {
127+
$namespace .= $tokens[$i][1];
128+
}
129+
}
130+
$namespace .= '\\';
131+
break;
132+
case T_CLASS:
133+
case T_INTERFACE:
134+
case T_TRAIT:
135+
// Skip usage of ::class constant
136+
$isClassConstant = false;
137+
for ($j = $i - 1; $j > 0; --$j) {
138+
if (!isset($tokens[$j][1])) {
139+
break;
140+
}
141+
142+
if (T_DOUBLE_COLON === $tokens[$j][0]) {
143+
$isClassConstant = true;
144+
break;
145+
} elseif (!in_array($tokens[$j][0], array(T_WHITESPACE, T_DOC_COMMENT, T_COMMENT))) {
146+
break;
147+
}
148+
}
149+
150+
if ($isClassConstant) {
151+
break;
152+
}
153+
154+
// Find the classname
155+
while (isset($tokens[++$i][1])) {
156+
$t = $tokens[$i];
157+
if (T_STRING === $t[0]) {
158+
$class .= $t[1];
159+
} elseif ('' !== $class && T_WHITESPACE === $t[0]) {
160+
break;
161+
}
162+
}
163+
164+
$classes[] = ltrim($namespace.$class, '\\');
165+
break;
166+
default:
167+
break;
168+
}
169+
}
170+
171+
return $classes;
172+
}
173+
92174
/**
93175
* Parse the component types to figure out what needs to be moved and added to the component JSON files
94176
* @param {String} file path to move
@@ -597,8 +679,8 @@ protected static function scanForListener($pathPackage,$remove = false) {
597679
foreach ($finder as $file) {
598680

599681
// create the name
600-
$dirs = explode(DIRECTORY_SEPARATOR,$file->getPath());
601-
$listenerName = "\\".$dirs[count($dirs)-2]."\\".$dirs[count($dirs)-1]."\\".str_replace(".php","",$file->getFilename());
682+
$classes = self::findClasses($file->getPathname());
683+
$listenerName = "\\".$classes[0];
602684

603685
// check to see what we should do with the listener info
604686
if (!$remove && !in_array($listenerName,$listenerList["listeners"])) {
@@ -639,9 +721,9 @@ protected static function scanForPatternEngineRule($pathPackage,$remove = false)
639721
// iterate over the returned objects
640722
foreach ($finder as $file) {
641723

642-
/// create the name
643-
$dirs = explode(DIRECTORY_SEPARATOR,$file->getPath());
644-
$patternEngineName = "\\".$dirs[count($dirs)-3]."\\".$dirs[count($dirs)-2]."\\".$dirs[count($dirs)-1]."\\".str_replace(".php","",$file->getFilename());
724+
// create the name
725+
$classes = self::findClasses($file->getPathname());
726+
$patternEngineName = "\\".$classes[0];
645727

646728
// check what we should do with the pattern engine info
647729
if (!$remove && !in_array($patternEngineName, $patternEngineList["patternengines"])) {

0 commit comments

Comments
 (0)