Skip to content

Commit 64486c7

Browse files
authored
Merge pull request #198 from mcg-web/optimizations
Allow using project composer Classloader and some more optimizations
2 parents 3bb9ff9 + 4d1fb07 commit 64486c7

File tree

40 files changed

+238
-213
lines changed

40 files changed

+238
-213
lines changed

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ matrix:
1717
- php: 7.0
1818
env: SYMFONY_VERSION=3.2.*
1919
- php: 7.1
20-
env: SYMFONY_VERSION=3.3.* SCRUTINIZER=true
20+
env: SYMFONY_VERSION=3.3.* TEST_COVERAGE=true
2121
- php: 7.1
2222
env: GRAPHQLPHP_VERSION=0.10.0
2323
- php: hhvm
@@ -31,17 +31,17 @@ cache:
3131
- $HOME/.php_cs.cache
3232

3333
before_install:
34-
- if [[ ${SCRUTINIZER} != true && ${TRAVIS_PHP_VERSION} != "hhvm" ]]; then phpenv config-rm xdebug.ini || true; fi
34+
- if [ ${TEST_COVERAGE} != true ]; then phpenv config-rm xdebug.ini || true; fi
3535
- composer selfupdate
3636
- if [ $SYMFONY_VERSION ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" "symfony/framework-bundle:${SYMFONY_VERSION}" --dev --no-update; fi;
3737
- if [ $GRAPHQLPHP_VERSION ]; then composer require "webonyx/graphql-php:${GRAPHQLPHP_VERSION}" --dev --no-update; fi;
3838

3939
install: composer update --prefer-source --no-interaction --optimize-autoloader
4040

4141
script:
42-
- bin/phpunit --debug $( if [ $SCRUTINIZER = true ]; then echo "-d xdebug.max_nesting_level=1000 --coverage-clover=build/logs/clover.xml"; fi; )
42+
- bin/phpunit --debug $( if [ $TEST_COVERAGE = true ]; then echo "-d xdebug.max_nesting_level=1000 --coverage-clover=build/logs/clover.xml"; fi; )
4343
- if [ ${TRAVIS_PHP_VERSION} == "7.0" ]; then bin/php-cs-fixer fix --diff --dry-run -v; fi;
4444

4545
after_script:
46-
- if [ ${SCRUTINIZER} = true ]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml; fi
47-
- if [ ${SCRUTINIZER} = true ]; then composer require "satooshi/php-coveralls:^1.0" && travis_retry php bin/coveralls -v; fi
46+
- if [ ${TEST_COVERAGE} = true ]; then wget https://scrutinizer-ci.com/ocular.phar && travis_retry php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml; fi
47+
- if [ ${TEST_COVERAGE} = true ]; then composer require "satooshi/php-coveralls:^1.0" && travis_retry php bin/coveralls -v; fi

CONTRIBUTING.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Contributing
2+
============
3+
4+
Running tests
5+
--------------
6+
7+
Install [phpunit](https://phpunit.de/manual/current/en/installation.html).
8+
9+
In the bundle directory:
10+
11+
```bash
12+
bin/phpunit
13+
```
14+
15+
Giving some love to PHP CS
16+
---------------------------
17+
18+
```bash
19+
bin/php-cs-fixer fix ./
20+
```

Config/Parser/YmlParser.php renamed to Config/Parser/YamlParser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1717
use Symfony\Component\Finder\SplFileInfo;
1818
use Symfony\Component\Yaml\Exception\ParseException;
19-
use Symfony\Component\Yaml\Parser as YamlParser;
19+
use Symfony\Component\Yaml\Parser;
2020

21-
class YmlParser implements ParserInterface
21+
class YamlParser implements ParserInterface
2222
{
2323
private static $yamlParser;
2424

@@ -31,7 +31,7 @@ class YmlParser implements ParserInterface
3131
public static function parse(SplFileInfo $file, ContainerBuilder $container)
3232
{
3333
if (null === self::$yamlParser) {
34-
self::$yamlParser = new YamlParser();
34+
self::$yamlParser = new Parser();
3535
}
3636

3737
try {

DependencyInjection/Compiler/ConfigTypesPass.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ class ConfigTypesPass implements CompilerPassInterface
2121
public function process(ContainerBuilder $container)
2222
{
2323
$config = $container->getParameter('overblog_graphql_types.config');
24-
$generatedClasses = $container->get('overblog_graphql.cache_compiler')->compile($this->processConfig($config));
24+
$generatedClasses = $container->get('overblog_graphql.cache_compiler')->compile(
25+
$this->processConfig($config),
26+
$container->getParameter('overblog_graphql.use_classloader_listener')
27+
);
2528

2629
foreach ($generatedClasses as $class => $file) {
30+
if (!class_exists($class)) {
31+
throw new \RuntimeException(sprintf(
32+
'Type class %s not found. If you are using your own classLoader verify the path and the namespace please.',
33+
json_encode($class))
34+
);
35+
}
2736
$aliases = call_user_func($class.'::getAliases');
2837
$this->setTypeServiceDefinition($container, $class, $aliases);
2938
}
39+
$container->getParameterBag()->remove('overblog_graphql_types.config');
3040
}
3141

3242
private function setTypeServiceDefinition(ContainerBuilder $container, $class, array $aliases)

DependencyInjection/Compiler/MutationTaggedServiceMappingTaggedPass.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ protected function getTagName()
1818
return 'overblog_graphql.mutation';
1919
}
2020

21-
protected function getParameterName()
22-
{
23-
return 'overblog_graphql.mutations_mapping';
24-
}
25-
2621
protected function getResolverServiceID()
2722
{
2823
return 'overblog_graphql.mutation_resolver';

DependencyInjection/Compiler/ResolverTaggedServiceMappingPass.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ protected function getTagName()
1818
return 'overblog_graphql.resolver';
1919
}
2020

21-
protected function getParameterName()
22-
{
23-
return 'overblog_graphql.resolvers_mapping';
24-
}
25-
2621
protected function checkRequirements($id, array $tag)
2722
{
2823
parent::checkRequirements($id, $tag);

DependencyInjection/Compiler/TaggedServiceMappingPass.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ private function getTaggedServiceMapping(ContainerBuilder $container, $tagName)
5454
public function process(ContainerBuilder $container)
5555
{
5656
$mapping = $this->getTaggedServiceMapping($container, $this->getTagName());
57-
$container->setParameter($this->getParameterName(), $mapping);
5857
$resolverDefinition = $container->findDefinition($this->getResolverServiceID());
5958

6059
foreach ($mapping as $name => $options) {
@@ -101,6 +100,4 @@ protected function checkRequirements($id, array $tag)
101100
abstract protected function getTagName();
102101

103102
abstract protected function getResolverServiceID();
104-
105-
abstract protected function getParameterName();
106103
}

DependencyInjection/Compiler/TypeTaggedServiceMappingPass.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ protected function getTagName()
1818
return 'overblog_graphql.type';
1919
}
2020

21-
protected function getParameterName()
22-
{
23-
return 'overblog_graphql.types_mapping';
24-
}
25-
2621
protected function getResolverServiceID()
2722
{
2823
return 'overblog_graphql.type_resolver';

DependencyInjection/Configuration.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,26 @@
1414
use GraphQL\Validator\Rules\QueryComplexity;
1515
use GraphQL\Validator\Rules\QueryDepth;
1616
use Overblog\GraphQLBundle\Error\ErrorHandler;
17+
use Overblog\GraphQLBundle\Resolver\Resolver;
1718
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1819
use Symfony\Component\Config\Definition\ConfigurationInterface;
1920

2021
class Configuration implements ConfigurationInterface
2122
{
2223
private $debug;
2324

25+
private $cacheDir;
26+
2427
/**
2528
* Constructor.
2629
*
27-
* @param bool $debug Whether to use the debug mode
30+
* @param bool $debug Whether to use the debug mode
31+
* @param null|string $cacheDir
2832
*/
29-
public function __construct($debug)
33+
public function __construct($debug, $cacheDir = null)
3034
{
3135
$this->debug = (bool) $debug;
36+
$this->cacheDir = $cacheDir;
3237
}
3338

3439
public function getConfigTreeBuilder()
@@ -46,6 +51,10 @@ public function getConfigTreeBuilder()
4651
->addDefaultsIfNotSet()
4752
->children()
4853
->scalarNode('internal_error_message')->defaultNull()->end()
54+
->variableNode('default_resolver')->defaultValue([Resolver::class, 'defaultResolveFn'])->end()
55+
->scalarNode('class_namespace')->defaultValue('Overblog\\GraphQLBundle\\__DEFINITIONS__')->end()
56+
->scalarNode('cache_dir')->defaultValue($this->cacheDir.'/overblog/graphql-bundle/__definitions__')->end()
57+
->booleanNode('use_classloader_listener')->defaultTrue()->end()
4958
->booleanNode('show_debug_info')->defaultFalse()->end()
5059
->booleanNode('config_validation')->defaultValue($this->debug)->end()
5160
->arrayNode('schema')
@@ -89,8 +98,18 @@ public function getConfigTreeBuilder()
8998
->arrayNode('types')
9099
->prototype('array')
91100
->addDefaultsIfNotSet()
101+
->beforeNormalization()
102+
->ifTrue(function ($v) {
103+
return isset($v['type']) && $v['type'] === 'yml';
104+
})
105+
->then(function ($v) {
106+
$v['type'] = 'yaml';
107+
108+
return $v;
109+
})
110+
->end()
92111
->children()
93-
->enumNode('type')->isRequired()->values(['yml', 'xml'])->end()
112+
->enumNode('type')->isRequired()->values(['yaml', 'xml'])->end()
94113
->scalarNode('dir')->defaultNull()->end()
95114
->end()
96115
->end()
@@ -167,8 +186,8 @@ public function getConfigTreeBuilder()
167186
->arrayNode('versions')
168187
->addDefaultsIfNotSet()
169188
->children()
170-
->scalarNode('graphiql')->defaultValue('0.9')->end()
171-
->scalarNode('react')->defaultValue('15.4')->end()
189+
->scalarNode('graphiql')->defaultValue('0.11')->end()
190+
->scalarNode('react')->defaultValue('15.6')->end()
172191
->scalarNode('fetch')->defaultValue('2.0')->end()
173192
->enumNode('relay')->values(['modern', 'classic'])->defaultValue('classic')->end()
174193
->end()

DependencyInjection/OverblogGraphQLExtension.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use GraphQL\Type\Schema;
1515
use Overblog\GraphQLBundle\Config\TypeWithOutputFieldsDefinition;
16+
use Overblog\GraphQLBundle\EventListener\ClassLoaderListener;
1617
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1718
use Symfony\Component\Config\FileLocator;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -45,7 +46,8 @@ public function load(array $configs, ContainerBuilder $container)
4546
$this->setConfigBuilders($config, $container);
4647
$this->setVersions($config, $container);
4748
$this->setShowDebug($config, $container);
48-
$this->setAutoMappingParameters($config, $container);
49+
$this->setDefinitionParameters($config, $container);
50+
$this->setClassLoaderListener($config, $container);
4951

5052
$container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources'));
5153
}
@@ -68,13 +70,35 @@ public function getAlias()
6870

6971
public function getConfiguration(array $config, ContainerBuilder $container)
7072
{
71-
return new Configuration($container->getParameter('kernel.debug'));
73+
return new Configuration(
74+
$container->getParameter('kernel.debug'),
75+
$container->hasParameter('kernel.cache_dir') ? $container->getParameter('kernel.cache_dir') : null
76+
);
7277
}
7378

74-
private function setAutoMappingParameters(array $config, ContainerBuilder $container)
79+
private function setClassLoaderListener(array $config, ContainerBuilder $container)
7580
{
81+
$container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']);
82+
if ($config['definitions']['use_classloader_listener']) {
83+
$definition = $container->setDefinition(
84+
$this->getAlias().'.event_listener.classloader_listener',
85+
new Definition(ClassLoaderListener::class)
86+
);
87+
$definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]);
88+
$definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]);
89+
$definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]);
90+
}
91+
}
92+
93+
private function setDefinitionParameters(array $config, ContainerBuilder $container)
94+
{
95+
// auto mapping
7696
$container->setParameter($this->getAlias().'.auto_mapping.enabled', $config['definitions']['auto_mapping']['enabled']);
7797
$container->setParameter($this->getAlias().'.auto_mapping.directories', $config['definitions']['auto_mapping']['directories']);
98+
// generator and config
99+
$container->setParameter($this->getAlias().'.default_resolver', $config['definitions']['default_resolver']);
100+
$container->setParameter($this->getAlias().'.class_namespace', $config['definitions']['class_namespace']);
101+
$container->setParameter($this->getAlias().'.cache_dir', $config['definitions']['cache_dir']);
78102
}
79103

80104
private function setBatchingMethod(array $config, ContainerBuilder $container)

0 commit comments

Comments
 (0)