Skip to content

Commit caf3a8d

Browse files
committed
Fix class consolidation for classes in different namespaces
Test output messages
1 parent 05542f3 commit caf3a8d

File tree

10 files changed

+532
-387
lines changed

10 files changed

+532
-387
lines changed

src/Model/RenderJob.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ public function render(string $destination, GeneratorConfiguration $generatorCon
7373
}
7474

7575
if ($generatorConfiguration->isOutputEnabled()) {
76-
// @codeCoverageIgnoreStart
77-
echo "Rendered class $this->className\n";
78-
// @codeCoverageIgnoreEnd
76+
echo "Rendered class {$generatorConfiguration->getNamespacePrefix()}$this->classPath\\$this->className\n";
7977
}
8078
}
8179

@@ -110,13 +108,19 @@ protected function generateModelDirectory(string $destination, string $classPath
110108
*/
111109
protected function renderClass(GeneratorConfiguration $generatorConfiguration): string
112110
{
113-
$render = new Render(__DIR__ . "/../Templates/");
111+
$getFullNamespace = function (string $classPath) use ($generatorConfiguration): string {
112+
return trim($generatorConfiguration->getNamespacePrefix() . $classPath, '\\');
113+
};
114114

115-
$namespace = trim($generatorConfiguration->getNamespacePrefix() . $this->classPath, '\\');
116-
117-
$use = $generatorConfiguration->collectErrors()
118-
? [$generatorConfiguration->getErrorRegistryClass()]
119-
: [$generatorConfiguration->getExceptionClass()];
115+
$render = new Render(__DIR__ . "/../Templates/");
116+
$namespace = $getFullNamespace($this->classPath);
117+
118+
$use = array_merge(
119+
array_map($getFullNamespace, $this->schema->getUsedClasses()),
120+
$generatorConfiguration->collectErrors()
121+
? [$generatorConfiguration->getErrorRegistryClass()]
122+
: [$generatorConfiguration->getExceptionClass()]
123+
);
120124

121125
if ($namespace) {
122126
$use[] = Throwable::class;

src/Model/Schema.php

Lines changed: 139 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,139 @@
1-
<?php
2-
3-
declare(strict_types = 1);
4-
5-
namespace PHPModelGenerator\Model;
6-
7-
use Exception;
8-
use PHPModelGenerator\Exception\SchemaException;
9-
use PHPModelGenerator\Model\Property\Property;
10-
use PHPModelGenerator\Model\Property\PropertyInterface;
11-
use PHPModelGenerator\Model\SchemaDefinition\SchemaDefinitionDictionary;
12-
use PHPModelGenerator\Model\Validator\PropertyValidator;
13-
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
14-
15-
/**
16-
* Class Schema
17-
*
18-
* @package PHPModelGenerator\Model
19-
*/
20-
class Schema
21-
{
22-
/** @var string */
23-
protected $className;
24-
/** @var Property[] The properties which are part of the class */
25-
protected $properties = [];
26-
/** @var PropertyValidator[] A Collection of validators which must be applied
27-
* before adding properties to the model
28-
*/
29-
protected $baseValidators = [];
30-
/** @var SchemaDefinitionDictionary */
31-
protected $schemaDefinitionDictionary;
32-
33-
/**
34-
* Schema constructor.
35-
*
36-
* @param string $className
37-
* @param SchemaDefinitionDictionary $dictionary
38-
*/
39-
public function __construct(string $className, SchemaDefinitionDictionary $dictionary = null)
40-
{
41-
$this->className = $className;
42-
$this->schemaDefinitionDictionary = $dictionary ?? new SchemaDefinitionDictionary('');
43-
}
44-
45-
/**
46-
* @return string
47-
*/
48-
public function getClassName(): string
49-
{
50-
return $this->className;
51-
}
52-
53-
/**
54-
* @return PropertyInterface[]
55-
*/
56-
public function getProperties(): array
57-
{
58-
return $this->properties;
59-
}
60-
61-
/**
62-
* @param PropertyInterface $property
63-
*
64-
* @return $this
65-
*/
66-
public function addProperty(PropertyInterface $property): self
67-
{
68-
if (!isset($this->properties[$property->getName()])) {
69-
$this->properties[$property->getName()] = $property;
70-
}
71-
72-
return $this;
73-
}
74-
75-
/**
76-
* @return PropertyValidator[]
77-
*/
78-
public function getBaseValidators(): array
79-
{
80-
return $this->baseValidators;
81-
}
82-
83-
/**
84-
* @param PropertyValidatorInterface $baseValidator
85-
*
86-
* @return $this
87-
*/
88-
public function addBaseValidator(PropertyValidatorInterface $baseValidator)
89-
{
90-
$this->baseValidators[] = $baseValidator;
91-
92-
return $this;
93-
}
94-
95-
/**
96-
* @return SchemaDefinitionDictionary
97-
*/
98-
public function getSchemaDictionary(): SchemaDefinitionDictionary
99-
{
100-
return $this->schemaDefinitionDictionary;
101-
}
102-
}
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Model;
6+
7+
use Exception;
8+
use PHPModelGenerator\Exception\SchemaException;
9+
use PHPModelGenerator\Model\Property\Property;
10+
use PHPModelGenerator\Model\Property\PropertyInterface;
11+
use PHPModelGenerator\Model\SchemaDefinition\SchemaDefinitionDictionary;
12+
use PHPModelGenerator\Model\Validator\PropertyValidator;
13+
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
14+
15+
/**
16+
* Class Schema
17+
*
18+
* @package PHPModelGenerator\Model
19+
*/
20+
class Schema
21+
{
22+
/** @var string */
23+
protected $className;
24+
/** @var string */
25+
protected $classPath;
26+
/** @var Property[] The properties which are part of the class */
27+
protected $properties = [];
28+
/** @var PropertyValidator[] A Collection of validators which must be applied
29+
* before adding properties to the model
30+
*/
31+
protected $baseValidators = [];
32+
/** @var array */
33+
protected $usedClasses = [];
34+
35+
/** @var SchemaDefinitionDictionary */
36+
protected $schemaDefinitionDictionary;
37+
38+
/**
39+
* Schema constructor.
40+
*
41+
* @param string $classPath
42+
* @param string $className
43+
* @param SchemaDefinitionDictionary $dictionary
44+
*/
45+
public function __construct(string $classPath, string $className, SchemaDefinitionDictionary $dictionary = null)
46+
{
47+
$this->className = $className;
48+
$this->classPath = $classPath;
49+
$this->schemaDefinitionDictionary = $dictionary ?? new SchemaDefinitionDictionary('');
50+
}
51+
52+
/**
53+
* @return string
54+
*/
55+
public function getClassName(): string
56+
{
57+
return $this->className;
58+
}
59+
60+
/**
61+
* @return string
62+
*/
63+
public function getClassPath(): string
64+
{
65+
return $this->classPath;
66+
}
67+
68+
/**
69+
* @return PropertyInterface[]
70+
*/
71+
public function getProperties(): array
72+
{
73+
return $this->properties;
74+
}
75+
76+
/**
77+
* @param PropertyInterface $property
78+
*
79+
* @return $this
80+
*/
81+
public function addProperty(PropertyInterface $property): self
82+
{
83+
if (!isset($this->properties[$property->getName()])) {
84+
$this->properties[$property->getName()] = $property;
85+
}
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* @return PropertyValidator[]
92+
*/
93+
public function getBaseValidators(): array
94+
{
95+
return $this->baseValidators;
96+
}
97+
98+
/**
99+
* @param PropertyValidatorInterface $baseValidator
100+
*
101+
* @return $this
102+
*/
103+
public function addBaseValidator(PropertyValidatorInterface $baseValidator)
104+
{
105+
$this->baseValidators[] = $baseValidator;
106+
107+
return $this;
108+
}
109+
110+
/**
111+
* @return SchemaDefinitionDictionary
112+
*/
113+
public function getSchemaDictionary(): SchemaDefinitionDictionary
114+
{
115+
return $this->schemaDefinitionDictionary;
116+
}
117+
118+
/**
119+
* Add a class to the schema which is required
120+
*
121+
* @param string $path
122+
*
123+
* @return $this
124+
*/
125+
public function addUsedClass(string $path): self
126+
{
127+
$this->usedClasses[] = $path;
128+
129+
return $this;
130+
}
131+
132+
/**
133+
* @return array
134+
*/
135+
public function getUsedClasses(): array
136+
{
137+
return $this->usedClasses;
138+
}
139+
}

0 commit comments

Comments
 (0)