-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernel.php
More file actions
132 lines (110 loc) · 3.74 KB
/
Kernel.php
File metadata and controls
132 lines (110 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace SyncEngine;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use SyncEngine\Attribute\MenuItem;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
const VERSION = '0.1.0-beta';
private $serviceTemplate = '
SyncEngine\Module\%vendor%\%module%\:
resource: "%kernel.project_dir%/modules/%vendor%/%module%/src"
lazy: false';
public function generateRegistry()
{
$modulesPath = $this->getProjectDir() . '/modules';
$vendors = $this->getDirectories( $modulesPath );
$register = [];
$services = [];
foreach ( $vendors as $vendor ) {
$vendorPath = $modulesPath . '/' . $vendor;
$modules = $this->getDirectories( $vendorPath );
foreach ( $modules as $module ) {
$autoloadFilePath = $vendorPath . '/' . $module . '/register.php';
if ( file_exists( $autoloadFilePath ) ) {
$register[] = "include __DIR__ . '/" . $vendor . '/' . $module . "/register.php';";
}
if ( is_dir( $vendorPath . '/' . $module . '/src' ) ) {
$services[] = $this->generateServiceEntry( $vendor, $module );
}
}
}
$this->writeRegistryFile( $register );
$this->writeServicesFile( $services );
}
private function getDirectories( string $path ): array
{
return array_filter(
scandir( $path ),
function ( $folder ) use ( $path ) {
return is_dir( $path . '/' . $folder ) && ! in_array( $folder, [ '.', '..' ] );
}
);
}
private function generateServiceEntry( string $vendor, string $module ): string
{
return str_replace(
[ '%vendor%', '%module%' ],
[ $vendor, $module ],
$this->serviceTemplate
);
}
private function writeRegistryFile( array $register )
{
$content = "<?php\n" . implode( "\n", $register );
$this->safeFilePutContents( $this->getProjectDir() . '/modules/registry.php', $content );
}
private function writeServicesFile( array $services )
{
$content = '
services:
' . implode( "\n", $services ) . '
_defaults:
autowire: true
autoconfigure: true
# Autoconfigure service tags based on instances.
_instanceof:
SyncEngine\Model\Abstract\EntityModel:
tags: [ \'syncengine.model.entity\' ]
SyncEngine\Model\ModuleModel:
tags: [ \'syncengine.model.module\' ]
SyncEngine\Model\BlueprintModel:
tags: [ \'syncengine.model.blueprint\' ]
SyncEngine\Model\ColumnModel:
tags: [ \'syncengine.model.column\' ]
SyncEngine\Model\CodecModel:
tags: [ \'syncengine.model.codec\' ]
SyncEngine\Model\TaskModel:
tags: [ \'syncengine.model.task\' ]
SyncEngine\Model\WebserviceModel:
tags: [ \'syncengine.model.webservice\' ]';
// @todo Generate YAML based on array?
$this->safeFilePutContents( $this->getProjectDir() . '/config/modules.yaml', $content );
}
private function safeFilePutContents( string $filePath, string $content )
{
if ( file_put_contents( $filePath, $content, LOCK_EX ) === false ) {
throw new \RuntimeException( "Failed to write module registry to file: {$filePath}" );
}
}
protected function build( ContainerBuilder $container ): void
{
$this->generateRegistry();
parent::build( $container );
$container->registerAttributeForAutoconfiguration(
MenuItem::class,
static function (
ChildDefinition $definition, MenuItem $attribute, \ReflectionClass|\ReflectionMethod $reflector
): void {
$args = [];
if ( $reflector instanceof \ReflectionMethod ) {
$args['method'] = $reflector->getName();
}
$definition->addTag( 'syncengine.attribute.menuitem', $args );
}
);
}
}