From fbfcfef8c7b2c1363dfa73fa4828ecb50cf45e8c Mon Sep 17 00:00:00 2001 From: "Paul M. Jones" Date: Tue, 29 Dec 2015 11:42:50 -0600 Subject: [PATCH] add --tpl custom template support, and better output --- src/Skeleton/Skeleton.php | 52 +++++++++++++++++++++----------- src/Skeleton/SkeletonCommand.php | 22 +++++++++----- src/Skeleton/SkeletonInput.php | 6 +++- 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/Skeleton/Skeleton.php b/src/Skeleton/Skeleton.php index ee41259..8e9bad6 100644 --- a/src/Skeleton/Skeleton.php +++ b/src/Skeleton/Skeleton.php @@ -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() @@ -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) diff --git a/src/Skeleton/SkeletonCommand.php b/src/Skeleton/SkeletonCommand.php index d6ba41b..3390320 100644 --- a/src/Skeleton/SkeletonCommand.php +++ b/src/Skeleton/SkeletonCommand.php @@ -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: * @@ -24,16 +26,19 @@ * --dir= * Write files to this directory instead of the current one. * - * --full - * Additionally create Record, RecordSet, and Plugin classes. - * * --conn= - * 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= - * 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= + * Use custom template files from this directory; fall back to the package + * templates in the "templates/" directory. * */ class SkeletonCommand @@ -71,7 +76,6 @@ public function __invoke() } } - $this->stdio->outln('Done!'); return Status::SUCCESS; } @@ -82,6 +86,7 @@ protected function setGetopt() 'full', 'conn:', 'table:', + 'tpl:', ]; $this->getopt = $this->context->getopt($options); @@ -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() diff --git a/src/Skeleton/SkeletonInput.php b/src/Skeleton/SkeletonInput.php index 8095b0f..efd05a6 100644 --- a/src/Skeleton/SkeletonInput.php +++ b/src/Skeleton/SkeletonInput.php @@ -10,6 +10,7 @@ class SkeletonInput protected $full = false; protected $namespace; protected $table; + protected $tpl; public function __set($key, $val) { @@ -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"); } @@ -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;