|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AppBundle\Command; |
| 4 | + |
| 5 | +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
| 6 | +use Symfony\Component\Console\Input\InputArgument; |
| 7 | +use Symfony\Component\Console\Input\InputInterface; |
| 8 | +use Symfony\Component\Console\Input\InputOption; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 11 | + |
| 12 | +class GenerateSchemaCommand extends ContainerAwareCommand |
| 13 | +{ |
| 14 | + protected function configure() |
| 15 | + { |
| 16 | + $this |
| 17 | + ->setName('graphql:generate-schema') |
| 18 | + ->setDescription('Generates GraphQL Schema class') |
| 19 | + ->addArgument('bundle', InputArgument::REQUIRED, 'Bundle to generate class to'); |
| 20 | + } |
| 21 | + |
| 22 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 23 | + { |
| 24 | + $bundleName = $input->getArgument('bundle'); |
| 25 | + if (strpos($bundleName, -6) != 'Bundle') $bundleName .= 'Bundle'; |
| 26 | + |
| 27 | + $srcPath = realpath($this->getContainer()->getParameter('kernel.root_dir') . '/../src'); |
| 28 | + $activeBundles = $this->getContainer()->getParameter('kernel.bundles'); |
| 29 | + if (!array_key_exists($bundleName, $activeBundles)) { |
| 30 | + $output->writeln('There is no active bundleName: ' . $bundleName); |
| 31 | + } |
| 32 | + |
| 33 | + $graphqlPath = $srcPath . '/' . $bundleName . '/GraphQL'; |
| 34 | + $className = 'Schema'; |
| 35 | + $classPath = $graphqlPath . '/' . $className . '.php'; |
| 36 | + |
| 37 | + $inputHelper = $this->getHelper('question'); |
| 38 | + $question = new ConfirmationQuestion(sprintf('Confirm creating class at %s ?', $classPath), false); |
| 39 | + if (!$inputHelper->ask($input, $output, $question)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (!is_dir($graphqlPath)) { |
| 44 | + mkdir($graphqlPath, 0777, true); |
| 45 | + } |
| 46 | + file_put_contents($classPath, $this->getSchemaClassTemplate($bundleName, $className)); |
| 47 | + |
| 48 | + $output->writeln('Schema file has been created at'); |
| 49 | + $output->writeln($classPath . "\n"); |
| 50 | + $output->writeln('Update your app/config/config.yml with the parameter:'); |
| 51 | + $output->writeln('graph_ql:'); |
| 52 | + $output->writeln(sprintf(' schema_class: %s\GraphQL\%s', $bundleName, $className)); |
| 53 | + } |
| 54 | + |
| 55 | + protected function getSchemaClassTemplate($bundleName, $className = 'Schema') |
| 56 | + { |
| 57 | + $tpl = <<<TEXT |
| 58 | +<?php |
| 59 | +/** |
| 60 | + * This class was automatically generated by GraphQL Schema generator |
| 61 | + */ |
| 62 | +
|
| 63 | +namespace $bundleName\GraphQL; |
| 64 | +
|
| 65 | +use Youshido\GraphQL\AbstractSchema; |
| 66 | +use Youshido\GraphQL\Type\Config\Schema\SchemaConfig; |
| 67 | +use Youshido\GraphQL\Type\Scalar\StringType; |
| 68 | +
|
| 69 | +class $className extends AbstractSchema |
| 70 | +{ |
| 71 | + public function build(SchemaConfig \$config) |
| 72 | + { |
| 73 | + \$config->getQuery()->addFields([ |
| 74 | + 'hello' => [ |
| 75 | + 'type' => new StringType(), |
| 76 | + 'resolve' => function () { |
| 77 | + return 'world!'; |
| 78 | + } |
| 79 | + ] |
| 80 | + ]); |
| 81 | + } |
| 82 | +
|
| 83 | +} |
| 84 | +
|
| 85 | +TEXT; |
| 86 | + |
| 87 | + return $tpl; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments