Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cache:
- $HOME/.composer/cache

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

Expand Down
19 changes: 14 additions & 5 deletions src/Generator/AbstractClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,22 @@ protected function tokenizeUseStatements(array $useStatements, $prefix = '')
/**
* Generates classes files.
*
* @param array $configs raw configs
* @param string $outputDirectory
* @param bool $regenerateIfExists
* @param array $configs raw configs
* @param string $outputDirectory
* @param int|bool $mode
*
* @return array classes map [[FQCLN => classPath], [FQCLN => classPath], ...]
*/
abstract public function generateClasses(array $configs, $outputDirectory, $regenerateIfExists = false);
abstract public function generateClasses(array $configs, $outputDirectory, $mode = false);

abstract public function generateClass(array $config, $outputDirectory, $regenerateIfExists = false);
/**
* Generates a class file.
*
* @param array $config
* @param $outputDirectory
* @param bool $mode
*
* @return array classes map [FQCLN => classPath]
*/
abstract public function generateClass(array $config, $outputDirectory, $mode = false);
}
61 changes: 42 additions & 19 deletions src/Generator/AbstractTypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ abstract class AbstractTypeGenerator extends AbstractClassGenerator
{
const DEFAULT_CLASS_NAMESPACE = 'Overblog\\CG\\GraphQLGenerator\\__Schema__';

const MODE_DRY_RUN = 1;
const MODE_MAPPING_ONLY = 2;
const MODE_WRITE = 4;
const MODE_OVERRIDE = 8;

protected static $deferredPlaceHolders = ['useStatement', 'spaces', 'closureUseStatements'];

protected static $closureTemplate = <<<EOF
function (%s) <closureUseStatements>{
<spaces><spaces>return %s;
Expand Down Expand Up @@ -308,45 +315,61 @@ protected function arrayKeyExistsAndIsNotNull(array $value, $key)
* ]
* </code>
*
* @param array $configs
* @param string $outputDirectory
* @param bool $regenerateIfExists
* @param array $configs
* @param string $outputDirectory
* @param int|bool $mode
*
* @return array
*/
public function generateClasses(array $configs, $outputDirectory, $regenerateIfExists = false)
public function generateClasses(array $configs, $outputDirectory, $mode = false)
{
$classesMap = [];

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

$classesMap = array_merge($classesMap, $classMap);
}

return $classesMap;
}

public function generateClass(array $config, $outputDirectory, $regenerateIfExists = false)
/**
* @param array $config
* @param string $outputDirectory
* @param int|bool $mode true consider as MODE_WRITE|MODE_OVERRIDE and false as MODE_WRITE
*
* @return array
*/
public function generateClass(array $config, $outputDirectory, $mode = false)
{
static $treatLater = ['useStatement', 'spaces', 'closureUseStatements'];
$this->clearInternalUseStatements();
$code = $this->processTemplatePlaceHoldersReplacements('TypeSystem', $config, $treatLater);
$code = $this->processPlaceHoldersReplacements($treatLater, $code, $config) . "\n";
if (true === $mode) {
$mode = self::MODE_WRITE|self::MODE_OVERRIDE;
} elseif (false === $mode) {
$mode = self::MODE_WRITE;
}

$className = $this->generateClassName($config);

$path = $outputDirectory . '/' . $className . '.php';
$dir = dirname($path);

if (!is_dir($dir)) {
mkdir($dir, 0775, true);
}
if ($regenerateIfExists || !file_exists($path)) {
file_put_contents($path, $code);
chmod($path, 0664);
if (!($mode & self::MODE_MAPPING_ONLY)) {
$this->clearInternalUseStatements();
$code = $this->processTemplatePlaceHoldersReplacements('TypeSystem', $config, static::$deferredPlaceHolders);
$code = $this->processPlaceHoldersReplacements(static::$deferredPlaceHolders, $code, $config) . "\n";

if ($mode & self::MODE_WRITE) {
$dir = dirname($path);
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
}
if (($mode & self::MODE_OVERRIDE) || !file_exists($path)) {
file_put_contents($path, $code);
chmod($path, 0664);
}
}
}

return [$this->getClassNamespace().'\\'.$className => realpath($path)];
return [$this->getClassNamespace().'\\'.$className => $path];
}
}
13 changes: 13 additions & 0 deletions tests/AbstractStarWarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ protected function assertValidQuery($query, $expected, $variables = null)
$expected = ['data' => $expected];
$this->assertEquals($expected, $actual, json_encode($actual));
}

protected function sortSchemaEntry(array &$entries, $entryKey, $sortBy)
{
if (isset($entries['data']['__schema'][$entryKey])) {
$data = &$entries['data']['__schema'][$entryKey];
} else {
$data = &$entries['__schema'][$entryKey];
}

usort($data, function ($data1, $data2) use ($sortBy) {
return strcmp($data1[$sortBy], $data2[$sortBy]);
});
}
}
9 changes: 8 additions & 1 deletion tests/StarWarsIntrospectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Overblog\GraphQLGenerator\Tests;

use GraphQL\GraphQL;

class StarWarsIntrospectionTest extends AbstractStarWarsTest
{
// Star Wars Introspection Tests
Expand Down Expand Up @@ -53,7 +55,12 @@ public function testAllowsQueryingTheSchemaForTypes()
]
]
];
$this->assertValidQuery($query, $expected);

$actual = GraphQL::execute($this->schema, $query);
$this->sortSchemaEntry($actual, 'types', 'name');
$this->sortSchemaEntry($expected, 'types', 'name');
$expected = ['data' => $expected];
$this->assertEquals($expected, $actual, json_encode($actual));
}

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