Skip to content

Commit e7db6b7

Browse files
Fixed service provider command
1 parent 1d65c37 commit e7db6b7

File tree

14 files changed

+322
-11
lines changed

14 files changed

+322
-11
lines changed

inc/App.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RocketLauncherBuilder;
44

55
use Ahc\Cli\Application;
6+
use Ahc\Cli\Helper\Shell;
67

78
class App extends Application
89
{
@@ -35,4 +36,18 @@ public function launchCommand(string $command_class, array $params = []) {
3536

3637
$this->doAction($command_instance);
3738
}
39+
40+
/**
41+
* Launch a shell command.
42+
*
43+
* @param string $command command to launch.
44+
* @param string|null $directory directory to exec
45+
* @param array $env
46+
* @param float $timeout
47+
* @return Shell
48+
*/
49+
public function launchShell(string $command, string $directory = null, array $env = [], float $timeout = 10.5) {
50+
$shell = new Shell($command);
51+
return $shell->setOptions($directory, $env, $timeout)->execute();
52+
}
3853
}

inc/Commands/GenerateServiceProvider.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
use League\Flysystem\Filesystem;
77
use RocketLauncherBuilder\Entities\Configurations;
88
use RocketLauncherBuilder\Services\ClassGenerator;
9+
use RocketLauncherBuilder\Services\ProjectManager;
910

