|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\CarbonExtension; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\CarbonExtension\TypeDefinition\FormatArgument; |
|
13
|
|
|
use Railt\Foundation\Events\TypeBuilding; |
|
14
|
|
|
use Railt\Foundation\Extensions\BaseExtension; |
|
15
|
|
|
use Railt\Io\File; |
|
16
|
|
|
use Railt\Routing\Contracts\RouterInterface; |
|
17
|
|
|
use Railt\SDL\Contracts\Definitions\TypeDefinition; |
|
18
|
|
|
use Railt\SDL\Contracts\Dependent\FieldDefinition; |
|
19
|
|
|
use Railt\SDL\Reflection\Builder\Dependent\FieldBuilder; |
|
20
|
|
|
use Railt\SDL\Schema\CompilerInterface as Compiler; |
|
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface as Events; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Extension |
|
25
|
|
|
*/ |
|
26
|
|
|
class Extension extends BaseExtension |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string Path to graphql schema file |
|
30
|
|
|
*/ |
|
31
|
|
|
private const GRAPHQL_DATETIME = __DIR__ . '/../resources/datetime.graphqls'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Compiler $compiler |
|
35
|
|
|
* @throws \Railt\Io\Exception\NotReadableException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function boot(Compiler $compiler): void |
|
38
|
|
|
{ |
|
39
|
|
|
$compiler->compile(File::fromPathname(self::GRAPHQL_DATETIME)); |
|
40
|
|
|
|
|
41
|
|
|
$this->call(\Closure::fromCallable([$this, 'listen'])); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Events $listener |
|
46
|
|
|
* @param Compiler $compiler |
|
47
|
|
|
* @param RouterInterface $router |
|
48
|
|
|
*/ |
|
49
|
|
|
private function listen(Events $listener, Compiler $compiler, RouterInterface $router): void |
|
50
|
|
|
{ |
|
51
|
|
|
$controller = $this->make(CarbonController::class); |
|
52
|
|
|
|
|
53
|
|
|
$invoke = function (TypeBuilding $event) use ($compiler, $router, $controller): void { |
|
54
|
|
|
if ($this->isCarbonField($event->getType())) { |
|
55
|
|
|
$this->extend($compiler, $event->getType()); |
|
56
|
|
|
|
|
57
|
|
|
$router->route($event->getType())->then(\Closure::fromCallable([$controller, 'getDateTime'])); |
|
58
|
|
|
} |
|
59
|
|
|
}; |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$listener->addListener(TypeBuilding::class, $invoke); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param TypeDefinition $type |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
private function isCarbonField(TypeDefinition $type): bool |
|
70
|
|
|
{ |
|
71
|
|
|
return $type instanceof FieldDefinition && $type->getTypeDefinition()->getName() === 'Carbon'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param Compiler $compiler |
|
76
|
|
|
* @param FieldDefinition|FieldBuilder|TypeDefinition $field |
|
77
|
|
|
*/ |
|
78
|
|
|
private function extend(Compiler $compiler, FieldDefinition $field): void |
|
79
|
|
|
{ |
|
80
|
|
|
(function () use ($field, $compiler): void { |
|
81
|
|
|
$this->arguments[FormatArgument::ARGUMENT_NAME] = new FormatArgument($field, $compiler); |
|
|
|
|
|
|
82
|
|
|
})->call($field); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: