Skip to content

Commit 5395d91

Browse files
pierlonMikk Mihkel Nurges
authored andcommitted
Add Lumen support (#188)
* Remove Laravel framework dependency * Add function to detect Lumen framework * Add Lumen service provider * Rewrite routes for Lumen compatibility * Refactor controller to make it Lumen compatible * Make regex routes compatible for Lumen * Update test * Add documentation for Lumen configuration * Always autoload helper functions
1 parent 947afe4 commit 5395d91

File tree

9 files changed

+836
-644
lines changed

9 files changed

+836
-644
lines changed

Readme.md

Lines changed: 561 additions & 534 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,64 @@
1-
{
2-
"name": "rebing/graphql-laravel",
3-
"description": "Laravel wrapper for PHP GraphQL",
4-
"keywords": ["framework", "laravel", "graphql", "react"],
5-
"authors": [
6-
{
7-
"name": "Rebing OÜ",
8-
"homepage": "http://www.rebing.ee",
9-
"role": "Company"
10-
},
11-
{
12-
"name": "Mikk Mihkel Nurges",
13-
"email": "mikk.nurges@rebing.ee",
14-
"role": "Developer"
15-
},
16-
{
17-
"name": "Folklore",
18-
"email": "info@atelierfolklore.ca",
19-
"homepage": "http://atelierfolklore.ca"
20-
},
21-
{
22-
"name": "David Mongeau-Petitpas",
23-
"email": "dmp@atelierfolklore.ca",
24-
"homepage": "http://mongo.ca",
25-
"role": "Developer"
26-
}
27-
],
28-
"license": "MIT",
29-
"type": "project",
30-
"require": {
31-
"php": "^7.1",
32-
"illuminate/support": "5.5.*|5.6.*|5.7.*",
33-
"webonyx/graphql-php": "^0.13",
34-
"laravel/framework": "5.5.*|5.6.*|5.7.*"
35-
},
36-
"require-dev": {
37-
"orchestra/testbench": "3.5.*|3.6.*|3.7.*",
38-
"phpunit/phpunit": "^5.5|~6.0|~7.0"
39-
},
40-
"autoload": {
41-
"psr-0": {
42-
"Rebing\\GraphQL\\": "src/"
43-
}
44-
},
45-
"autoload-dev": {
46-
"classmap": [
47-
"tests/"
48-
]
49-
},
50-
"extra": {
51-
"laravel": {
52-
"providers": [
53-
"Rebing\\GraphQL\\GraphQLServiceProvider"
54-
],
55-
"aliases": {
56-
"GraphQL": "Rebing\\GraphQL\\Support\\Facades\\GraphQL"
57-
}
58-
}
59-
},
60-
"minimum-stability": "dev",
61-
"prefer-stable": true
62-
}
1+
{
2+
"name": "rebing/graphql-laravel",
3+
"description": "Laravel wrapper for PHP GraphQL",
4+
"keywords": ["framework", "laravel", "graphql", "react"],
5+
"authors": [
6+
{
7+
"name": "Rebing OÜ",
8+
"homepage": "http://www.rebing.ee",
9+
"role": "Company"
10+
},
11+
{
12+
"name": "Mikk Mihkel Nurges",
13+
"email": "mikk.nurges@rebing.ee",
14+
"role": "Developer"
15+
},
16+
{
17+
"name": "Folklore",
18+
"email": "info@atelierfolklore.ca",
19+
"homepage": "http://atelierfolklore.ca"
20+
},
21+
{
22+
"name": "David Mongeau-Petitpas",
23+
"email": "dmp@atelierfolklore.ca",
24+
"homepage": "http://mongo.ca",
25+
"role": "Developer"
26+
}
27+
],
28+
"license": "MIT",
29+
"type": "project",
30+
"require": {
31+
"php": "^7.1",
32+
"illuminate/support": "5.5.*|5.6.*|5.7.*",
33+
"webonyx/graphql-php": "^0.13"
34+
},
35+
"require-dev": {
36+
"orchestra/testbench": "3.5.*|3.6.*|3.7.*",
37+
"phpunit/phpunit": "^5.5|~6.0|~7.0"
38+
},
39+
"autoload": {
40+
"psr-0": {
41+
"Rebing\\GraphQL\\": "src/"
42+
},
43+
"files": [
44+
"src/Rebing/GraphQL/helpers.php"
45+
]
46+
},
47+
"autoload-dev": {
48+
"classmap": [
49+
"tests/"
50+
]
51+
},
52+
"extra": {
53+
"laravel": {
54+
"providers": [
55+
"Rebing\\GraphQL\\GraphQLServiceProvider"
56+
],
57+
"aliases": {
58+
"GraphQL": "Rebing\\GraphQL\\Support\\Facades\\GraphQL"
59+
}
60+
}
61+
},
62+
"minimum-stability": "dev",
63+
"prefer-stable": true
64+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Rebing\GraphQL\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
8+
class PublishCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'graphql:publish {--force : Overwrite any existing files.}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Publishes GraphQL configuration file to config directory of app';
23+
24+
/**
25+
* Filesystem instance for fs operations
26+
*
27+
* @var Filesystem
28+
*/
29+
protected $files;
30+
31+
/**
32+
* A list of files (source => destination)
33+
*
34+
* @var array
35+
*/
36+
protected $fileMap = [];
37+
38+
public function __construct(Filesystem $files)
39+
{
40+
parent::__construct();
41+
$this->files = $files;
42+
43+
$fromPath = __DIR__ . '/../../..';
44+
$this->fileMap = [
45+
$fromPath.'/config/config.php' => app()->basePath('config/graphql.php'),
46+
$fromPath.'/resources/views/graphiql.php' => app()->basePath('resources/views/vendor/graphql/graphiql.php')
47+
];
48+
}
49+
/**
50+
* Execute the console command.
51+
*
52+
* @return mixed
53+
*/
54+
public function handle()
55+
{
56+
foreach ($this->fileMap as $from => $to) {
57+
if ($this->files->exists($to) && !$this->option('force')) {
58+
continue;
59+
}
60+
$this->createParentDirectory(dirname($to));
61+
$this->files->copy($from, $to);
62+
$this->status($from, $to, 'File');
63+
}
64+
}
65+
/**
66+
* Create the directory to house the published files if needed.
67+
*
68+
* @param string $directory
69+
* @return void
70+
*/
71+
protected function createParentDirectory($directory)
72+
{
73+
if (!$this->files->isDirectory($directory)) {
74+
$this->files->makeDirectory($directory, 0755, true);
75+
}
76+
}
77+
/**
78+
* Write a status message to the console.
79+
*
80+
* @param string $from
81+
* @param string $to
82+
* @return void
83+
*/
84+
protected function status($from, $to)
85+
{
86+
$from = str_replace(base_path(), '', realpath($from));
87+
$to = str_replace(base_path(), '', realpath($to));
88+
$this->line("<info>Copied File</info> <comment>[{$from}]</comment> <info>To</info> <comment>[{$to}]</comment>");
89+
}
90+
}

src/Rebing/GraphQL/GraphQL.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ public static function routeNameTransformer ($name, $schemaParameterPattern, $qu
324324
$routeName = null;
325325

326326
if (count($multiLevelPath) > 1) {
327+
if (is_lumen()) {
328+
array_walk($multiLevelPath, function (&$multiName) {
329+
$multiName = "$multiName:$multiName";
330+
});
331+
}
332+
327333
foreach ($multiLevelPath as $multiName) {
328334
$routeName = !$routeName ? null : $routeName . '/';
329335
$routeName =
@@ -332,7 +338,7 @@ public static function routeNameTransformer ($name, $schemaParameterPattern, $qu
332338
}
333339
}
334340

335-
return $routeName ?: preg_replace($schemaParameterPattern, '{' . $name . '}', $queryRoute);
341+
return $routeName ?: preg_replace($schemaParameterPattern, '{' . (is_lumen() ? "$name:$name" : $name) . '}', $queryRoute);
336342
}
337343

338344
protected function getSchemaConfiguration($schema)

src/Rebing/GraphQL/GraphQLController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public function query(Request $request, $schema = null)
1212

1313
// If there are multiple route params we can expect that there
1414
// will be a schema name that has to be built
15-
if ($request->route()->parameters && count($request->route()->parameters) > 1) {
16-
$schema = implode('/', $request->route()->parameters);
15+
if ($request->request->count() > 1) {
16+
$schema = implode('/', $request->request->all());
1717
}
1818

1919
if( ! $schema)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rebing\GraphQL;
4+
5+
use Rebing\GraphQL\Console\PublishCommand;
6+
7+
class GraphQLLumenServiceProvider extends GraphQLServiceProvider
8+
{
9+
protected function bootPublishes()
10+
{
11+
$configPath = __DIR__.'/../../config';
12+
13+
$this->mergeConfigFrom($configPath.'/config.php', 'graphql');
14+
15+
$viewsPath = __DIR__.'/../../resources/views';
16+
$this->loadViewsFrom($viewsPath, 'graphql');
17+
}
18+
19+
public function register()
20+
{
21+
class_alias('Laravel\Lumen\Routing\Controller', 'Illuminate\Routing\Controller');
22+
23+
parent::register();
24+
}
25+
26+
public function registerConsole()
27+
{
28+
parent::registerConsole();
29+
30+
$this->commands(PublishCommand::class);
31+
}
32+
}

src/Rebing/GraphQL/helpers.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
if (! function_exists('is_lumen')) {
4+
/**
5+
* Detect if Lumen installed.
6+
*
7+
* @return bool
8+
*/
9+
function is_lumen() {
10+
return class_exists('Laravel\Lumen\Application');
11+
}
12+
}

0 commit comments

Comments
 (0)