Skip to content

Commit dc00449

Browse files
Changed syntax for subscriber
1 parent e7db6b7 commit dc00449

File tree

6 files changed

+58
-34
lines changed

6 files changed

+58
-34
lines changed

inc/Commands/GenerateSubscriberCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function execute($name, $type)
9999
$io->write("The subscriber is created at this path: $path", true);
100100

101101
$service_provider_name = $this->class_generator->get_dirname( $name );
102-
$id_subscriber = $this->class_generator->create_id( $this->class_generator->get_fullname($name) );
102+
$id_subscriber = $this->class_generator->get_fullname($name) . '::class';
103103

104104
$this->service_provider_manager->maybe_generate_service_provider($name);
105105

@@ -118,4 +118,5 @@ public function execute($name, $type)
118118
}
119119

120120
}
121+
121122
}

inc/Services/ProviderManager.php

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function add_class(string $path, string $class) {
9494
$provider_content = $this->filesystem->read( $provider_path );
9595

9696
$full_name = $this->class_generator->get_fullname( $class );
97-
$id = $this->class_generator->create_id( $full_name );
97+
$id = $this->class_generator->get_fullname( $full_name ) . '::class';
9898
if(! preg_match('/\n(?<indents>\s*)(protected\s)?\$provides = \[(?<content>[^]]*[^ ]*) *];/', $provider_content, $content) ) {
9999
return;
100100
}
@@ -105,7 +105,7 @@ public function add_class(string $path, string $class) {
105105
}
106106

107107
$indents = $content['indents'];
108-
$content = $content['content'] . "$indents '" . $id . "',\n";
108+
$content = $content['content'] . "$indents " . $id . ",\n";
109109
$provider_content = preg_replace('/\$provides = \[(?<content>[^\]])*];/', "\$provides = [$content$indents];", $provider_content);
110110

111111
preg_match( '/\n(?<indents> *)public function register\(\)\s*{ *\n(?<content>[^}]*)}/',
@@ -119,7 +119,7 @@ public function add_class(string $path, string $class) {
119119
$content['content'] = rtrim($content['content'], " \n") . "\n";
120120
}
121121

122-
$content = $content['content'] . "$indents \$this->getContainer()->share('" . $id . "', $full_name::class);\n";
122+
$content = $content['content'] . "$indents \$this->getContainer()->share(" . $id . ", $full_name::class);\n";
123123
$provider_content = preg_replace( '/public function register\(\)[^}]*{ *\n(?<content>[^}]*)}/', "public function register()\n$indents{\n$content$indents}", $provider_content );
124124

125125
$this->filesystem->update( $provider_path, $provider_content );
@@ -164,18 +164,36 @@ public function register_subscriber(string $id, string $path, SubscriberType $ty
164164
*/
165165
protected function add_to_subscriber_method(string $id, string $method, string $content): string {
166166
if ( ! preg_match('/\n(?<indents> *)public function ' . $method . '\(\)[^{]*{(?<content>[^}]*)}/', $content, $results ) ) {
167-
preg_match('/(?<content>\$provides = \[[^]]*];)/', $content, $results);
168-
$new_content = $this->renderer->apply_template('serviceprovider/_partials/' . $method . '.php.tpl', [
169-
'ids' => "'$id',"
170-
]);
171-
$results = $results['content'] . $new_content;
172-
return preg_replace('/(?<content>\$provides = \[[^]]*];) *\n/', $results, $content);
167+
if($this->is_autoresolver($content)) {
168+
return $this->add_method_autoresolve($content, $method, $id);
169+
}
170+
return $this->add_method_vanilla($content, $method, $id);
173171
}
174172
$indents = $results['indents'];
175-
$results = $results['content'] . " '" . $id . "',\n";
173+
$results = $results['content'] . " " . $id . ",\n";
176174
return preg_replace('/public function ' . $method . '\(\)[^}]*{(?<content>[^}]*)}/', "public function $method()\n$indents{\n$results$indents}", $content);
177175
}
178176

177+
protected function add_method_autoresolve(string $content, string $method, string $id) {
178+
if(! preg_match('/class(?<content>\s*[^{]*{\h*\n)/', $content, $results)) {
179+
return $content;
180+
}
181+
$new_content = $this->renderer->apply_template('serviceprovider/_partials/' . $method . '.php.tpl', [
182+
'ids' => "$id,"
183+
]);
184+
$results = $results['content'] . $new_content;
185+
return preg_replace('/class(?<content>\s*[^{]*{\h*\n)/', $results, $content);
186+
}
187+
188+
protected function add_method_vanilla(string $content, string $method, string $id){
189+
preg_match('/(?<content>\$provides = \[[^]]*];)/', $content, $results);
190+
$new_content = $this->renderer->apply_template('serviceprovider/_partials/' . $method . '.php.tpl', [
191+
'ids' => "$id,"
192+
]);
193+
$results = $results['content'] . $new_content;
194+
return preg_replace('/(?<content>\$provides = \[[^]]*];) *\n/', $results, $content);
195+
}
196+
179197
/**
180198
* Instantiate a class inside the service provider.
181199
*
@@ -190,15 +208,20 @@ public function instantiate(string $path, string $class) {
190208
$provider_content = $this->filesystem->read( $provider_path );
191209

192210
$full_name = $this->class_generator->get_fullname( $class );
193-
$id = $this->class_generator->create_id( $full_name );
211+
$id = $full_name . '::class';
194212

195213
preg_match( '/\n(?<indents> *)public function register\(\)[^}]*\s*{(?<content>[^}]*)}/', $provider_content, $content );
196214

197215
$indents = $content['indents'];
198-
$content = $content['content'] . "$indents\$this->getContainer()->get('" . $id . "');\n";
216+
$content = $content['content'] . "$indents\$this->getContainer()->get(" . $id . ");\n";
199217

200218
$provider_content = preg_replace( '/public function register\(\)[^}]*{(?<content>[^}]*)}/', "public function register()\n$indents{"."$content$indents}", $provider_content );
201219

202220
$this->filesystem->update( $provider_path, $provider_content );
203221
}
222+
223+
224+
public function is_autoresolver(string $content) {
225+
return (bool) preg_match('/Dependencies\\\\RocketLauncherAutoresolver\\\\ServiceProvider/', $content);
226+
}
204227
}

tests/Fixtures/inc/Commands/GenerateSubscriberCommand/files/admin_provider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ServiceProvider extends AbstractServiceProvider
2020
* @var array
2121
*/
2222
protected $provides = [
23-
'psr2plugin.engine.test.mysubscriber',
23+
\PSR2Plugin\Engine\Test\MySubscriber::class,
2424
];
2525

2626
/**
@@ -30,7 +30,7 @@ class ServiceProvider extends AbstractServiceProvider
3030
*/
3131
public function get_admin_subscribers(): array {
3232
return [
33-
'psr2plugin.engine.test.mysubscriber',
33+
\PSR2Plugin\Engine\Test\MySubscriber::class,
3434
];
3535
}
3636

@@ -41,6 +41,6 @@ public function get_admin_subscribers(): array {
4141
*/
4242
public function register()
4343
{
44-
$this->getContainer()->share('psr2plugin.engine.test.mysubscriber', \PSR2Plugin\Engine\Test\MySubscriber::class);
44+
$this->getContainer()->share(\PSR2Plugin\Engine\Test\MySubscriber::class, \PSR2Plugin\Engine\Test\MySubscriber::class);
4545
}
4646
}

tests/Fixtures/inc/Commands/GenerateSubscriberCommand/files/front_provider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ServiceProvider extends AbstractServiceProvider
2020
* @var array
2121
*/
2222
protected $provides = [
23-
'psr2plugin.engine.test.mysubscriber',
23+
\PSR2Plugin\Engine\Test\MySubscriber::class,
2424
];
2525

2626
/**
@@ -30,7 +30,7 @@ class ServiceProvider extends AbstractServiceProvider
3030
*/
3131
public function get_front_subscribers(): array {
3232
return [
33-
'psr2plugin.engine.test.mysubscriber',
33+
\PSR2Plugin\Engine\Test\MySubscriber::class,
3434
];
3535
}
3636

@@ -41,6 +41,6 @@ public function get_front_subscribers(): array {
4141
*/
4242
public function register()
4343
{
44-
$this->getContainer()->share('psr2plugin.engine.test.mysubscriber', \PSR2Plugin\Engine\Test\MySubscriber::class);
44+
$this->getContainer()->share(\PSR2Plugin\Engine\Test\MySubscriber::class, \PSR2Plugin\Engine\Test\MySubscriber::class);
4545
}
4646
}

tests/Fixtures/inc/Commands/GenerateSubscriberCommand/files/provider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ServiceProvider extends AbstractServiceProvider
2020
* @var array
2121
*/
2222
protected $provides = [
23-
'psr2plugin.engine.test.mysubscriber',
23+
\PSR2Plugin\Engine\Test\MySubscriber::class,
2424
];
2525

2626
/**
@@ -30,7 +30,7 @@ class ServiceProvider extends AbstractServiceProvider
3030
*/
3131
public function get_common_subscribers(): array {
3232
return [
33-
'psr2plugin.engine.test.mysubscriber',
33+
\PSR2Plugin\Engine\Test\MySubscriber::class,
3434
];
3535
}
3636

@@ -41,6 +41,6 @@ public function get_common_subscribers(): array {
4141
*/
4242
public function register()
4343
{
44-
$this->getContainer()->share('psr2plugin.engine.test.mysubscriber', \PSR2Plugin\Engine\Test\MySubscriber::class);
44+
$this->getContainer()->share(\PSR2Plugin\Engine\Test\MySubscriber::class, \PSR2Plugin\Engine\Test\MySubscriber::class);
4545
}
4646
}

tests/Fixtures/inc/Commands/GenerateTableCommand/files/provider.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class ServiceProvider extends AbstractServiceProvider
2020
* @var array
2121
*/
2222
protected $provides = [
23-
'psr2plugin.engine.test.database.queries.mytable',
24-
'psr2plugin.engine.test.database.tables.mytable',
25-
'psr2plugin.engine.test.database.rows.mytable',
26-
'psr2plugin.engine.test.database.schemas.mytable',
23+
\PSR2Plugin\Engine\Test\Database\Queries\MyTable::class,
24+
\PSR2Plugin\Engine\Test\Database\Tables\MyTable::class,
25+
\PSR2Plugin\Engine\Test\Database\Rows\MyTable::class,
26+
\PSR2Plugin\Engine\Test\Database\Schemas\MyTable::class,
2727
];
2828

2929
/**
@@ -33,13 +33,13 @@ class ServiceProvider extends AbstractServiceProvider
3333
*/
3434
public function register()
3535
{
36-
$this->getContainer()->share('psr2plugin.engine.test.database.queries.mytable', \PSR2Plugin\Engine\Test\Database\Queries\MyTable::class);
37-
$this->getContainer()->get('psr2plugin.engine.test.database.queries.mytable');
38-
$this->getContainer()->share('psr2plugin.engine.test.database.tables.mytable', \PSR2Plugin\Engine\Test\Database\Tables\MyTable::class);
39-
$this->getContainer()->get('psr2plugin.engine.test.database.tables.mytable');
40-
$this->getContainer()->share('psr2plugin.engine.test.database.rows.mytable', \PSR2Plugin\Engine\Test\Database\Rows\MyTable::class);
41-
$this->getContainer()->get('psr2plugin.engine.test.database.rows.mytable');
42-
$this->getContainer()->share('psr2plugin.engine.test.database.schemas.mytable', \PSR2Plugin\Engine\Test\Database\Schemas\MyTable::class);
43-
$this->getContainer()->get('psr2plugin.engine.test.database.schemas.mytable');
36+
$this->getContainer()->share(\PSR2Plugin\Engine\Test\Database\Queries\MyTable::class, \PSR2Plugin\Engine\Test\Database\Queries\MyTable::class);
37+
$this->getContainer()->get(\PSR2Plugin\Engine\Test\Database\Queries\MyTable::class);
38+
$this->getContainer()->share(\PSR2Plugin\Engine\Test\Database\Tables\MyTable::class, \PSR2Plugin\Engine\Test\Database\Tables\MyTable::class);
39+
$this->getContainer()->get(\PSR2Plugin\Engine\Test\Database\Tables\MyTable::class);
40+
$this->getContainer()->share(\PSR2Plugin\Engine\Test\Database\Rows\MyTable::class, \PSR2Plugin\Engine\Test\Database\Rows\MyTable::class);
41+
$this->getContainer()->get(\PSR2Plugin\Engine\Test\Database\Rows\MyTable::class);
42+
$this->getContainer()->share(\PSR2Plugin\Engine\Test\Database\Schemas\MyTable::class, \PSR2Plugin\Engine\Test\Database\Schemas\MyTable::class);
43+
$this->getContainer()->get(\PSR2Plugin\Engine\Test\Database\Schemas\MyTable::class);
4444
}
4545
}

0 commit comments

Comments
 (0)