Skip to content

Commit 4292e77

Browse files
committed
We can now Override a unique skeleton (#3)
1 parent f69530b commit 4292e77

File tree

3 files changed

+87
-24
lines changed

3 files changed

+87
-24
lines changed

src/Generator/AbstractClassGenerator.php

+69-16
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract class AbstractClassGenerator
3434

3535
private $implements = [];
3636

37-
private $skeletonDirs = null;
37+
private $skeletonDirs = [];
3838

3939
/**
4040
* Number of spaces to use for indention in generated code.
@@ -52,9 +52,9 @@ abstract class AbstractClassGenerator
5252

5353
/**
5454
* @param string $classNamespace The namespace to use for the classes.
55-
* @param string|null $skeletonDirs
55+
* @param string[]|string $skeletonDirs
5656
*/
57-
public function __construct($classNamespace = null, $skeletonDirs = null)
57+
public function __construct($classNamespace = null, $skeletonDirs = [])
5858
{
5959
$this->setClassNamespace($classNamespace);
6060
$this->setSkeletonDirs($skeletonDirs);
@@ -73,20 +73,61 @@ public function setClassNamespace($classNamespace)
7373
return $this;
7474
}
7575

76-
public function setSkeletonDirs($skeletonDirs = null)
76+
/**
77+
* @param string[]|string $skeletonDirs
78+
* @return $this
79+
*/
80+
public function setSkeletonDirs($skeletonDirs)
7781
{
78-
if (null === $skeletonDirs) {
79-
$skeletonDirs = __DIR__ . '/../Resources/skeleton';
82+
$this->skeletonDirs = [];
83+
84+
if (is_string($skeletonDirs)) {
85+
$this->addSkeletonDir($skeletonDirs);
8086
} else {
81-
if (!is_dir($skeletonDirs)) {
82-
throw new \InvalidArgumentException(sprintf('Skeleton dir "%s" not found.', $skeletonDirs));
87+
if (!is_array($skeletonDirs) && !$skeletonDirs instanceof \Traversable) {
88+
throw new \InvalidArgumentException(
89+
sprintf('Skeleton dirs must be array or object implementing \Traversable interface, "%s" given.', gettype($skeletonDirs))
90+
);
8391
}
92+
93+
foreach ($skeletonDirs as $skeletonDir) {
94+
$this->addSkeletonDir($skeletonDir);
95+
}
96+
}
97+
98+
return $this;
99+
}
100+
101+
public function getSkeletonDirs($withDefault = true)
102+
{
103+
$skeletonDirs = $this->skeletonDirs ;
104+
105+
if ($withDefault) {
106+
$skeletonDirs[] = __DIR__ . '/../Resources/skeleton';
84107
}
85-
$this->skeletonDirs = realpath($skeletonDirs);
108+
109+
return $skeletonDirs;
110+
}
111+
112+
public function addSkeletonDir($skeletonDir)
113+
{
114+
if (!is_string($skeletonDir) && !is_object($skeletonDir) && !is_callable($skeletonDir, '__toString')) {
115+
throw new \InvalidArgumentException(
116+
sprintf('Skeleton dir must be string or object implementing __toString, "%s" given.', gettype($skeletonDir))
117+
);
118+
}
119+
120+
$skeletonDir = (string) $skeletonDir;
121+
122+
if (!is_dir($skeletonDir)) {
123+
throw new \InvalidArgumentException(sprintf('Skeleton dir "%s" not found.', $skeletonDir));
124+
}
125+
$this->skeletonDirs[] = realpath($skeletonDir);
86126

87127
return $this;
88128
}
89129

130+
90131
/**
91132
* Sets the number of spaces the exported class should have.
92133
*
@@ -153,21 +194,33 @@ public function clearUseStatements()
153194
return $this;
154195
}
155196

156-
public function getSkeletonContent($skeleton)
197+
public function getSkeletonContent($skeleton, $withDefault = true)
157198
{
158-
$path = $this->skeletonDirs . '/' . $skeleton . static::SKELETON_FILE_PREFIX;
199+
$skeletonDirs = $this->getSkeletonDirs($withDefault);
200+
201+
foreach ($skeletonDirs as $skeletonDir) {
202+
$path = $skeletonDir . '/' . $skeleton . static::SKELETON_FILE_PREFIX;
159203

160-
if (!isset(self::$templates[$path])) {
161204
if (!file_exists($path)) {
162-
throw new \InvalidArgumentException(sprintf('Template "%s" not found.', $path));
205+
continue;
163206
}
164207

165-
$content = trim(file_get_contents($path));
208+
if (!isset(self::$templates[$path])) {
209+
$content = trim(file_get_contents($path));
166210

167-
self::$templates[$path] = $content;
211+
self::$templates[$path] = $content;
212+
}
213+
214+
return self::$templates[$path];
168215
}
169216

170-
return self::$templates[$path];
217+
throw new \InvalidArgumentException(
218+
sprintf(
219+
'Skeleton "%s" could not be found in %s.',
220+
$skeleton,
221+
implode(', ', $skeletonDirs)
222+
)
223+
);
171224
}
172225

173226
protected function addInternalUseStatement($use)

src/Generator/AbstractTypeGenerator.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ abstract class AbstractTypeGenerator extends AbstractClassGenerator
4949
private $expressionLanguage;
5050

5151
/**
52-
* @param string $classNamespace The namespace to use for the classes.
53-
* @param string|null $skeletonDirs
52+
* @param string $classNamespace The namespace to use for the classes.
53+
* @param string[]|string $skeletonDirs
5454
*/
55-
public function __construct($classNamespace = self::DEFAULT_CLASS_NAMESPACE, $skeletonDirs = null)
55+
public function __construct($classNamespace = self::DEFAULT_CLASS_NAMESPACE, $skeletonDirs = [])
5656
{
5757
parent::__construct($classNamespace, $skeletonDirs);
5858
}

tests/Generator/TypeGeneratorTest.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,31 @@ class TypeGeneratorTest extends AbstractTypeGeneratorTest
2222
*/
2323
public function testWrongSetSkeletonDirs()
2424
{
25-
$this->typeGenerator->setSkeletonDirs('fake');
25+
$this->typeGenerator->setSkeletonDirs(['fake']);
2626
}
2727

2828
/**
2929
* @expectedException \InvalidArgumentException
30-
* @expectedExceptionMessage Skeleton dir "fake" not found.
30+
* @expectedExceptionMessage Skeleton dir must be string or object implementing __toString, "array" given.
31+
*/
32+
public function testWrongAddSkeletonDir()
33+
{
34+
$this->typeGenerator->addSkeletonDir([]);
35+
}
36+
37+
/**
38+
* @expectedException \InvalidArgumentException
39+
* @expectedExceptionMessage Skeleton dirs must be array or object implementing \Traversable interface, "object" given.
3140
*/
32-
public function testGoodSetSkeletonDirs()
41+
public function testWrongObjectSetSkeletonDir()
3342
{
34-
$this->typeGenerator->setSkeletonDirs('fake');
43+
$this->typeGenerator->setSkeletonDirs(new \stdClass());
3544
}
3645

46+
3747
/**
3848
* @expectedException \InvalidArgumentException
39-
* @expectedExceptionMessageRegExp /Template ".*fake.php.skeleton" not found./
49+
* @expectedExceptionMessageRegExp /Skeleton "fake" could not be found in .*\/skeleton./
4050
*/
4151
public function testWrongGetSkeletonDirs()
4252
{

0 commit comments

Comments
 (0)