Skip to content

Commit

Permalink
We can now Override a unique skeleton (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcg-web committed Apr 29, 2016
1 parent f69530b commit 4292e77
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 24 deletions.
85 changes: 69 additions & 16 deletions src/Generator/AbstractClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class AbstractClassGenerator

private $implements = [];

private $skeletonDirs = null;
private $skeletonDirs = [];

/**
* Number of spaces to use for indention in generated code.
Expand All @@ -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);
Expand All @@ -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.
*
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/Generator/AbstractTypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
20 changes: 15 additions & 5 deletions tests/Generator/TypeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 4292e77

Please sign in to comment.