Skip to content

Commit a79a7e2

Browse files
authored
Merge pull request #293 from mcg-web/commands_optimizations
Commands fixes and optimizations
2 parents d005cb4 + a7a5a13 commit a79a7e2

17 files changed

+186
-176
lines changed

Command/CompileCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Symfony\Component\Console\Output\OutputInterface;
1010
use Symfony\Component\Console\Style\SymfonyStyle;
1111

12-
class CompileCommand extends Command
12+
final class CompileCommand extends Command
1313
{
1414
private $typeGenerator;
1515

Command/DebugCommand.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ protected function configure()
5353
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
5454
sprintf('filter by a category (%s).', implode(', ', self::$categories))
5555
)
56+
->addOption(
57+
'with-service-id',
58+
null,
59+
InputOption::VALUE_NONE,
60+
'also display service id'
61+
)
5662
->setDescription('Display current GraphQL services (types, resolvers and mutations)');
5763
}
5864

@@ -66,28 +72,41 @@ protected function execute(InputInterface $input, OutputInterface $output)
6672
}
6773

6874
$categories = empty($categoriesOption) ? self::$categories : $categoriesOption;
75+
$withServiceId = $input->getOption('with-service-id');
6976

7077
$io = new SymfonyStyle($input, $output);
71-
$tableHeaders = ['id', 'aliases'];
78+
$tableHeaders = ['solution id', 'aliases'];
79+
if ($withServiceId) {
80+
$tableHeaders[] = 'service id';
81+
}
82+
7283
foreach ($categories as $category) {
7384
$io->title(sprintf('GraphQL %ss Services', ucfirst($category)));
7485

7586
/** @var FluentResolverInterface $resolver */
7687
$resolver = $this->{$category.'Resolver'};
77-
$this->renderTable($resolver, $tableHeaders, $io);
88+
$this->renderTable($resolver, $tableHeaders, $io, $withServiceId);
7889
}
7990
}
8091

