Skip to content

Commit a133661

Browse files
author
Artem Bilenskiy
committed
sm4 support
1 parent 7c6e245 commit a133661

File tree

5 files changed

+82
-40
lines changed

5 files changed

+82
-40
lines changed

Command/GraphQLConfigureCommand.php

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,75 @@
55
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
66
use Symfony\Component\Config\Resource\DirectoryResource;
77
use Symfony\Component\Config\Resource\FileResource;
8-
use Symfony\Component\Console\Input\InputArgument;
98
use Symfony\Component\Console\Input\InputInterface;
10-
use Symfony\Component\Console\Input\InputOption;
119
use Symfony\Component\Console\Output\OutputInterface;
1210
use Symfony\Component\Console\Question\ConfirmationQuestion;
1311

1412
class GraphQLConfigureCommand extends ContainerAwareCommand
1513
{
14+
const PROJECT_NAMESPACE = 'App';
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
1619
protected function configure()
1720
{
1821
$this
1922
->setName('graphql:configure')
2023
->setDescription('Generates GraphQL Schema class')
21-
->addArgument('bundle', InputArgument::OPTIONAL, 'Bundle to generate class to', 'AppBundle')
2224
->addOption('composer');
2325
}
2426

27+
/**
28+
* {@inheritdoc}
29+
*/
2530
protected function execute(InputInterface $input, OutputInterface $output)
2631
{
27-
$bundleName = $input->getArgument('bundle');
2832
$isComposerCall = $input->getOption('composer');
29-
if (substr($bundleName, -6) != 'Bundle') $bundleName .= 'Bundle';
30-
31-
$container = $this->getContainer();
3233

33-
$rootDir = $container->getParameter('kernel.root_dir');
34-
$configFile = $rootDir . '/../app/config/config.yml';
35-
try {
36-
$bundle = $container->get('kernel')->getBundle($bundleName);
37-
} catch (\InvalidArgumentException $e) {
38-
$output->writeln('There is no active bundleName: ' . $bundleName);
39-
return;
40-
}
34+
$container = $this->getContainer();
35+
$rootDir = $container->getParameter('kernel.root_dir');
36+
$configFile = $rootDir . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config/packages/graphql.yml';
4137

42-
$className = 'Schema';
43-
$bundleNameSpace = $bundle->getNameSpace() . '\\GraphQL';
44-
$graphqlPath = $bundle->getPath() . '/GraphQL/';
45-
$classPath = $graphqlPath . '/' . $className . '.php';
38+
$className = 'Schema';
39+
$schemaNamespace = self::PROJECT_NAMESPACE . '\\GraphQL';
40+
$graphqlPath = rtrim($rootDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'GraphQL';
41+
$classPath = $graphqlPath . DIRECTORY_SEPARATOR . $className . '.php';
4642

4743
$inputHelper = $this->getHelper('question');
4844
if (file_exists($classPath)) {
4945
if (!$isComposerCall) {
50-
$output->writeln(sprintf('Schema class %s was found.', $bundleNameSpace.'\\'.$className));
46+
$output->writeln(sprintf('Schema class %s was found.', $schemaNamespace . '\\' . $className));
5147
}
5248
} else {
53-
$question = new ConfirmationQuestion(sprintf('Confirm creating class at %s ? [Y/n]', $bundleNameSpace.'\\'.$className), true);
49+
$question = new ConfirmationQuestion(sprintf('Confirm creating class at %s ? [Y/n]', $schemaNamespace . '\\' . $className), true);
5450
if (!$inputHelper->ask($input, $output, $question)) {
5551
return;
5652
}
5753

5854
if (!is_dir($graphqlPath)) {
5955
mkdir($graphqlPath, 0777, true);
6056
}
61-
file_put_contents($classPath, $this->getSchemaClassTemplate($bundleNameSpace, $className));
57+
file_put_contents($classPath, $this->getSchemaClassTemplate($schemaNamespace, $className));
6258

6359
$output->writeln('Schema file has been created at');
6460
$output->writeln($classPath . "\n");
6561

62+
if (!file_exists($configFile)) {
63+
$question = new ConfirmationQuestion(sprintf('Config file not found (look at %s). Create it? [Y/n]', $configFile), true);
64+
if (!$inputHelper->ask($input, $output, $question)) {
65+
return;
66+
}
67+
68+
touch($configFile);
69+
}
70+
6671
$originalConfigData = file_get_contents($configFile);
6772
if (strpos($originalConfigData, 'graphql') === false) {
68-
$configData = <<<CONFIG
73+
$projectNameSpace = self::PROJECT_NAMESPACE;
74+
$configData = <<<CONFIG
6975
graphql:
70-
schema_class: "{$bundleName}\\\\GraphQL\\\\{$className}"
71-
76+
schema_class: "{$projectNameSpace}\\\\GraphQL\\\\{$className}"
7277
7378
CONFIG;
7479
file_put_contents($configFile, $configData . $originalConfigData);
@@ -77,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7782
if (!$this->graphQLRouteExists()) {
7883
$question = new ConfirmationQuestion('Confirm adding GraphQL route? [Y/n]', true);
7984
$resource = $this->getMainRouteConfig();
80-
if ($resource && !$inputHelper->ask($input, $output, $question)) {
85+
if ($resource && $inputHelper->ask($input, $output, $question)) {
8186
$routeConfigData = <<<CONFIG
8287
8388
graphql:
@@ -93,25 +98,34 @@ protected function execute(InputInterface $input, OutputInterface $output)
9398
}
9499
}
95100

101+
/**
102+
* @return null|string
103+
*
104+
* @throws \Exception
105+
*/
96106
protected function getMainRouteConfig()
97107
{
98108
$routerResources = $this->getContainer()->get('router')->getRouteCollection()->getResources();
99109
foreach ($routerResources as $resource) {
100110
/** @var FileResource|DirectoryResource $resource */
101-
if (substr($resource->getResource(), -11) == 'routing.yml') {
111+
if (method_exists($resource, 'getResource') && substr($resource->getResource(), -11) == 'routes.yaml') {
102112
return $resource->getResource();
103113
}
104114
}
105115

106116
return null;
107117
}
108118

119+
/**
120+
* @return bool
121+
* @throws \Exception
122+
*/
109123
protected function graphQLRouteExists()
110124
{
111125
$routerResources = $this->getContainer()->get('router')->getRouteCollection()->getResources();
112126
foreach ($routerResources as $resource) {
113127
/** @var FileResource|DirectoryResource $resource */
114-
if (strpos($resource->getResource(), 'GraphQLController.php') !== false) {
128+
if (method_exists($resource, 'getResource') && strpos($resource->getResource(), 'GraphQLController.php') !== false) {
115129
return true;
116130
}
117131
}
@@ -124,15 +138,15 @@ protected function generateRoutes()
124138

125139
}
126140

127-
protected function getSchemaClassTemplate($bundleNameSpace, $className = 'Schema')
141+
protected function getSchemaClassTemplate($nameSpace, $className = 'Schema')
128142
{
129143
$tpl = <<<TEXT
130144
<?php
131145
/**
132146
* This class was automatically generated by GraphQL Schema generator
133147
*/
134148
135-
namespace $bundleNameSpace;
149+
namespace $nameSpace;
136150
137151
use Youshido\GraphQL\Schema\AbstractSchema;
138152
use Youshido\GraphQL\Config\Schema\SchemaConfig;
@@ -165,5 +179,4 @@ public function build(SchemaConfig \$config)
165179

166180
return $tpl;
167181
}
168-
169182
}

Controller/GraphQLController.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1212
use Symfony\Component\HttpFoundation\JsonResponse;
1313
use Symfony\Component\Routing\Annotation\Route;
14-
use Youshido\GraphQL\Validator\Exception\ConfigurationException;
14+
use Youshido\GraphQL\Exception\ConfigurationException;
1515
use Youshido\GraphQLBundle\Exception\UnableToInitializeSchemaServiceException;
1616
use Youshido\GraphQLBundle\Execution\Processor;
1717

@@ -20,7 +20,7 @@ class GraphQLController extends Controller
2020
/**
2121
* @Route("/graphql")
2222
*
23-
* @throws ConfigurationException
23+
* @throws \Exception
2424
*
2525
* @return JsonResponse
2626
*/
@@ -69,6 +69,11 @@ private function executeQuery($query, $variables)
6969
return $processor->getResponseData();
7070
}
7171

72+
/**
73+
* @return array
74+
*
75+
* @throws \Exception
76+
*/
7277
private function getPayload()
7378
{
7479
$request = $this->get('request_stack')->getCurrentRequest();
@@ -127,6 +132,9 @@ private function getPayload()
127132
return [$queries, $isMultiQueryRequest];
128133
}
129134

135+
/**
136+
* @throws \Exception
137+
*/
130138
private function initializeSchemaService()
131139
{
132140
if ($this->container->initialized('graphql.schema')) {
@@ -136,6 +144,11 @@ private function initializeSchemaService()
136144
$this->container->set('graphql.schema', $this->makeSchemaService());
137145
}
138146

147+
/**
148+
* @return object
149+
*
150+
* @throws \Exception
151+
*/
139152
private function makeSchemaService()
140153
{
141154
if ($this->container->has($this->getSchemaService())) {

Controller/GraphQLExplorerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class GraphQLExplorerController extends Controller
2222
*/
2323
public function explorerAction()
2424
{
25-
$response = $this->render('GraphQLBundle:Feature:explorer.html.twig', [
25+
$response = $this->render('@GraphQLBundle/Feature/explorer.html.twig', [
2626
'graphQLUrl' => $this->generateUrl('youshido_graphql_graphql_default'),
2727
'tokenHeader' => 'access-token'
2828
]);

DependencyInjection/Compiler/GraphQlCompilerPass.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
class GraphQlCompilerPass implements CompilerPassInterface
1717
{
18-
1918
/**
2019
* You can modify the container here before it is dumped to PHP code.
2120
*
2221
* @param ContainerBuilder $container
22+
*
23+
* @throws \Exception
2324
*/
2425
public function process(ContainerBuilder $container)
2526
{
@@ -42,6 +43,11 @@ public function process(ContainerBuilder $container)
4243
$this->processSecurityGuard($container);
4344
}
4445

46+
/**
47+
* @param ContainerBuilder $container
48+
*
49+
* @throws \Exception
50+
*/
4551
private function processSecurityGuard(ContainerBuilder $container)
4652
{
4753
$guardConfig = $container->getParameter('graphql.security.guard_config');
@@ -58,6 +64,13 @@ private function processSecurityGuard(ContainerBuilder $container)
5864
}
5965
}
6066

67+
/**
68+
* @param ContainerBuilder $container
69+
* @param $voterClass
70+
* @param array $list
71+
*
72+
* @throws \Exception
73+
*/
6174
private function addListVoter(ContainerBuilder $container, $voterClass, array $list)
6275
{
6376
if ($list) {
@@ -69,7 +82,7 @@ private function addListVoter(ContainerBuilder $container, $voterClass, array $l
6982

7083
$container->setParameter('graphql.security.guard_config', [
7184
'operation' => true,
72-
'field' => false
85+
'field' => false,
7386
]);
7487
}
7588
}

Resources/config/services.xml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
</parameters>
1212

1313
<services>
14-
<service id="graphql.schema" synthetic="true" />
14+
<service id="graphql.schema" synthetic="true" public="true"/>
1515

16-
<service id="graphql.processor" class="%graphql.processor.class%">
16+
<service id="youshido_graphql.command.graphql_configure_command" class="Youshido\GraphQLBundle\Command\GraphQLConfigureCommand">
17+
<tag name="console.command"/>
18+
</service>
19+
20+
<service id="graphql.processor" class="%graphql.processor.class%" public="true">
1721
<argument type="service" id="graphql.execution_context" />
1822
<argument type="service" id="graphql.event_dispatcher" />
1923

@@ -22,7 +26,7 @@
2226
</call>
2327
</service>
2428

25-
<service id="graphql.event_dispatcher" class="Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher">
29+
<service id="graphql.event_dispatcher" class="Symfony\Component\EventDispatcher\EventDispatcher">
2630
<argument type="service" id="service_container" />
2731
</service>
2832

@@ -40,7 +44,6 @@
4044
</call>
4145
</service>
4246

43-
4447
<service id="graphql.security_manager" class="%graphql.security_manager.class%" lazy="true">
4548
<argument type="service" id="security.authorization_checker"/>
4649
<argument>%graphql.security.guard_config%</argument>

0 commit comments

Comments
 (0)