Skip to content

Commit 7ee5852

Browse files
authored
Merge pull request #17 from mcg-web/introduce_dry_run_mode
Introduce differents generation mode
2 parents bca60ad + d6be529 commit 7ee5852

File tree

5 files changed

+78
-26
lines changed

5 files changed

+78
-26
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ cache:
2727
- $HOME/.composer/cache
2828

2929
before_install:
30-
- if [[ "$TRAVIS_PHP_VERSION" != "7.1" && "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini; fi
30+
- if [[ "$TRAVIS_PHP_VERSION" != "7.1" && "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini || true; fi
3131
- composer selfupdate
3232
- if [ "$GRAPHQLPHP_VERSION" != "" ]; then composer require "webonyx/graphql-php:${GRAPHQLPHP_VERSION}" --dev --no-update; fi;
3333

src/Generator/AbstractClassGenerator.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,22 @@ protected function tokenizeUseStatements(array $useStatements, $prefix = '')
375375
/**
376376
* Generates classes files.
377377
*
378-
* @param array $configs raw configs
379-
* @param string $outputDirectory
380-
* @param bool $regenerateIfExists
378+
* @param array $configs raw configs
379+
* @param string $outputDirectory
380+
* @param int|bool $mode
381381
*
382382
* @return array classes map [[FQCLN => classPath], [FQCLN => classPath], ...]
383383
*/
384-
abstract public function generateClasses(array $configs, $outputDirectory, $regenerateIfExists = false);
384+
abstract public function generateClasses(array $configs, $outputDirectory, $mode = false);
385385

386-
abstract public function generateClass(array $config, $outputDirectory, $regenerateIfExists = false);
386+
/**
387+
* Generates a class file.
388+
*
389+
* @param array $config
390+
* @param $outputDirectory
391+
* @param bool $mode
392+
*
393+
* @return array classes map [FQCLN => classPath]
394+
*/
395+
abstract public function generateClass(array $config, $outputDirectory, $mode = false);
387396
}

src/Generator/AbstractTypeGenerator.php

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ abstract class AbstractTypeGenerator extends AbstractClassGenerator
1919
{
2020
const DEFAULT_CLASS_NAMESPACE = 'Overblog\\CG\\GraphQLGenerator\\__Schema__';
2121

22+
const MODE_DRY_RUN = 1;
23+
const MODE_MAPPING_ONLY = 2;
24+
const MODE_WRITE = 4;
25+
const MODE_OVERRIDE = 8;
26+
27+
protected static $deferredPlaceHolders = ['useStatement', 'spaces', 'closureUseStatements'];
28+
2229
protected static $closureTemplate = <<<EOF
2330
function (%s) <closureUseStatements>{
2431
<spaces><spaces>return %s;
@@ -308,45 +315,61 @@ protected function arrayKeyExistsAndIsNotNull(array $value, $key)
308315
* ]
309316
* </code>
310317
*
311-
* @param array $configs
312-
* @param string $outputDirectory
313-
* @param bool $regenerateIfExists
318+
* @param array $configs
319+
* @param string $outputDirectory
320+
* @param int|bool $mode
321+
*
314322
* @return array
315323
*/
316-
public function generateClasses(array $configs, $outputDirectory, $regenerateIfExists = false)
324+
public function generateClasses(array $configs, $outputDirectory, $mode = false)
317325
{
318326
$classesMap = [];
319327

320328
foreach ($configs as $name => $config) {
321329
$config['config']['name'] = isset($config['config']['name']) ? $config['config']['name'] : $name;
322-
$classMap = $this->generateClass($config, $outputDirectory, $regenerateIfExists);
330+
$classMap = $this->generateClass($config, $outputDirectory, $mode);
323331

324332
$classesMap = array_merge($classesMap, $classMap);
325333
}
326334

327335
return $classesMap;
328336
}
329337

330-
public function generateClass(array $config, $outputDirectory, $regenerateIfExists = false)
338+
/**
339+
* @param array $config
340+
* @param string $outputDirectory
341+
* @param int|bool $mode true consider as MODE_WRITE|MODE_OVERRIDE and false as MODE_WRITE
342+
*
343+
* @return array
344+
*/
345+
public function generateClass(array $config, $outputDirectory, $mode = false)
331346
{
332-
static $treatLater = ['useStatement', 'spaces', 'closureUseStatements'];
333-
$this->clearInternalUseStatements();
334-
$code = $this->processTemplatePlaceHoldersReplacements('TypeSystem', $config, $treatLater);
335-
$code = $this->processPlaceHoldersReplacements($treatLater, $code, $config) . "\n";
347+
if (true === $mode) {
348+
$mode = self::MODE_WRITE|self::MODE_OVERRIDE;
349+
} elseif (false === $mode) {
350+
$mode = self::MODE_WRITE;
351+
}
336352

337353
$className = $this->generateClassName($config);
338-
339354
$path = $outputDirectory . '/' . $className . '.php';
340-
$dir = dirname($path);
341355

342-
if (!is_dir($dir)) {
343-
mkdir($dir, 0775, true);
344-
}
345-
if ($regenerateIfExists || !file_exists($path)) {
346-
file_put_contents($path, $code);
347-
chmod($path, 0664);
356+
if (!($mode & self::MODE_MAPPING_ONLY)) {
357+
$this->clearInternalUseStatements();
358+
$code = $this->processTemplatePlaceHoldersReplacements('TypeSystem', $config, static::$deferredPlaceHolders);
359+
$code = $this->processPlaceHoldersReplacements(static::$deferredPlaceHolders, $code, $config) . "\n";
360+
361+
if ($mode & self::MODE_WRITE) {
362+
$dir = dirname($path);
363+
if (!is_dir($dir)) {
364+
mkdir($dir, 0775, true);
365+
}
366+
if (($mode & self::MODE_OVERRIDE) || !file_exists($path)) {
367+
file_put_contents($path, $code);
368+
chmod($path, 0664);
369+
}
370+
}
348371
}
349372

350-
return [$this->getClassNamespace().'\\'.$className => realpath($path)];
373+
return [$this->getClassNamespace().'\\'.$className => $path];
351374
}
352375
}

tests/AbstractStarWarsTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,17 @@ protected function assertValidQuery($query, $expected, $variables = null)
4949
$expected = ['data' => $expected];
5050
$this->assertEquals($expected, $actual, json_encode($actual));
5151
}
52+
53+
protected function sortSchemaEntry(array &$entries, $entryKey, $sortBy)
54+
{
55+
if (isset($entries['data']['__schema'][$entryKey])) {
56+
$data = &$entries['data']['__schema'][$entryKey];
57+
} else {
58+
$data = &$entries['__schema'][$entryKey];
59+
}
60+
61+
usort($data, function ($data1, $data2) use ($sortBy) {
62+
return strcmp($data1[$sortBy], $data2[$sortBy]);
63+
});
64+
}
5265
}

tests/StarWarsIntrospectionTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Overblog\GraphQLGenerator\Tests;
1313

14+
use GraphQL\GraphQL;
15+
1416
class StarWarsIntrospectionTest extends AbstractStarWarsTest
1517
{
1618
// Star Wars Introspection Tests
@@ -53,7 +55,12 @@ public function testAllowsQueryingTheSchemaForTypes()
5355
]
5456
]
5557
];
56-
$this->assertValidQuery($query, $expected);
58+
59+
$actual = GraphQL::execute($this->schema, $query);
60+
$this->sortSchemaEntry($actual, 'types', 'name');
61+
$this->sortSchemaEntry($expected, 'types', 'name');
62+
$expected = ['data' => $expected];
63+
$this->assertEquals($expected, $actual, json_encode($actual));
5764
}
5865

5966
// it('Allows querying the schema for query type')

0 commit comments

Comments
 (0)