Skip to content

Commit

Permalink
Moved build logic to Build class
Browse files Browse the repository at this point in the history
  • Loading branch information
tedivm committed Jun 27, 2014
1 parent d393401 commit dd55263
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 90 deletions.
91 changes: 91 additions & 0 deletions src/Spark/Builder.php
@@ -0,0 +1,91 @@
<?php

namespace Spark;

use Symfony\Component\Finder\Finder;

class Builder
{

protected $plugins;
protected $pluginPath;

protected $templateSourceDirectories = array();

protected $outputDirectories = array();
protected $outputFiles = array();

public function __construct($plugins, $pluginsPath)
{
$this->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;
}
}
}
}

}
107 changes: 17 additions & 90 deletions src/Spark/Commands/Create.php
Expand Up @@ -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
{
Expand All @@ -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');
Expand All @@ -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];
}

}

0 comments on commit dd55263

Please sign in to comment.