From dd55263142bdbe9c5007987ffa3b50ecd2908c76 Mon Sep 17 00:00:00 2001 From: Robert Hafner Date: Fri, 27 Jun 2014 13:35:43 -0700 Subject: [PATCH] Moved build logic to Build class --- src/Spark/Builder.php | 91 +++++++++++++++++++++++++++++ src/Spark/Commands/Create.php | 107 ++++++---------------------------- 2 files changed, 108 insertions(+), 90 deletions(-) create mode 100644 src/Spark/Builder.php diff --git a/src/Spark/Builder.php b/src/Spark/Builder.php new file mode 100644 index 0000000..30aad32 --- /dev/null +++ b/src/Spark/Builder.php @@ -0,0 +1,91 @@ +plugins = $plugins; + $this->pluginPath = $pluginsPath; + } + + public function build($outputDirectory, $tags) + { + $this->getTemplateFiles($tags); + $this->makeDirectories($outputDirectory); + $this->makeFiles($outputDirectory, $tags); + } + + protected function makeDirectories($base) + { + if (!is_dir($base)) { + mkdir($base); + } + + foreach ($this->outputDirectories as $directory) { + $newDir = $base . $directory; + if (!is_dir($newDir)) { + mkdir($newDir); + } + } + } + + protected function makeFiles($base, $tags) + { + $paths = $this->templateSourceDirectories; + + $twigFilesystem = new \Twig_Loader_Filesystem(array_reverse($paths)); + $twigEnvironment = new \Twig_Environment($twigFilesystem); + + foreach ($this->outputFiles as $file) { + $newFile = $base . $file; + + if (!file_exists($newFile)) { + $contents = $twigEnvironment->render($file, $tags); + file_put_contents($newFile, $contents); + } + } + + } + + protected function getTemplateFiles($tags) + { + $name = isset($tags['name']) ? $tags['name'] : 'PROJECTNAME'; + + $plugins = $this->plugins; + $pluginPath = $this->pluginPath; + + foreach ($plugins as $plugin) { + $path = $pluginPath . $plugin; + $this->templateSourceDirectories[] = $path; + $pathLen = strlen($path); + + $finder = new Finder(); + $finder->in($path)->ignoreVCS(false)->notName('.gitkeep')->ignoreDotFiles(false); + + foreach ($finder as $file) { + $longPath = $file->getRealpath(); + $processedPath = str_replace('PROJECTNAME', $name, $longPath); + $shortPath = substr($processedPath, $pathLen + 1); + if ($file->isDir()) { + $this->outputDirectories[] = $shortPath; + } else { + $this->outputFiles[] = $shortPath; + } + } + } + } + +} diff --git a/src/Spark/Commands/Create.php b/src/Spark/Commands/Create.php index b06fff6..22d8095 100644 --- a/src/Spark/Commands/Create.php +++ b/src/Spark/Commands/Create.php @@ -3,9 +3,9 @@ namespace Spark\Commands; use Spark\Resources; +use Spark\Builder; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Finder\Finder; class Create extends Base { @@ -14,29 +14,6 @@ class Create extends Base EOT; protected function execute(InputInterface $input, OutputInterface $output) - { - $input = $this->getInput($input); - $name = $input['name']; - $type = $input['type']; - $dir = $input['dir']; - $plugins = $input['plugins']; - $configPath = $input['configPath']; - $vendor = $input['vendor']; - $templatePath = $input['templatePath']; - $templateFiles = $this->getTemplateFiles($name, $plugins, $templatePath); - - $tags = array( - 'name' => $name, - 'vendor' => $vendor - ); - - $this->makeDirectories($dir, $templateFiles['directories']); - $this->makeFiles($dir, $templateFiles['paths'], $templateFiles['files'], $tags); - - $output->writeln($name . ' has been created using the ' . $type . ' package.'); - } - - protected function getInput(InputInterface $input) { $name = $input->getArgument('name'); $type = $input->getArgument('type'); @@ -53,84 +30,34 @@ protected function getInput(InputInterface $input) $vendor = 'VENDOR'; } + $plugins = $this->getPlugins($type); + $resources = new Resources(); - $configPath = $resources->getPath('config'); $templatePath = $resources->getPath('templates'); - $packages = json_decode(file_get_contents($configPath . 'packages.json'), true); - - if (!isset($packages[$type])) { - throw new \RuntimeException('Not a supported type.'); - } - - $options['name'] = $name; - $options['type'] = $type; - $options['dir'] = $dir; - $options['plugins'] = $packages[$type]; - $options['configPath'] = $configPath; - $options['templatePath'] = $templatePath; - $options['vendor'] = $vendor; - - return $options; - } + $tags = array( + 'name' => $name, + 'vendor' => $vendor + ); - protected function makeDirectories($base, $directories) - { - if (!is_dir($base)) { - mkdir($base); - } + $builder = new Builder($plugins, $templatePath); + $builder->build($dir, $tags); - foreach ($directories as $directory) { - $newDir = $base . $directory; - if (!is_dir($newDir)) { - mkdir($newDir); - } - } + $output->writeln($name . ' has been created using the ' . $type . ' package.'); } - protected function makeFiles($base, $paths, $files, $tags) + protected function getPlugins($type) { - $twigFilesystem = new \Twig_Loader_Filesystem(array_reverse($paths)); - $twigEnvironment = new \Twig_Environment($twigFilesystem); - - foreach ($files as $file) { - $newFile = $base . $file; - - if (!file_exists($newFile)) { - $contents = $twigEnvironment->render($file, $tags); - file_put_contents($newFile, $contents); - } - } + $resources = new Resources(); + $configPath = $resources->getPath('config'); - } + $packages = json_decode(file_get_contents($configPath . 'packages.json'), true); - protected function getTemplateFiles($name, $plugins, $templatePath) - { - $paths = array(); - $files = array(); - $directories = array(); - - foreach ($plugins as $plugin) { - $path = $templatePath . $plugin; - $paths[] = $path; - $pathLen = strlen($path); - - $finder = new Finder(); - $finder->in($path)->ignoreVCS(false)->notName('.gitkeep')->ignoreDotFiles(false); - - foreach ($finder as $file) { - $longPath = $file->getRealpath(); - $processedPath = str_replace('PROJECTNAME', $name, $longPath); - $shortPath = substr($processedPath, $pathLen + 1); - if ($file->isDir()) { - $directories[] = $shortPath; - } else { - $files[] = $shortPath; - } - } + if (!isset($packages[$type])) { + throw new \RuntimeException('Not a supported type.'); } - return array('directories' => $directories, 'files' => $files, 'paths' => $paths); + return $packages[$type]; } }