Skip to content

Commit 8a439c5

Browse files
committed
Import classes in generated code.
1 parent 875a34b commit 8a439c5

File tree

3 files changed

+114
-57
lines changed

3 files changed

+114
-57
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"plaisio/kernel": "^1.1",
1818
"plaisio/obfuscator": "^1.2",
1919
"plaisio/response-core": "^1.1",
20-
"setbased/helper-code-store-php": "^2.3",
20+
"setbased/helper-code-store-php": "^2.4.1",
2121
"setbased/php-stratum-middle": "^5.2",
2222
"symfony/console": "^5.0"
2323
},

src/Helper/ExceptionHandlerCodeGenerator.php

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Plaisio\ExceptionHandler\Helper;
55

6+
use SetBased\Helper\CodeStore\Importing;
67
use SetBased\Helper\CodeStore\PhpCodeStore;
78

89
/**
@@ -11,6 +12,20 @@
1112
class ExceptionHandlerCodeGenerator
1213
{
1314
//--------------------------------------------------------------------------------------------------------------------
15+
/**
16+
* The parent class of the generated exception handler.
17+
*
18+
* @var string
19+
*/
20+
private static $parentClass = '\\Plaisio\\ExceptionHandler\\ExceptionHandler';
21+
22+
/**
23+
* The helper object for importing classes.
24+
*
25+
* @var Importing
26+
*/
27+
private $importing;
28+
1429
/**
1530
* The PHP code store.
1631
*
@@ -31,17 +46,23 @@ public function __construct()
3146
/**
3247
* Generates the PHP code of the exception handler.
3348
*
34-
* @param string $class The fully qualified class name.
35-
* @param array[] $allAgents The metadata of the exception agents.
49+
* @param string $fullyQualifiedName The fully qualified class name.
50+
* @param array[] $allAgents The metadata of the exception agents.
3651
*
3752
* @return string
3853
*/
39-
public function generateCode(string $class, array $allAgents): string
54+
public function generateCode(string $fullyQualifiedName, array $allAgents): string
4055
{
41-
$parts = explode('\\', $class);
56+
$parts = explode('\\', $fullyQualifiedName);
4257
$class = array_pop($parts);
4358
$namespace = ltrim(implode('\\', $parts), '\\');
4459

60+
$this->importing = new Importing($namespace);
61+
$this->importing->addClass(self::$parentClass);
62+
$this->importClasses($allAgents);
63+
64+
$this->importing->prepare();
65+
4566
$this->generateHeader($namespace);
4667
$this->generateClass($class, $allAgents);
4768
$this->generateTrailer();
@@ -84,7 +105,7 @@ private function generateHeader(string $namespace): void
84105
$this->store->append('');
85106
$this->store->append(sprintf('namespace %s;', $namespace));
86107
$this->store->append('');
87-
$this->store->append('use Plaisio\ExceptionHandler\ExceptionHandler;');
108+
$this->store->append($this->importing->imports());
88109
$this->store->append('');
89110
}
90111

@@ -101,7 +122,9 @@ private function generateMethod(string $method, array $agents)
101122
$this->store->append('/**');
102123
$this->store->append(' * @inheritdoc', false);
103124
$this->store->append(' */', false);
104-
$this->store->append(sprintf('public function %s(\\Throwable $exception): void', $method));
125+
$this->store->append(sprintf('public function %s(%s $exception): void',
126+
$method,
127+
$this->importing->simplyFullyQualifiedName('\\Throwable')));
105128
$this->store->append('{');
106129
$this->store->append('switch (true)');
107130
$this->store->append('{');
@@ -110,9 +133,12 @@ private function generateMethod(string $method, array $agents)
110133
{
111134
if (!$first) $this->store->append('');
112135

113-
$this->store->append(sprintf("case is_a(\$exception, '%s'):", $agent['type']));
114-
$this->store->append(sprintf('/** @var \\%s $exception */', $agent['type']));
115-
$this->store->append(sprintf("\$handler = new \\%s();", $agent['class']));
136+
$this->store->append(sprintf("case is_a(\$exception, %s::class):",
137+
$this->importing->simplyFullyQualifiedName($agent['type'])));
138+
$this->store->append(sprintf('/** @var %s $exception */',
139+
$this->importing->simplyFullyQualifiedName($agent['type'])));
140+
$this->store->append(sprintf("\$handler = new %s();",
141+
$this->importing->simplyFullyQualifiedName($agent['class'])));
116142
$this->store->append(sprintf('$handler->%s($exception);', $agent['method']));
117143
$this->store->append('break;');
118144

@@ -133,6 +159,24 @@ private function generateTrailer(): void
133159
$this->store->appendSeparator();
134160
}
135161

