8000 [ClassLoader] Add ClassCollectionLoader::inline() to generate inlined… · symfony/symfony@d53fa08 · GitHub
[go: up one dir, main page]

Skip to content

Commit d53fa08

Browse files
[ClassLoader] Add ClassCollectionLoader::inline() to generate inlined-classes files
1 parent 5213778 commit d53fa08

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/Symfony/Component/ClassLoader/ClassCollectionLoader.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,41 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
9090
return;
9191
}
9292

93+
$files = self::inline($classes, $cache, $declared);
94+
95+
if ($autoReload) {
96+
// save the resources
97+
self::writeCacheFile($metadata, serialize(array(array_values($files), $classes)));
98+
}
99+
}
100+
101+
/**
102+
* Generates a file where classes and their parents are inlined.
103+
*
104+
* @param array $classes An array of classes to load
105+
* @param string $cache The file where classes are inlined
106+
* @param array $excluded An array of classes that won't be inlined
107+
*
108+
* @return array The source map of inlined classes, with classes as keys and files as values
109+
*
110+
* @throws \RuntimeException When class can't be loaded
111+
*/
112+
public static function inline($classes, $cache, array $excluded)
113+
{
114+
$declared = array();
115+
foreach (self::getOrderedClasses($excluded) as $class) {
116+
$declared[$class->getName()] = true;
117+
}
118+
93119
$files = array();
94120
$content = '';
95121
foreach (self::getOrderedClasses($classes) as $class) {
96-
if (in_array($class->getName(), $declared)) {
122+
if (isset($declared[$class->getName()])) {
97123
continue;
98124
}
125+
$declared[$class->getName()] = true;
99126

100-
$files[] = $class->getFileName();
127+
$files[$class->getName()] = $class->getFileName();
101128

102129
$c = preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($class->getFileName()));
103130

@@ -113,15 +140,13 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
113140
}
114141

115142
// cache the core classes
143+
$cacheDir = dirname($cache);
116144
if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) {
117145
throw new \RuntimeException(sprintf('Class Collection Loader was not able to create directory "%s"', $cacheDir));
118146
}
119147
self::writeCacheFile($cache, '<?php '.$content);
120148

121-
if ($autoReload) {
122-
// save the resources
123-
self::writeCacheFile($metadata, serialize(array($files, $classes)));
124-
}
149+
return $files;
125150
}
126151

127152
/**

0 commit comments

Comments
 (0)
0