81-
private function renderTable(FluentResolverInterface $resolver, array $tableHeaders, SymfonyStyle $io)
92+
/**
93+
* @param FluentResolverInterface $resolver
94+
* @param array $tableHeaders
95+
* @param SymfonyStyle $io
96+
* @param bool $withServiceId
97+
*/
98+
private function renderTable(FluentResolverInterface $resolver, array $tableHeaders, SymfonyStyle $io, $withServiceId)
8299
{
83100
$tableRows = [];
84101
$solutionIDs = array_keys($resolver->getSolutions());
85102
sort($solutionIDs);
86103
foreach ($solutionIDs as $solutionID) {
87104
$aliases = $resolver->getSolutionAliases($solutionID);
88-
$aliases[] = $solutionID;
89105
$options = $resolver->getSolutionOptions($solutionID);
90-
$tableRows[$options['id']] = [$options['id'], self::serializeAliases($aliases, $options)];
106+
$tableRows[$solutionID] = [$solutionID, self::serializeAliases($aliases, $options)];
107+
if ($withServiceId) {
108+
$tableRows[$solutionID][] = $options['id'];
109+
}
91110
}
92111
ksort($tableRows);
93112
$io->table($tableHeaders, $tableRows);

Command/GraphQLDumpSchemaCommand.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,22 @@
44

55
use GraphQL\Type\Introspection;
66
use GraphQL\Utils\SchemaPrinter;
7-
use Overblog\GraphQLBundle\Request\Executor as RequestExecutor;
87
use Symfony\Component\Console\Command\Command;
98
use Symfony\Component\Console\Input\InputInterface;
109
use Symfony\Component\Console\Input\InputOption;
1110
use Symfony\Component\Console\Output\OutputInterface;
1211
use Symfony\Component\Console\Style\SymfonyStyle;
13-
use Symfony\Component\DependencyInjection\ContainerInterface;
1412

1513
final class GraphQLDumpSchemaCommand extends Command
1614
{
17-
/** @var ContainerInterface */
18-
private $container;
15+
use RequestExecutorLazyLoaderTrait;
1916

2017
/** @var string */
2118
private $baseExportPath;
2219

23-
public function __construct(
24-
ContainerInterface $container,
25-
$baseExportPath
26-
) {
20+
public function __construct($baseExportPath)
21+
{
2722
parent::__construct();
28-
$this->container = $container;
2923
$this->baseExportPath = $baseExportPath;
3024
}
3125

@@ -123,12 +117,4 @@ private function useModernJsonFormat(InputInterface $input)
123117

124118
return true === $modern;
125119
}
126-
127-
/**
128-
* @return RequestExecutor
129-
*/
130-
private function getRequestExecutor()
131-
{
132-
return $this->container->get('overblog_graphql.request_executor');
133-
}
134120
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Overblog\GraphQLBundle\Command;
4+
5+
use Overblog\GraphQLBundle\Request\Executor as RequestExecutor;
6+
7+
trait RequestExecutorLazyLoaderTrait
8+
{
9+
/** @var RequestExecutor */
10+
private $requestExecutor;
11+
12+
/** @var array */
13+
private $requestExecutorFactory;
14+
15+
public function setRequestExecutorFactory(array $requestExecutorFactory)
16+
{
17+
$this->requestExecutorFactory = $requestExecutorFactory;
18+
}
19+
20+
/**
21+
* @return RequestExecutor
22+
*/
23+
protected function getRequestExecutor()
24+
{
25+
if (null === $this->requestExecutor && null !== $this->requestExecutorFactory) {
26+
$this->requestExecutor = call_user_func_array(...$this->requestExecutorFactory);
27+
}
28+
29+
return $this->requestExecutor;
30+
}
31+
}

Request/Executor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Executor
2323
const PROMISE_ADAPTER_SERVICE_ID = 'overblog_graphql.promise_adapter';
2424

2525
/** @var Schema[] */
26-
private $schemas;
26+
private $schemas = [];
2727

2828
/** @var EventDispatcherInterface|null */
2929
private $dispatcher;

Resources/config/services.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,12 @@ services:
126126
class: Overblog\GraphQLBundle\Command\GraphQLDumpSchemaCommand
127127
public: true
128128
arguments:
129+
- "%kernel.root_dir%"
130+
calls:
129131
# "overblog_graphql.request_executor" service must be load lazy since that command service
130132
# is instanced before ClassLoaderEvent. This issues is fix in Symfony 3.4 introducing lazy commands
131133
# see https://symfony.com/blog/new-in-symfony-3-4-lazy-commands
132-
- '@service_container'
133-
- "%kernel.root_dir%"
134+
- ['setRequestExecutorFactory', [[['@service_container', 'get'], ['overblog_graphql.request_executor']]]]
134135
tags:
135136
- { name: console.command }
136137

Tests/Functional/Command/DebugCommandTest.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@ public function setUp()
2626
$this->command = static::$kernel->getContainer()->get('overblog_graphql.command.debug');
2727
$this->commandTester = new CommandTester($this->command);
2828

29-
foreach (DebugCommand::getCategories() as $category) {
30-
$this->logs[$category] = trim(
31-
file_get_contents(
32-
sprintf(
33-
__DIR__.'/fixtures/%sdebug-%s.txt',
34-
version_compare(Kernel::VERSION, '3.3.0') >= 0 ? 'case-sensitive/' : '',
35-
$category
36-
)
29+
$categories = DebugCommand::getCategories();
30+
$categories[] = 'all';
31+
32+
foreach ($categories as $category) {
33+
$content = file_get_contents(
34+
sprintf(
35+
__DIR__.'/fixtures/debug/debug-%s.txt',
36+
$category
3737
)
3838
);
39+
40+
$this->logs[$category] = 'all' === $category ? $content : trim($content);
3941
}
4042
}
4143

@@ -60,6 +62,17 @@ public function testProcess(array $categories)
6062
$this->assertEquals($expected, $this->commandTester->getDisplay());
6163
}
6264

65+
public function testProcessWithServiceId()
66+
{
67+
if (version_compare(Kernel::VERSION, '3.3.0') < 0) {
68+
$this->markTestSkipped('Test only for Symfony >= 3.3.0.');
69+
}
70+
71+
$this->commandTester->execute(['--with-service-id' => null]);
72+
$this->assertEquals(0, $this->commandTester->getStatusCode());
73+
$this->assertEquals($this->logs['all'], $this->commandTester->getDisplay());
74+
}
75+
6376
/**
6477
* @expectedException \InvalidArgumentException
6578
* @expectedExceptionMessage Invalid category (fake)

Tests/Functional/Command/fixtures/case-sensitive/debug-mutation.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

Tests/Functional/Command/fixtures/case-sensitive/debug-resolver.txt

Lines changed: 0 additions & 19 deletions
This file was deleted.

Tests/Functional/Command/fixtures/case-sensitive/debug-type.txt

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)