162+
//--------------------------------------------------------------------------------------------------------------------
163+
/**
164+
* Adds classes of exception agents to the importing helper object.
165+
*
166+
* @param array[] $allAgents The metadata of the exception agents.
167+
*/
168+
private function importClasses(array $allAgents): void
169+
{
170+
foreach ($allAgents as $name => $agents)
171+
{
172+
foreach ($agents as $agent)
173+
{
174+
$this->importing->addClass($agent['type']);
175+
$this->importing->addClass($agent['class']);
176+
}
177+
}
178+
}
179+
136180
//--------------------------------------------------------------------------------------------------------------------
137181
}
138182

test/Command/Foo.txt

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@ declare(strict_types=1);
33

44
namespace SetBased\Stratum\Test\Application;
55

6+
use Plaisio\Exception\BadRequestException;
7+
use Plaisio\Exception\InvalidUrlException;
8+
use Plaisio\Exception\NotAuthorizedException;
9+
use Plaisio\Exception\NotPreferredUrlException;
10+
use Plaisio\ExceptionHandler\BadRequestExceptionAgent;
11+
use Plaisio\ExceptionHandler\DecodeExceptionAgent;
612
use Plaisio\ExceptionHandler\ExceptionHandler;
13+
use Plaisio\ExceptionHandler\InvalidUrlExceptionAgent;
14+
use Plaisio\ExceptionHandler\NotAuthorizedExceptionAgent;
15+
use Plaisio\ExceptionHandler\NotPreferredUrlExceptionAgent;
16+
use Plaisio\ExceptionHandler\ResultExceptionAgent;
17+
use Plaisio\ExceptionHandler\ThrowableAgent;
18+
use Plaisio\Obfuscator\Exception\DecodeException;
19+
use SetBased\Stratum\Middle\Exception\ResultException;
720

821
/**
922
* Concrete implementation of the exception handler.
@@ -18,27 +31,27 @@ class Foo implements ExceptionHandler
1831
{
1932
switch (true)
2033
{
21-
case is_a($exception, 'Plaisio\Exception\BadRequestException'):
22-
/** @var \Plaisio\Exception\BadRequestException $exception */
23-
$handler = new \Plaisio\ExceptionHandler\BadRequestExceptionAgent();
34+
case is_a($exception, BadRequestException::class):
35+
/** @var BadRequestException $exception */
36+
$handler = new BadRequestExceptionAgent();
2437
$handler->handlePrepareException($exception);
2538
break;
2639

27-
case is_a($exception, 'Plaisio\Exception\InvalidUrlException'):
28-
/** @var \Plaisio\Exception\InvalidUrlException $exception */
29-
$handler = new \Plaisio\ExceptionHandler\InvalidUrlExceptionAgent();
40+
case is_a($exception, InvalidUrlException::class):
41+
/** @var InvalidUrlException $exception */
42+
$handler = new InvalidUrlExceptionAgent();
3043
$handler->handlePrepareException($exception);
3144
break;
3245

33-
case is_a($exception, 'Plaisio\Exception\NotAuthorizedException'):
34-
/** @var \Plaisio\Exception\NotAuthorizedException $exception */
35-
$handler = new \Plaisio\ExceptionHandler\NotAuthorizedExceptionAgent();
46+
case is_a($exception, NotAuthorizedException::class):
47+
/** @var NotAuthorizedException $exception */
48+
$handler = new NotAuthorizedExceptionAgent();
3649
$handler->handlePrepareException($exception);
3750
break;
3851

