66use League \Flysystem \Filesystem ;
77use RocketLauncherBuilder \Entities \Configurations ;
88use RocketLauncherBuilder \Services \ClassGenerator ;
9+ use RocketLauncherBuilder \Services \ProjectManager ;
910
1011/**
1112 * @property string|null $name Name from the service provider.
1213 */
1314class 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}
0 commit comments