Skip to content

Commit 33a8cbb

Browse files
authored
Merge pull request #49 from moufmouf/flex_route
Fixing loading of files with bad class names
2 parents 0a6fa6a + d221cb1 commit 33a8cbb

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

DependencyInjection/GraphqliteCompilerPass.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
use ReflectionParameter;
1919
use Symfony\Component\Cache\Simple\ApcuCache as SymfonyApcuCache;
2020
use Symfony\Component\Cache\Simple\PhpFilesCache as SymfonyPhpFilesCache;
21+
use function filter_var;
2122
use function function_exists;
2223
use GraphQL\Type\Definition\InputObjectType;
2324
use GraphQL\Type\Definition\ObjectType;
2425
use Psr\Container\ContainerInterface;
2526
use ReflectionClass;
2627
use ReflectionMethod;
28+
use function ini_get;
29+
use function interface_exists;
30+
use function php_sapi_name;
2731
use function str_replace;
2832
use function strpos;
2933
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
@@ -252,7 +256,7 @@ public function process(ContainerBuilder $container)
252256
$this->mapAdderToTag('graphql.type_mapper_factory', 'addTypeMapperFactory', $container, $schemaFactory);
253257

254258
// Configure cache
255-
if (ApcuAdapter::isSupported()) {
259+
if (ApcuAdapter::isSupported() && (PHP_SAPI !== 'cli' || filter_var(ini_get('apc.enabled_cli')))) {
256260
$container->setAlias('graphqlite.cache', 'graphqlite.apcucache');
257261
} else {
258262
$container->setAlias('graphqlite.cache', 'graphqlite.phpfilescache');
@@ -421,12 +425,21 @@ private function getCodeCache(): ClassBoundCacheContractInterface
421425
private function getClassList(string $namespace, int $globTtl = 2, bool $recursive = true): array
422426
{
423427
$explorer = new GlobClassExplorer($namespace, $this->getPsr16Cache(), $globTtl, ClassNameMapper::createFromComposerFile(null, null, true), $recursive);
424-
$allClasses = $explorer->getClasses();
428+
$allClasses = $explorer->getClassMap();
425429
$classes = [];
426-
foreach ($allClasses as $className) {
427-
if (! class_exists($className)) {
428-
continue;
430+
foreach ($allClasses as $className => $phpFile) {
431+
if (! class_exists($className, false)) {
432+
// Let's try to load the file if it was not imported yet.
433+
// We are importing the file manually to avoid triggering the autoloader.
434+
// The autoloader might trigger errors if the file does not respect PSR-4 or if the
435+
// Symfony DebugAutoLoader is installed. (see https://github.com/thecodingmachine/graphqlite/issues/216)
436+
require_once $phpFile;
437+
// Does it exists now?
438+
if (! class_exists($className, false)) {
439+
continue;
440+
}
429441
}
442+
430443
$refClass = new ReflectionClass($className);
431444
if (! $refClass->isInstantiable()) {
432445
continue;

Tests/Fixtures/Entities/BadClass.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
4+
// The namespace for this class is broken. It must not impact Symfony.
5+
class BadClass
6+
{
7+
8+
}

Tests/GraphqliteTestingKernel.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
6363
'secret' => 'S0ME_SECRET'
6464
);
6565

66+
$frameworkConf['cache'] =[
67+
'app' => 'cache.adapter.array',
68+
];
69+
6670
if ($this->enableSession) {
6771
$frameworkConf['session'] =[
6872
'enabled' => true,
@@ -139,6 +143,6 @@ protected function configureRoutes(RouteCollectionBuilder $routes)
139143

140144
public function getCacheDir()
141145
{
142-
return __DIR__.'/../cache/'.spl_object_hash($this);
146+
return __DIR__.'/../cache/'.($this->enableSession?'withSession':'withoutSession').$this->enableLogin.($this->enableSecurity?'withSecurity':'withoutSecurity').$this->enableMe;
143147
}
144148
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require" : {
1919
"php" : ">=7.2",
20-
"thecodingmachine/graphqlite" : "~4.0.0",
20+
"thecodingmachine/graphqlite" : "~4.0.1",
2121
"thecodingmachine/graphqlite-symfony-validator-bridge" : "~4.0.0",
2222
"symfony/framework-bundle": "^4.1.9 | ^5",
2323
"symfony/validator": "^4.1.9 | ^5",

0 commit comments

Comments
 (0)