8000 [FrameworkBundle] Fix class_exists() checks in PhpArrayAdapter-related cache warmers by nicolas-grekas · Pull Request #21243 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Fix class_exists() checks in PhpArrayAdapter-related cache warmers #21243

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

Merged
merged 2 commits into from
Jan 12, 2017
Merged
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
[FrameworkBundle] Fix class_exists() checks in PhpArrayAdapter-relate…
…d cache warmers
  • Loading branch information
nicolas-grekas committed Jan 12, 2017
commit c60009eae8506cfd31786fdcd867b4e76478345a
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class AnnotationsCacheWarmer implements CacheWarmerInterface

/**
* @param Reader $annotationReader
* @param string $phpArrayFile The PHP file where annotations are cached.
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered annotations are cached.
* @param string $phpArrayFile the PHP file where annotations are cached
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered annotations are cached
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maj should be reverted, dots can be removed indeed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted

*/
public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
{
Expand Down Expand Up @@ -67,9 +67,8 @@ public function warmUp($cacheDir)

$arrayPool = new ArrayAdapter(0, false);
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayPool));
$throwingAutoloader = function ($class) { throw new \ReflectionException(sprintf('Class %s does not exist', $class)); };
spl_autoload_register($throwingAutoloader);

spl_autoload_register(array($adapter, 'throwOnRequiredClass'));
try {
foreach ($annotatedClasses as $class) {
try {
Expand All @@ -88,7 +87,7 @@ public function warmUp($cacheDir)
}
}
} finally {
spl_autoload_unregister($throwingAutoloader);
spl_autoload_unregister(array($adapter, 'throwOnRequiredClass'));
}

$values = $arrayPool->getValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;

use Doctrine\Common\Annotations\AnnotationException;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
Expand All @@ -36,9 +37,9 @@ class SerializerCacheWarmer implements CacheWarmerInterface
private $fallbackPool;

/**
* @param LoaderInterface[] $loaders The serializer metadata loaders.
* @param string $phpArrayFile The PHP file where metadata are cached.
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached.
* @param LoaderInterface[] $loaders the serializer metadata loaders
* @param string $phpArrayFile the PHP file where metadata are cached
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered metadata are cached
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

*/
public function __construct(array $loaders, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
{
Expand All @@ -64,10 +65,21 @@ public function warmUp($cacheDir)

$metadataFactory = new CacheClassMetadataFactory(new ClassMetadataFactory(new LoaderChain($this->loaders)), $arrayPool);

foreach ($this->extractSupportedLoaders($this->loaders) as $loader) {
foreach ($loader->getMappedClasses() as $mappedClass) {
$metadataFactory->getMetadataFor($mappedClass);
spl_autoload_register(array($adapter, 'throwOnRequiredClass'));
try {
foreach ($this->extractSupportedLoaders($this->loaders) as $loader) {
foreach ($loader->getMappedClasses() as $mappedClass) {
try {
$metadataFactory->getMetadataFor($mappedClass);
} catch (\ReflectionException $e) {
// ignore failing reflection
} catch (AnnotationException $e) {
// ignore failing annotations
}
}
}
} finally {
spl_autoload_unregister(array($adapter, 'throwOnRequiredClass'));
}

$values = $arrayPool->getValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;

use Doctrine\Common\Annotations\AnnotationException;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
Expand Down Expand Up @@ -38,8 +39,8 @@ class ValidatorCacheWarmer implements CacheWarmerInterface

/**
* @param ValidatorBuilderInterface $validatorBuilder
* @param string $phpArrayFile The PHP file where metadata are cached.
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached.
* @param string $phpArrayFile the PHP file where metadata are cached
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered metadata are cached
*/
public function __construct(ValidatorBuilderInterface $validatorBuilder, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
{
Expand All @@ -66,9 +67,7 @@ public function warmUp($cacheDir)
$loaders = $this->validatorBuilder->getLoaders();
$metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), new Psr6Cache($arrayPool));

$throwingAutoloader = function ($class) { throw new \ReflectionException(sprintf('Class %s does not exist', $class)); };
spl_autoload_register($throwingAutoloader);

spl_autoload_register(array($adapter, 'throwOnRequiredClass'));
try {
foreach ($this->extractSupportedLoaders($loaders) as $loader) {
foreach ($loader->getMappedClasses() as $mappedClass) {
Expand All @@ -78,15 +77,17 @@ public function warmUp($cacheDir)
}
} catch (\ReflectionException $e) {
// ignore failing reflection
} catch (AnnotationException $e) {
// ignore failing annotations
}
}
}
} finally {
spl_autoload_unregister($throwingAutoloader);
spl_autoload_unregister(array($adapter, 'throwOnRequiredClass'));
}

$values = $arrayPool->getValues();
$adapter->warmUp($values);
$adapter->warmUp(array_filter($values));

foreach ($values as $k => $v) {
$item = $this->fallbackPool->getItem($k);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": ">=5.5.9",
"symfony/cache": "~3.2",
"symfony/cache": "~3.2.2|~3.3",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not ^3.2.2?

"symfony/class-loader": "~3.2",
"symfony/dependency-injection": "~3.2.1|~3.3",
"symfony/config": "~2.8|~3.0",
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,42 @@ private function generateItems(array $keys)
}
}
}

/**
* @throws \ReflectionException When $class is not found and is required
*
* @internal
*/
public static function throwOnRequiredClass($class)
{
$e = new \ReflectionException(sprintf('Class %s does not exist', $class));
$trace = $e->getTrace();
$autoloadFrame = array(
'function' => 'spl_autoload_call',
'args' => array($class),
);
$i = array_search($autoloadFrame, $trace);

if (false !== $i++ && isset($trace[$i]['function']) && !isset($trace[$i]['class'])) {
switch ($trace[$i]['function']) {
case 'get_class_methods':
case 'get_class_vars':
case 'get_parent_class':
case 'is_a':
case 'is_subclass_of':
case 'class_exists':
case 'class_implements':
case 'class_parents':
case 'trait_exists':
case 'defined':
case 'interface_exists':
case 'method_exists':
case 'property_exists':
case 'is_callable':
return;
}
}

throw $e;
}
}
0