1011
/**
1112
* @property string|null $name Name from the service provider.
1213
*/
1314
class GenerateServiceProvider extends Command
1415
{
16+
CONST PROVIDER_CONFIGS = 'configs/providers.php';
17+
1518
/**
1619
* Class generator.
1720
*
@@ -33,28 +36,39 @@ class GenerateServiceProvider extends Command
3336
*/
3437
protected $configurations;
3538

39+
/**
40+
* @var ProjectManager
41+
*/
42+
protected $project_manager;
43+
3644
/**
3745
* Instantiate the class.
3846
*
3947
* @param ClassGenerator $class_generator Class generator.
4048
* @param Filesystem $filesystem Interacts with the filesystem.
4149
* @param Configurations $configurations Configuration from the project.
4250
*/
43-
public function __construct(ClassGenerator $class_generator, Filesystem $filesystem, Configurations $configurations)
51+
public function __construct(ClassGenerator $class_generator, Filesystem $filesystem, Configurations $configurations, ProjectManager $project_manager)
4452
{
4553
parent::__construct('provider', 'Generate service provider class');
4654

4755
$this->filesystem = $filesystem;
4856
$this->configurations = $configurations;
4957
$this->class_generator = $class_generator;
58+
$this->project_manager = $project_manager;
5059

5160
$this
5261
->argument('[name]', 'Full name from the service provider')
62+
5363
// Usage examples:
5464
->usage(
5565
// append details or explanation of given example with ` ## ` so they will be uniformly aligned when shown
5666
'<bold> $0 provider</end> <comment>MyClass</end> ## Creates the service provider<eol/>'
5767
);
68+
69+
if($this->project_manager->is_resolver_present()) {
70+
$this->option('-t --type', 'Type from the provider');
71+
}
5872
}
5973

6074
/**
@@ -77,11 +91,13 @@ public function interact(Interactor $io)
7791
* @return void
7892
* @throws \League\Flysystem\FileNotFoundException
7993
*/
80-
public function execute($name)
94+
public function execute($name, $type)
8195
{
8296
$io = $this->app()->io();
8397

84-
$path = $this->class_generator->generate('serviceprovider.php.tpl', $name );
98+
$is_resolver = $type === 'autoresolver' || $type === 'a';
99+
100+
$path = $this->class_generator->generate( $is_resolver ? 'serviceprovider/autoresolver.php.tpl' : 'serviceprovider/vanilla.php.tpl', $name );
85101

86102
if( ! $path ) {
87103
$io->write("The class already exists", true);
@@ -90,12 +106,18 @@ public function execute($name)
90106

91107
$io->write("The service provider is created at this path: $path", true);
92108

109+
$this->add_provider_to_plugin($name);
110+
111+
$this->add_provider_to_configs($name);
112+
}
113+
114+
protected function add_provider_to_plugin(string $class) {
93115
$plugin_path = $this->configurations->getCodeDir() . 'Plugin.php';
94116

95117
$plugin_content = $this->filesystem->read($plugin_path);
96118

97-
preg_match('/\$providers = \[(?<content>[^]]*)];/', $plugin_content, $content);
98-
$content = $content['content'] . " " . $this->class_generator->get_fullname($name) . "::class,\n";
119+
preg_match('/\$providers = \[(?<content>[^]]*)];/', $plugin_content, $content);
120+
$content = $content['content'] . " " . $this->class_generator->get_fullname($class) . "::class,\n";
99121
$plugin_content = preg_replace('/\$providers = \[(?<content>[^\]])*];/', "\$providers = [$content ];", $plugin_content);
100122

101123
if(! $plugin_content) {
@@ -104,4 +126,22 @@ public function execute($name)
104126

105127
$this->filesystem->update($plugin_path, $plugin_content);
106128
}
129+
130+
protected function add_provider_to_configs(string $class) {
131+
if(! $this->filesystem->has(self::PROVIDER_CONFIGS)) {
132+
return;
133+
}
134+
$content = $this->filesystem->read(self::PROVIDER_CONFIGS);
135+
136+
if(! preg_match('/(?<array>return\s\[(?:[^[\]]+|(?R))*\]\s*;\s*$)/', $content, $results)) {
137+
return;
138+
}
139+
140+
$new_content = " {$this->class_generator->get_fullname($class)}::class,\n";
141+
$new_content .= "];\n";
142+
143+
$content = preg_replace('/\n\h*]\s*;\s*$/', $new_content, $content);
144+
145+
$this->filesystem->update(self::PROVIDER_CONFIGS, $content);
146+
}
107147
}

inc/ServiceProviders/BaseServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,21 @@ public function __construct(Configurations $configs, Filesystem $filesystem, str
8383
*/
8484
public function attach_commands(App $app): App
8585
{
86+
$project_manager = new ProjectManager($this->filesystem);
87+
8688
$class_generator = new ClassGenerator($this->filesystem, $this->renderer, $this->configs);
87-
$provider_manager = new ProviderManager($app, $this->filesystem, $class_generator, $this->renderer);
89+
$provider_manager = new ProviderManager($app, $this->filesystem, $class_generator, $this->renderer, $project_manager);
8890

8991
$setup_generator = new SetUpGenerator($this->filesystem, $this->renderer, $this->configs);
9092

9193
$fixture_generator = new FixtureGenerator($this->filesystem, $this->renderer);
9294

9395
$content_generator = new ContentGenerator($this->filesystem, $this->renderer);
9496

95-
$project_manager = new ProjectManager($this->filesystem);
96-
9797
$bootstrap_manager = new BootstrapManager($this->filesystem, $this->configs, $this->renderer);
9898

9999
$app->add(new GenerateSubscriberCommand($class_generator, $this->filesystem, $provider_manager));
100-
$app->add(new GenerateServiceProvider($class_generator, $this->filesystem, $this->configs));
100+
$app->add(new GenerateServiceProvider($class_generator, $this->filesystem, $this->configs, $project_manager));
101101
$app->add(new GenerateTableCommand($class_generator, $this->configs, $provider_manager));
102102
$app->add(new GenerateTestsCommand($class_generator, $this->configs, $this->filesystem, $setup_generator, $fixture_generator, $content_generator, $bootstrap_manager, $project_manager));
103103
$app->add(new GenerateFixtureCommand($class_generator, $this->filesystem, $this->configs));

inc/Services/ProjectManager.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ public function add_external_test_group(string $group) {
6666
return true;
6767
}
6868

69+
public function is_resolver_present() {
70+
if( ! $this->filesystem->has(self::COMPOSER_FILE)) {
71+
return false;
72+
}
73+
74+
$content = $this->filesystem->read(self::COMPOSER_FILE);
75+
$json = json_decode($content,true);
76+
77+
return $json && key_exists('extra', $json) && key_exists('mozart', $json['extra']) && key_exists('packages', $json['extra']['mozart']) && in_array('crochetfeve0251/rocket-launcher-autoresolver', $json['extra']['mozart']['packages']);
78+
}
79+
6980
/**
7081
* Create an ID from the class.
7182
*
@@ -78,4 +89,5 @@ protected function create_id(string $class ) {
7889
$class = str_replace( '\\', '.', $class );
7990
return 'test-integration-' . strtolower( preg_replace( ['/([a-z])\d([A-Z])/', '/[^_]([A-Z][a-z])]/'], '$1_$2', $class ) );
8091
}
92+
8193
}

inc/Services/ProviderManager.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class ProviderManager
3636
*/
3737
protected $renderer;
3838

39+
/**
40+
* @var ProjectManager
41+
*/
42+
protected $project_manager;
43+
3944
/**
4045
* Instantiate the class.
4146
*
@@ -44,12 +49,13 @@ class ProviderManager
4449
* @param ClassGenerator $class_generator Class generator.
4550
* @param Renderer $renderer Renderer that handles layout of template files.
4651
*/
47-
public function __construct(App $app, Filesystem $filesystem, ClassGenerator $class_generator, Renderer $renderer)
52+
public function __construct(App $app, Filesystem $filesystem, ClassGenerator $class_generator, Renderer $renderer, ProjectManager $project_manager)
4853
{
4954
$this->app = $app;
5055
$this->filesystem = $filesystem;
5156
$this->class_generator = $class_generator;
5257
$this->renderer = $renderer;
58+
$this->project_manager = $project_manager;
5359
}
5460

5561
/**
@@ -81,6 +87,9 @@ public function maybe_generate_service_provider(string $namespace ) {
8187
* @throws \League\Flysystem\FileNotFoundException
8288
*/
8389
public function add_class(string $path, string $class) {
90+
if( $this->project_manager->is_resolver_present() ) {
91+
return;
92+
}
8493
$provider_path = $this->class_generator->generate_path( $path. '/ServiceProvider' );
8594
$provider_content = $this->filesystem->read( $provider_path );
8695

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
class {{ class_name }} extends Dependencies\RocketLauncherAutoresolver\ServiceProvider
6+
{
7+
8+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use {{ base_namespace }}Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
6+
7+
/**
8+
* Service provider.
9+
*/
10+
class {{ class_name }} extends AbstractServiceProvider
11+
{
12+
13+
/**
14+
* The provided array is a way to let the container
15+
* know that a service is provided by this service
16+
* provider. Every service that is registered via
17+
* this service provider must have an alias added
18+
* to this array or it will be ignored.
19+
*
20+
* @var array
21+
*/
22+
protected $provides = [
23+
24+
];
25+
26+
/**
27+
* Registers items with the container
28+
*
29+
* @return void
30+
*/
31+
public function register()
32+
{
33+
34+
}
35+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"name": "crochetfeve0251/rocket-launcher",
3+
"description": "Template for a psr4 compatible plugin",
4+
"keywords": [
5+
"wordpress"
6+
],
7+
"license": "GPL-2.0-or-later",
8+
"authors": [
9+
{
10+
"name": "CrochetFeve0251"
11+
}
12+
],
13+
"type": "project",
14+
"config": {
15+
"sort-packages": true,
16+
"preferred-install": {
17+
"wp-media/phpunit": "source"
18+
},
19+
"process-timeout": 0,
20+
"allow-plugins": {
21+
"composer/installers": true,
22+
"mnsami/composer-custom-directory-installer": true,
23+
"dealerdirect/phpcodesniffer-composer-installer": true
24+
}
25+
},
26+
"repositories": [
27+
{
28+
"type": "composer",
29+
"url": "https://wpackagist.org"
30+
}
31+
],
32+
"require": {
33+
"php": ">=7.0",
34+
"berlindb/core": "^2.0",
35+
"composer/installers": "^1.0 || ^2.0",
36+
"monolog/monolog": "^1.0"
37+
},
38+
"require-dev": {
39+
"php": "^7 || ^8",
40+
"brain/monkey": "^2.0",
41+
"coenjacobs/mozart": "^0.7",
42+
"crochetfeve0251/rocket-launcher-builder": "^0.0.5",
43+
"crochetfeve0251/rocket-launcher-take-off": "^0.0.2",
44+
"crochetfeve0251/rocket-launcher-core": "^0.0.4",
45+
"crochetfeve0251/rocket-launcher-autoresolver": "^0.0.3",
46+
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
47+
"league/container": "^3.3",
48+
"mnsami/composer-custom-directory-installer": "^2.0",
49+
"phpcompatibility/phpcompatibility-wp": "^2.0",
50+
"phpstan/phpstan": "^0.12",
51+
"phpunit/phpunit": "^7.5 || ^8 || ^9",
52+
"psr/container": "1.0.0",
53+
"roave/security-advisories": "dev-master",
54+
"szepeviktor/phpstan-wordpress": "^0.7.0",
55+
"wp-coding-standards/wpcs": "^2",
56+
"wp-media/phpunit": "^3.0"
57+
},
58+
"autoload": {
59+
"classmap": [
60+
"inc/classes"
61+
],
62+
"psr-4": {
63+
"PSR2Plugin\\": "inc/"
64+
}
65+
},
66+
"autoload-dev": {
67+
"psr-4": {
68+
"PSR2Plugin\\Tests\\": "tests/"
69+
}
70+
},
71+
"extra": {
72+
"installer-paths": {
73+
"vendor/{$vendor}/{$name}/": ["type:wordpress-plugin"]
74+
},
75+
"mozart": {
76+
"dep_namespace": "PSR2Plugin\\Dependencies\\",
77+
"dep_directory": "/inc/Dependencies/",
78+
"classmap_directory": "/inc/classes/dependencies/",
79+
"classmap_prefix": "PSR2Plugin",
80+
"packages": [
81+
"berlindb/core",
82+
"league/container",
83+
"crochetfeve0251/rocket-launcher-core",
84+
"crochetfeve0251/rocket-launcher-autoresolver"
85+
]
86+
}
87+
},
88+
"scripts": {
89+
"test-unit": "\"vendor/bin/phpunit\" --testsuite unit --colors=always --configuration tests/Unit/phpunit.xml.dist",
90+
"test-integration": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --exclude-group AdminOnly",
91+
"test-integration-adminonly": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group AdminOnly",
92+
"run-tests": [
93+
"@test-unit",
94+
"@test-integration",
95+
"@test-integration-adminonly"
96+
],
97+
"run-stan": "vendor/bin/phpstan analyze --memory-limit=2G --no-progress",
98+
"install-codestandards": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run",
99+
"phpcs": "phpcs --basepath=.",
100+
"phpcs-changed": "./bin/phpcs-changed.sh",
101+
"phpcs:fix": "phpcbf",
102+
"post-install-cmd": [
103+
"\"vendor/bin/mozart\" compose",
104+
"composer dump-autoload"
105+
],
106+
"post-update-cmd": [
107+
"\"vendor/bin/mozart\" compose",
108+
"composer dump-autoload"
109+
],
110+
"code-coverage": "\"vendor/bin/phpunit\" --testsuite unit --colors=always --configuration tests/Unit/phpunit.xml.dist --coverage-clover=tests/report/coverage.clover"
111+
},
112+
"minimum-stability": "stable",
113+
"prefer-stable": true
114+
}

0 commit comments

Comments
 (0)