39-
case is_a($exception, 'Throwable'):
52+
case is_a($exception, \Throwable::class):
4053
/** @var \Throwable $exception */
41-
$handler = new \Plaisio\ExceptionHandler\ThrowableAgent();
54+
$handler = new ThrowableAgent();
4255
$handler->handlePrepareException($exception);
4356
break;
4457
}
@@ -52,39 +65,39 @@ class Foo implements ExceptionHandler
5265
{
5366
switch (true)
5467
{
55-
case is_a($exception, 'Plaisio\Exception\BadRequestException'):
56-
/** @var \Plaisio\Exception\BadRequestException $exception */
57-
$handler = new \Plaisio\ExceptionHandler\BadRequestExceptionAgent();
68+
case is_a($exception, BadRequestException::class):
69+
/** @var BadRequestException $exception */
70+
$handler = new BadRequestExceptionAgent();
5871
$handler->handleConstructException($exception);
5972
break;
6073

61-
case is_a($exception, 'Plaisio\Obfuscator\Exception\DecodeException'):
62-
/** @var \Plaisio\Obfuscator\Exception\DecodeException $exception */
63-
$handler = new \Plaisio\ExceptionHandler\DecodeExceptionAgent();
74+
case is_a($exception, DecodeException::class):
75+
/** @var DecodeException $exception */
76+
$handler = new DecodeExceptionAgent();
6477
$handler->handleConstructException($exception);
6578
break;
6679

67-
case is_a($exception, 'Plaisio\Exception\InvalidUrlException'):
68-
/** @var \Plaisio\Exception\InvalidUrlException $exception */
69-
$handler = new \Plaisio\ExceptionHandler\InvalidUrlExceptionAgent();
80+
case is_a($exception, InvalidUrlException::class):
81+
/** @var InvalidUrlException $exception */
82+
$handler = new InvalidUrlExceptionAgent();
7083
$handler->handleConstructException($exception);
7184
break;
7285

73-
case is_a($exception, 'Plaisio\Exception\NotAuthorizedException'):
74-
/** @var \Plaisio\Exception\NotAuthorizedException $exception */
75-
$handler = new \Plaisio\ExceptionHandler\NotAuthorizedExceptionAgent();
86+
case is_a($exception, NotAuthorizedException::class):
87+
/** @var NotAuthorizedException $exception */
88+
$handler = new NotAuthorizedExceptionAgent();
7689
$handler->handleConstructException($exception);
7790
break;
7891

79-
case is_a($exception, 'SetBased\Stratum\Middle\Exception\ResultException'):
80-
/** @var \SetBased\Stratum\Middle\Exception\ResultException $exception */
81-
$handler = new \Plaisio\ExceptionHandler\ResultExceptionAgent();
92+
case is_a($exception, ResultException::class):
93+
/** @var ResultException $exception */
94+
$handler = new ResultExceptionAgent();
8295
$handler->handleConstructException($exception);
8396
break;
8497

85-
case is_a($exception, 'Throwable'):
98+
case is_a($exception, \Throwable::class):
8699
/** @var \Throwable $exception */
87-
$handler = new \Plaisio\ExceptionHandler\ThrowableAgent();
100+
$handler = new ThrowableAgent();
88101
$handler->handleConstructException($exception);
89102
break;
90103
}
@@ -98,39 +111,39 @@ class Foo implements ExceptionHandler
98111
{
99112
switch (true)
100113
{
101-
case is_a($exception, 'Plaisio\Exception\BadRequestException'):
102-
/** @var \Plaisio\Exception\BadRequestException $exception */
103-
$handler = new \Plaisio\ExceptionHandler\BadRequestExceptionAgent();
114+
case is_a($exception, BadRequestException::class):
115+
/** @var BadRequestException $exception */
116+
$handler = new BadRequestExceptionAgent();
104117
$handler->handleResponseException($exception);
105118
break;
106119

107-
case is_a($exception, 'Plaisio\Obfuscator\Exception\DecodeException'):
108-
/** @var \Plaisio\Obfuscator\Exception\DecodeException $exception */
109-
$handler = new \Plaisio\ExceptionHandler\DecodeExceptionAgent();
120+
case is_a($exception, DecodeException::class):
121+
/** @var DecodeException $exception */
122+
$handler = new DecodeExceptionAgent();
110123
$handler->handleResponseException($exception);
111124
break;
112125

113-
case is_a($exception, 'Plaisio\Exception\InvalidUrlException'):
114-
/** @var \Plaisio\Exception\InvalidUrlException $exception */
115-
$handler = new \Plaisio\ExceptionHandler\InvalidUrlExceptionAgent();
126+
case is_a($exception, InvalidUrlException::class):
127+
/** @var InvalidUrlException $exception */
128+
$handler = new InvalidUrlExceptionAgent();
116129
$handler->handleResponseException($exception);
117130
break;
118131

119-
case is_a($exception, 'Plaisio\Exception\NotAuthorizedException'):
120-
/** @var \Plaisio\Exception\NotAuthorizedException $exception */
121-
$handler = new \Plaisio\ExceptionHandler\NotAuthorizedExceptionAgent();
132+
case is_a($exception, NotAuthorizedException::class):
133+
/** @var NotAuthorizedException $exception */
134+
$handler = new NotAuthorizedExceptionAgent();
122135
$handler->handleResponseException($exception);
123136
break;
124137

125-
case is_a($exception, 'Plaisio\Exception\NotPreferredUrlException'):
126-
/** @var \Plaisio\Exception\NotPreferredUrlException $exception */
127-
$handler = new \Plaisio\ExceptionHandler\NotPreferredUrlExceptionAgent();
138+
case is_a($exception, NotPreferredUrlException::class):
139+
/** @var NotPreferredUrlException $exception */
140+
$handler = new NotPreferredUrlExceptionAgent();
128141
$handler->handleResponseException($exception);
129142
break;
130143

131-
case is_a($exception, 'Throwable'):
144+
case is_a($exception, \Throwable::class):
132145
/** @var \Throwable $exception */
133-
$handler = new \Plaisio\ExceptionHandler\ThrowableAgent();
146+
$handler = new ThrowableAgent();
134147
$handler->handleResponseException($exception);
135148
break;
136149
}
@@ -144,9 +157,9 @@ class Foo implements ExceptionHandler
144157
{
145158
switch (true)
146159
{
147-
case is_a($exception, 'Throwable'):
160+
case is_a($exception, \Throwable::class):
148161
/** @var \Throwable $exception */
149-
$handler = new \Plaisio\ExceptionHandler\ThrowableAgent();
162+
$handler = new ThrowableAgent();
150163
$handler->handleFinalizeException($exception);
151164
break;
152165
}

0 commit comments

Comments
 (0)