8000 [ClassLoader] Add ClassCollectionLoader::inline() to generate inlined-classes files by nicolas-grekas · Pull Request #19276 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[ClassLoader] Add ClassCollectionLoader::inline() to generate inlined-classes files #19276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[ClassLoader] Add ClassCollectionLoader::inline() to generate inlined…
…-classes files
  • Loading branch information
nicolas-grekas committed Jul 17, 2016
commit 5ad1f0ad754a37424cb4fefcadc2b2ed3ad3fb5f
37 changes: 31 additions & 6 deletions src/Symfony/Component/ClassLoader/ClassCollectionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,41 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
$declared = array_merge(get_declared_classes(), get_declared_interfaces(), get_declared_traits());
}

$files = self::inline($classes, $cache, $declared);

if ($autoReload) {
// save the resources
self::writeCacheFile($metadata, serialize(array(array_values($files), $classes)));
}
}

/**
* Generates a file where classes and their parents are inlined.
*
* @param array $classes An array of classes to load
* @param string $cache The file where classes are inlined
* @param array $excluded An array of classes that won't be inlined
*
* @return array The source map of inlined classes, with classes as keys and files as values
*
* @throws \RuntimeException When class can't be loaded
*/
public static function inline($classes, $cache, array $excluded)
{
$declared = array();
foreach (self::getOrderedClasses($excluded) as $class) {
$declared[$class->getName()] = true;
}

$files = array();
$content = '';
foreach (self::getOrderedClasses($classes) as $class) {
if (in_array($class->getName(), $declared)) {
if (isset($declared[$class->getName()])) {
continue;
}
$declared[$class->getName()] = true;

$files[] = $class->getFileName();
$files[$class->getName()] = $class->getFileName();

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

Expand All @@ -116,15 +143,13 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
}

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

if ($autoReload) {
// save the resources
self::writeCacheFile($metadata, serialize(array($files, $classes)));
}
return $files;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\ClassLoader\Tests;

use Symfony\Component\ClassLoader\ClassCollectionLoader;
use Symfony\Component\ClassLoader\Tests\Fixtures\DeclaredClass;
use Symfony\Component\ClassLoader\Tests\Fixtures\WarmedClass;

require_once __DIR__.'/Fixtures/ClassesWithParents/GInterface.php';
require_once __DIR__.'/Fixtures/ClassesWithParents/CInterface.php';
Expand Down Expand Up @@ -271,4 +273,36 @@ class Pearlike_WithComments

unlink($file);
}

public function testInline()
{
$this->assertTrue(class_exists(WarmedClass::class, true));

@unlink($cache = sys_get_temp_dir().'/inline.php');

$classes = array(WarmedClass::class);
$excluded = array(DeclaredClass::class);

ClassCollectionLoader::inline($classes, $cache, $excluded);

$this->assertSame(<<<'EOTXT'
<?php
namespace Symfony\Component\ClassLoader\Tests\Fixtures
{
interface WarmedInterface
{
}
}
namespace Symfony\Component\ClassLoader\Tests\Fixtures
{
class WarmedClass extends DeclaredClass implements WarmedInterface
{
}
}
EOTXT
, file_get_contents($cache)
);

unlink($cache);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\ClassLoader\Tests\Fixtures;

class DeclaredClass implements DeclaredInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\ClassLoader\Tests\Fixtures;

interface DeclaredInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\ClassLoader\Tests\Fixtures;

class WarmedClass extends DeclaredClass implements WarmedInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\ClassLoader\Tests\Fixtures;

interface WarmedInterface
{
}
0