From 4292e77c39366705b2959c1ca2476abc583a26eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9miah=20VALERIE?= Date: Fri, 29 Apr 2016 17:16:53 +0200 Subject: [PATCH] We can now Override a unique skeleton (#3) --- src/Generator/AbstractClassGenerator.php | 85 +++++++++++++++++++----- src/Generator/AbstractTypeGenerator.php | 6 +- tests/Generator/TypeGeneratorTest.php | 20 ++++-- 3 files changed, 87 insertions(+), 24 deletions(-) diff --git a/src/Generator/AbstractClassGenerator.php b/src/Generator/AbstractClassGenerator.php index 4e9876b..612b7f8 100644 --- a/src/Generator/AbstractClassGenerator.php +++ b/src/Generator/AbstractClassGenerator.php @@ -34,7 +34,7 @@ abstract class AbstractClassGenerator private $implements = []; - private $skeletonDirs = null; + private $skeletonDirs = []; /** * Number of spaces to use for indention in generated code. @@ -52,9 +52,9 @@ abstract class AbstractClassGenerator /** * @param string $classNamespace The namespace to use for the classes. - * @param string|null $skeletonDirs + * @param string[]|string $skeletonDirs */ - public function __construct($classNamespace = null, $skeletonDirs = null) + public function __construct($classNamespace = null, $skeletonDirs = []) { $this->setClassNamespace($classNamespace); $this->setSkeletonDirs($skeletonDirs); @@ -73,20 +73,61 @@ public function setClassNamespace($classNamespace) return $this; } - public function setSkeletonDirs($skeletonDirs = null) + /** + * @param string[]|string $skeletonDirs + * @return $this + */ + public function setSkeletonDirs($skeletonDirs) { - if (null === $skeletonDirs) { - $skeletonDirs = __DIR__ . '/../Resources/skeleton'; + $this->skeletonDirs = []; + + if (is_string($skeletonDirs)) { + $this->addSkeletonDir($skeletonDirs); } else { - if (!is_dir($skeletonDirs)) { - throw new \InvalidArgumentException(sprintf('Skeleton dir "%s" not found.', $skeletonDirs)); + if (!is_array($skeletonDirs) && !$skeletonDirs instanceof \Traversable) { + throw new \InvalidArgumentException( + sprintf('Skeleton dirs must be array or object implementing \Traversable interface, "%s" given.', gettype($skeletonDirs)) + ); } + + foreach ($skeletonDirs as $skeletonDir) { + $this->addSkeletonDir($skeletonDir); + } + } + + return $this; + } + + public function getSkeletonDirs($withDefault = true) + { + $skeletonDirs = $this->skeletonDirs ; + + if ($withDefault) { + $skeletonDirs[] = __DIR__ . '/../Resources/skeleton'; } - $this->skeletonDirs = realpath($skeletonDirs); + + return $skeletonDirs; + } + + public function addSkeletonDir($skeletonDir) + { + if (!is_string($skeletonDir) && !is_object($skeletonDir) && !is_callable($skeletonDir, '__toString')) { + throw new \InvalidArgumentException( + sprintf('Skeleton dir must be string or object implementing __toString, "%s" given.', gettype($skeletonDir)) + ); + } + + $skeletonDir = (string) $skeletonDir; + + if (!is_dir($skeletonDir)) { + throw new \InvalidArgumentException(sprintf('Skeleton dir "%s" not found.', $skeletonDir)); + } + $this->skeletonDirs[] = realpath($skeletonDir); return $this; } + /** * Sets the number of spaces the exported class should have. * @@ -153,21 +194,33 @@ public function clearUseStatements() return $this; } - public function getSkeletonContent($skeleton) + public function getSkeletonContent($skeleton, $withDefault = true) { - $path = $this->skeletonDirs . '/' . $skeleton . static::SKELETON_FILE_PREFIX; + $skeletonDirs = $this->getSkeletonDirs($withDefault); + + foreach ($skeletonDirs as $skeletonDir) { + $path = $skeletonDir . '/' . $skeleton . static::SKELETON_FILE_PREFIX; - if (!isset(self::$templates[$path])) { if (!file_exists($path)) { - throw new \InvalidArgumentException(sprintf('Template "%s" not found.', $path)); + continue; } - $content = trim(file_get_contents($path)); + if (!isset(self::$templates[$path])) { + $content = trim(file_get_contents($path)); - self::$templates[$path] = $content; + self::$templates[$path] = $content; + } + + return self::$templates[$path]; } - return self::$templates[$path]; + throw new \InvalidArgumentException( + sprintf( + 'Skeleton "%s" could not be found in %s.', + $skeleton, + implode(', ', $skeletonDirs) + ) + ); } protected function addInternalUseStatement($use) diff --git a/src/Generator/AbstractTypeGenerator.php b/src/Generator/AbstractTypeGenerator.php index 670156f..37ea21c 100644 --- a/src/Generator/AbstractTypeGenerator.php +++ b/src/Generator/AbstractTypeGenerator.php @@ -49,10 +49,10 @@ abstract class AbstractTypeGenerator extends AbstractClassGenerator private $expressionLanguage; /** - * @param string $classNamespace The namespace to use for the classes. - * @param string|null $skeletonDirs + * @param string $classNamespace The namespace to use for the classes. + * @param string[]|string $skeletonDirs */ - public function __construct($classNamespace = self::DEFAULT_CLASS_NAMESPACE, $skeletonDirs = null) + public function __construct($classNamespace = self::DEFAULT_CLASS_NAMESPACE, $skeletonDirs = []) { parent::__construct($classNamespace, $skeletonDirs); } diff --git a/tests/Generator/TypeGeneratorTest.php b/tests/Generator/TypeGeneratorTest.php index 2acea40..d19ec39 100644 --- a/tests/Generator/TypeGeneratorTest.php +++ b/tests/Generator/TypeGeneratorTest.php @@ -22,21 +22,31 @@ class TypeGeneratorTest extends AbstractTypeGeneratorTest */ public function testWrongSetSkeletonDirs() { - $this->typeGenerator->setSkeletonDirs('fake'); + $this->typeGenerator->setSkeletonDirs(['fake']); } /** * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Skeleton dir "fake" not found. + * @expectedExceptionMessage Skeleton dir must be string or object implementing __toString, "array" given. + */ + public function testWrongAddSkeletonDir() + { + $this->typeGenerator->addSkeletonDir([]); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Skeleton dirs must be array or object implementing \Traversable interface, "object" given. */ - public function testGoodSetSkeletonDirs() + public function testWrongObjectSetSkeletonDir() { - $this->typeGenerator->setSkeletonDirs('fake'); + $this->typeGenerator->setSkeletonDirs(new \stdClass()); } + /** * @expectedException \InvalidArgumentException - * @expectedExceptionMessageRegExp /Template ".*fake.php.skeleton" not found./ + * @expectedExceptionMessageRegExp /Skeleton "fake" could not be found in .*\/skeleton./ */ public function testWrongGetSkeletonDirs() {