Skip to content

Commit 05542f3

Browse files
committed
Remove exception parameter from initial classes
Tests for empty compositions Show a warning when handling an empty composition
1 parent 402ea9e commit 05542f3

File tree

12 files changed

+969
-748
lines changed

12 files changed

+969
-748
lines changed

src/Model/RenderJob.php

Lines changed: 144 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,145 @@
1-
<?php
2-
3-
declare(strict_types = 1);
4-
5-
namespace PHPModelGenerator\Model;
6-
7-
use PHPMicroTemplate\Exception\PHPMicroTemplateException;
8-
use PHPMicroTemplate\Render;
9-
use PHPModelGenerator\Exception\FileSystemException;
10-
use PHPModelGenerator\Exception\RenderException;
11-
use PHPModelGenerator\Utils\RenderHelper;
12-
use Throwable;
13-
14-
class RenderJob
15-
{
16-
/** @var Schema */
17-
protected $schema;
18-
/** @var string */
19-
protected $className;
20-
/** @var string */
21-
protected $classPath;
22-
/** @var string */
23-
protected $fileName;
24-
25-
/**
26-
* Create a new class render job
27-
*
28-
* @param string $fileName The file name
29-
* @param string $classPath The relative path of the class for namespace generation
30-
* @param string $className The class name
31-
* @param Schema $schema The Schema object which holds properties and validators
32-
*/
33-
public function __construct(string $fileName, string $classPath, string $className, Schema $schema)
34-
{
35-
$this->fileName = $fileName;
36-
$this->classPath = $classPath;
37-
$this->className = $className;
38-
$this->schema = $schema;
39-
}
40-
41-
/**
42-
* Execute the render job and render the class
43-
*
44-
* @param string $destination
45-
* @param GeneratorConfiguration $generatorConfiguration
46-
*
47-
* @throws FileSystemException
48-
* @throws RenderException
49-
*/
50-
public function render(string $destination, GeneratorConfiguration $generatorConfiguration): void
51-
{
52-
$this->generateModelDirectory($destination, $this->classPath);
53-
54-
$class = $this->renderClass($generatorConfiguration);
55-
56-
if (file_exists($this->fileName)) {
57-
throw new FileSystemException("File {$this->fileName} already exists. Make sure object IDs are unique.");
58-
}
59-
60-
if (!file_put_contents($this->fileName, $class)) {
61-
// @codeCoverageIgnoreStart
62-
throw new FileSystemException("Can't write class $this->classPath\\$this->className.");
63-
// @codeCoverageIgnoreEnd
64-
}
65-
66-
if ($generatorConfiguration->isOutputEnabled()) {
67-
// @codeCoverageIgnoreStart
68-
echo "Rendered class $this->className\n";
69-
// @codeCoverageIgnoreEnd
70-
}
71-
}
72-
73-
/**
74-
* Generate the directory structure for saving a generated class
75-
*
76-
* @param string $destination
77-
* @param string $classPath
78-
*
79-
* @throws FileSystemException
80-
*/
81-
protected function generateModelDirectory(string $destination, string $classPath): void
82-
{
83-
$subDirectoryPath = '';
84-
foreach (explode('\\', $classPath) as $directory) {
85-
$subDirectoryPath .= "/$directory";
86-
$fullPath = $destination . $subDirectoryPath;
87-
88-
if (!is_dir($fullPath) && !mkdir($fullPath)) {
89-
throw new FileSystemException("Can't create path $fullPath");
90-
}
91-
}
92-
}
93-
94-
/**
95-
* Render a class. Returns the php code of the class
96-
*
97-
* @param GeneratorConfiguration $generatorConfiguration
98-
*
99-
* @return string
100-
* @throws RenderException
101-
*/
102-
protected function renderClass(GeneratorConfiguration $generatorConfiguration): string
103-
{
104-
$render = new Render(__DIR__ . "/../Templates/");
105-
106-
$namespace = trim($generatorConfiguration->getNamespacePrefix() . $this->classPath, '\\');
107-
108-
$use = $generatorConfiguration->collectErrors()
109-
? [$generatorConfiguration->getErrorRegistryClass()]
110-
: [$generatorConfiguration->getExceptionClass()];
111-
112-
if ($namespace) {
113-
$use[] = Throwable::class;
114-
}
115-
116-
try {
117-
$class = $render->renderTemplate(
118-
'Model.phptpl',
119-
[
120-
'namespace' => $namespace,
121-
'use' => empty($use) ? '' : 'use ' . join(";\nuse ", array_unique($use)) . ';',
122-
'class' => $this->className,
123-
'baseValidators' => $this->schema->getBaseValidators(),
124-
'properties' => $this->schema->getProperties(),
125-
'generatorConfiguration' => $generatorConfiguration,
126-
'viewHelper' => new RenderHelper($generatorConfiguration),
127-
]
128-
);
129-
} catch (PHPMicroTemplateException $exception) {
130-
throw new RenderException("Can't render class $this->classPath\\$this->className", 0, $exception);
131-
}
132-
133-
return $class;
134-
}
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Model;
6+
7+
use PHPMicroTemplate\Exception\PHPMicroTemplateException;
8+
use PHPMicroTemplate\Render;
9+
use PHPModelGenerator\Exception\FileSystemException;
10+
use PHPModelGenerator\Exception\RenderException;
11+
use PHPModelGenerator\Utils\RenderHelper;
12+
use Throwable;
13+
14+
class RenderJob
15+
{
16+
/** @var Schema */
17+
protected $schema;
18+
/** @var string */
19+
protected $className;
20+
/** @var string */
21+
protected $classPath;
22+
/** @var string */
23+
protected $fileName;
24+
/** @var bool */
25+
protected $initialClass;
26+
27+
/**
28+
* Create a new class render job
29+
*
30+
* @param string $fileName The file name
31+
* @param string $classPath The relative path of the class for namespace generation
32+
* @param string $className The class name
33+
* @param Schema $schema The Schema object which holds properties and validators
34+
* @param bool $initialClass Render job for an initial class or render job for a nested class?
35+
*/
36+
public function __construct(
37+
string $fileName,
38+
string $classPath,
39+
string $className,
40+
Schema $schema,
41+
bool $initialClass
42+
) {
43+
$this->fileName = $fileName;
44+
$this->classPath = $classPath;
45+
$this->className = $className;
46+
$this->schema = $schema;
47+
$this->initialClass = $initialClass;
48+
}
49+
50+
/**
51+
* Execute the render job and render the class
52+
*
53+
* @param string $destination
54+
* @param GeneratorConfiguration $generatorConfiguration
55+
*
56+
* @throws FileSystemException
57+
* @throws RenderException
58+
*/
59+
public function render(string $destination, GeneratorConfiguration $generatorConfiguration): void
60+
{
61+
$this->generateModelDirectory($destination, $this->classPath);
62+
63+
$class = $this->renderClass($generatorConfiguration);
64+
65+
if (file_exists($this->fileName)) {
66+
throw new FileSystemException("File {$this->fileName} already exists. Make sure object IDs are unique.");
67+
}
68+
69+
if (!file_put_contents($this->fileName, $class)) {
70+
// @codeCoverageIgnoreStart
71+
throw new FileSystemException("Can't write class $this->classPath\\$this->className.");
72+
// @codeCoverageIgnoreEnd
73+
}
74+
75+
if ($generatorConfiguration->isOutputEnabled()) {
76+
// @codeCoverageIgnoreStart
77+
echo "Rendered class $this->className\n";
78+
// @codeCoverageIgnoreEnd
79+
}
80+
}
81+
82+
/**
83+
* Generate the directory structure for saving a generated class
84+
*
85+
* @param string $destination
86+
* @param string $classPath
87+
*
88+
* @throws FileSystemException
89+
*/
90+
protected function generateModelDirectory(string $destination, string $classPath): void
91+
{
92+
$subDirectoryPath = '';
93+
foreach (explode('\\', $classPath) as $directory) {
94+
$subDirectoryPath .= "/$directory";
95+
$fullPath = $destination . $subDirectoryPath;
96+
97+
if (!is_dir($fullPath) && !mkdir($fullPath)) {
98+
throw new FileSystemException("Can't create path $fullPath");
99+
}
100+
}
101+
}
102+
103+
/**
104+
* Render a class. Returns the php code of the class
105+
*
106+
* @param GeneratorConfiguration $generatorConfiguration
107+
*
108+
* @return string
109+
* @throws RenderException
110+
*/
111+
protected function renderClass(GeneratorConfiguration $generatorConfiguration): string
112+
{
113+
$render = new Render(__DIR__ . "/../Templates/");
114+
115+
$namespace = trim($generatorConfiguration->getNamespacePrefix() . $this->classPath, '\\');
116+
117+
$use = $generatorConfiguration->collectErrors()
118+
? [$generatorConfiguration->getErrorRegistryClass()]
119+
: [$generatorConfiguration->getExceptionClass()];
120+
121+
if ($namespace) {
122+
$use[] = Throwable::class;
123+
}
124+
125+
try {
126+
$class = $render->renderTemplate(
127+
'Model.phptpl',
128+
[
129+
'namespace' => $namespace,
130+
'use' => empty($use) ? '' : 'use ' . join(";\nuse ", array_unique($use)) . ';',
131+
'class' => $this->className,
132+
'baseValidators' => $this->schema->getBaseValidators(),
133+
'properties' => $this->schema->getProperties(),
134+
'generatorConfiguration' => $generatorConfiguration,
135+
'viewHelper' => new RenderHelper($generatorConfiguration),
136+
'initialClass' => $this->initialClass,
137+
]
138+
);
139+
} catch (PHPMicroTemplateException $exception) {
140+
throw new RenderException("Can't render class $this->classPath\\$this->className", 0, $exception);
141+
}
142+
143+
return $class;
144+
}
135145
}

0 commit comments

Comments
 (0)