Skip to content

Commit

Permalink
add --tpl custom template support, and better output
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul M. Jones committed Dec 29, 2015
1 parent a7fb7f9 commit fbfcfef
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
52 changes: 35 additions & 17 deletions src/Skeleton/Skeleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,6 @@ protected function setSubdir()
$this->subdir =
$this->input->dir . DIRECTORY_SEPARATOR .
$this->type . DIRECTORY_SEPARATOR;

if ($this->fsio->isDir($this->subdir)) {
$this->logger->info(" Skipped: mkdir {$this->subdir}");
return;
}

try {
$this->fsio->mkdir($this->subdir, 0755, true);
} catch (Exception $e) {
$this->logger->error("-Failure: mkdir {$this->subdir}");
return Status::CANTCREAT;
}

$this->logger->info("+Success: mkdir {$this->subdir}");
}

protected function setVars()
Expand Down Expand Up @@ -201,18 +187,50 @@ protected function setTemplates()
$classes[] = 'RecordSet';
}

$dir = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'templates';
// look in custom template dir first, then default location
$dirs = [];
if ($this->input->tpl) {
$dirs[] = $this->input->tpl;
}
$dirs[] = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'templates';

foreach ($classes as $class) {
$file = $dir. DIRECTORY_SEPARATOR . $class . '.tpl';
$this->templates[$class] = $this->fsio->get($file);
foreach ($dirs as $dir) {
$file = $dir. DIRECTORY_SEPARATOR . $class . '.tpl';
if ($this->fsio->isFile($file)) {
$this->templates[$class] = $this->fsio->get($file);
}
}
}
}

protected function createClasses()
{
$this->logger->info("Generating skeleton data source classes.");
$this->logger->info("Namespace: " . $this->input->namespace);
$this->logger->info("Directory: " . $this->input->dir);
$this->mkSubDir();
foreach ($this->templates as $class => $template) {
$this->createClass($class, $template);
}
$this->logger->info("Done!");
}

protected function mkSubDir()
{
if ($this->fsio->isDir($this->subdir)) {
$this->logger->info(" Skipped: mkdir {$this->subdir}");
return;
}

try {
$this->fsio->mkdir($this->subdir, 0755, true);
} catch (Exception $e) {
$this->logger->error("-Failure: mkdir {$this->subdir}");
return Status::CANTCREAT;
}

$this->logger->info("+Success: mkdir {$this->subdir}");
}

protected function createClass($class, $template)
Expand Down
22 changes: 14 additions & 8 deletions src/Skeleton/SkeletonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
*
* Description:
*
* Creates skeleton data-source classes.
* Creates skeleton data source classes. By default it creates only a Mapper
* class, but first-time creation should include `--conn` and `--table` to
* create a Table class from the database table description.
*
* Usage:
*
Expand All @@ -24,16 +26,19 @@
* --dir=<value>
* Write files to this directory instead of the current one.
*
* --full
* Additionally create Record, RecordSet, and Plugin classes.
*
* --conn=<value>
* Connect to the database and create, or overwrite, a Table class.
* Connect to the database and overwrite the existing Table class.
* Must also pass a --table value.
*
* --table=<value>
* Use the specified table name instead of determining from the type name.
* Must also pass a --conn value.
* Read this table from the database. Must also pass a --conn value.
*
* --full
* Additionally create Record, RecordSet, and Plugin classes.
*
* --tpl=<value>
* Use custom template files from this directory; fall back to the package
* templates in the "templates/" directory.
*
*/
class SkeletonCommand
Expand Down Expand Up @@ -71,7 +76,6 @@ public function __invoke()
}
}

$this->stdio->outln('Done!');
return Status::SUCCESS;
}

Expand All @@ -82,6 +86,7 @@ protected function setGetopt()
'full',
'conn:',
'table:',
'tpl:',
];
$this->getopt = $this->context->getopt($options);

Expand All @@ -105,6 +110,7 @@ protected function setInput()
$this->input->namespace = $this->getopt->get(1);
$this->input->conn = $this->getConn();
$this->input->table = $this->getopt->get('--table');
$this->input->tpl = $this->getopt->get('--tpl');
}

protected function getConn()
Expand Down
6 changes: 5 additions & 1 deletion src/Skeleton/SkeletonInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SkeletonInput
protected $full = false;
protected $namespace;
protected $table;
protected $tpl;

public function __set($key, $val)
{
Expand All @@ -29,6 +30,9 @@ public function __set($key, $val)
case 'table':
$val = trim($val);
break;
case 'tpl':
$val = rtrim(trim($val), DIRECTORY_SEPARATOR);
break;
default:
throw new Exception("No such property: $key");
}
Expand All @@ -39,7 +43,7 @@ public function __set($key, $val)
public function __get($key)
{
if (! property_exists($this, $key)) {
throw new Exception();
throw new Exception("No such property: $key");
}

return $this->$key;
Expand Down

0 comments on commit fbfcfef

Please sign in to comment.