Skip to content

Commit

Permalink
Create Bake View
Browse files Browse the repository at this point in the history
This is a view is to be used mostly (or probably exclusively) via bake,
allowing templates to use all view features (helpers, elements) with
the goal of simplifying bake template modifications
  • Loading branch information
AD7six committed Nov 14, 2014
1 parent 1b786b0 commit d8ba6c2
Show file tree
Hide file tree
Showing 39 changed files with 848 additions and 645 deletions.
2 changes: 1 addition & 1 deletion src/Shell/Task/BehaviorTask.php
Expand Up @@ -46,7 +46,7 @@ public function fileName($name) {
* {@inheritDoc}
*/
public function template() {
return 'behavior';
return 'Model/behavior';
}

}
2 changes: 1 addition & 1 deletion src/Shell/Task/CellTask.php
Expand Up @@ -46,7 +46,7 @@ public function fileName($name) {
* {@inheritDoc}
*/
public function template() {
return 'cell';
return 'View/cell';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Shell/Task/ComponentTask.php
Expand Up @@ -46,7 +46,7 @@ public function fileName($name) {
* {@inheritDoc}
*/
public function template() {
return 'component';
return 'Controller/component';
}

}
4 changes: 2 additions & 2 deletions src/Shell/Task/ControllerTask.php
Expand Up @@ -99,7 +99,7 @@ public function bakeActions($controllerName) {
'plugin', 'admin', 'pluralName', 'singularName',
'singularHumanName', 'pluralHumanName', 'modelObj', 'currentModelName'
));
$actions = $this->Template->generate('actions', 'controller_actions');
$actions = $this->Template->generate('Controller/actions');
return $actions;
}

Expand Down Expand Up @@ -161,7 +161,7 @@ public function bakeController($controllerName, array $data) {

$this->Template->set($data);

$contents = $this->Template->generate('classes', 'controller');
$contents = $this->Template->generate('Controller/controller');

$path = $this->getPath();
$filename = $path . $controllerName . 'Controller.php';
Expand Down
2 changes: 1 addition & 1 deletion src/Shell/Task/FixtureTask.php
Expand Up @@ -212,7 +212,7 @@ public function generateFixtureFile($model, array $otherVars) {

$this->Template->set('model', $model);
$this->Template->set($vars);
$content = $this->Template->generate('classes', 'fixture');
$content = $this->Template->generate('tests/fixture');

$this->out("\n" . sprintf('Baking test fixture for %s...', $model), 1, Shell::QUIET);
$this->createFile($path . $filename, $content);
Expand Down
2 changes: 1 addition & 1 deletion src/Shell/Task/HelperTask.php
Expand Up @@ -46,7 +46,7 @@ public function fileName($name) {
* {@inheritDoc}
*/
public function template() {
return 'helper';
return 'View/helper';
}

}
4 changes: 2 additions & 2 deletions src/Shell/Task/ModelTask.php
Expand Up @@ -594,7 +594,7 @@ public function bakeEntity($model, array $data = []) {
];

$this->Template->set($data);
$out = $this->Template->generate('classes', 'entity');
$out = $this->Template->generate('Model/entity');

$path = $this->getPath();
$filename = $path . 'Entity' . DS . $name . '.php';
Expand Down Expand Up @@ -639,7 +639,7 @@ public function bakeTable($model, array $data = []) {
];

$this->Template->set($data);
$out = $this->Template->generate('classes', 'table');
$out = $this->Template->generate('Model/table');

$path = $this->getPath();
$filename = $path . 'Table' . DS . $name . 'Table.php';
Expand Down
6 changes: 3 additions & 3 deletions src/Shell/Task/PluginTask.php
Expand Up @@ -189,7 +189,7 @@ protected function _generateRoutes($plugin, $path) {
'plugin' => $plugin,
]);
$this->out('Generating routes.php file...');
$out = $this->Template->generate('config', 'routes');
$out = $this->Template->generate('config/routes');
$file = $path . $plugin . DS . 'config' . DS . 'routes.php';
$this->createFile($file, $out);
}
Expand All @@ -207,7 +207,7 @@ protected function _generatePhpunitXml($plugin, $path) {
'path' => $path
]);
$this->out('Generating phpunit.xml file...');
$out = $this->Template->generate('test', 'phpunit.xml');
$out = $this->Template->generate('tests/phpunit.xml');
$file = $path . $plugin . DS . 'phpunit.xml';
$this->createFile($file, $out);
}
Expand All @@ -226,7 +226,7 @@ protected function _generateTestBootstrap($plugin, $path) {
'root' => ROOT
]);
$this->out('Generating tests/bootstrap.php file...');
$out = $this->Template->generate('test', 'bootstrap');
$out = $this->Template->generate('tests/bootstrap');
$file = $path . $plugin . DS . 'tests' . DS . 'bootstrap.php';
$this->createFile($file, $out);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Shell/Task/ShellTask.php
Expand Up @@ -46,7 +46,7 @@ public function fileName($name) {
* {@inheritDoc}
*/
public function template() {
return 'shell';
return 'Shell/shell';
}

}
2 changes: 1 addition & 1 deletion src/Shell/Task/SimpleBakeTask.php
Expand Up @@ -91,7 +91,7 @@ public function main($name = null) {
public function bake($name) {
$this->Template->set('name', $name);
$this->Template->set($this->templateData());
$contents = $this->Template->generate('classes', $this->template());
$contents = $this->Template->generate($this->template());

$filename = $this->getPath() . $this->fileName($name);
$this->createFile($filename, $contents);
Expand Down
124 changes: 15 additions & 109 deletions src/Shell/Task/TemplateTask.php
Expand Up @@ -19,6 +19,9 @@
use Cake\Core\ConventionsTrait;
use Cake\Core\Plugin;
use Cake\Filesystem\Folder;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\View\BakeView;
use Cake\View\ViewVarsTrait;

/**
Expand All @@ -39,135 +42,38 @@ class TemplateTask extends Shell {
public $templatePaths = [];

/**
* Initialize callback. Setup paths for the template task.
* BakeView instance
*
* @return void
* @var Cake\View\BakeView
*/
public function initialize() {
$this->templatePaths = $this->_findTemplates();
}
public $View;

/**
* Find the paths to all the installed shell templates in the app.
*
* Bake templates are directories under `Template/Bake` path.
* They are listed in this order: app -> plugin -> default
* Initialize callback. Setup paths for the template task.
*
* @return array Array of bake templates that are installed.
* @return void
*/
protected function _findTemplates() {
$paths = App::path('Template');

$plugins = Plugin::loaded();
foreach ($plugins as $plugin) {
$paths[] = Plugin::classPath($plugin) . 'Template' . DS;
}

$core = current(App::core('Template'));
$Folder = new Folder($core . 'Bake' . DS . 'default');

$contents = $Folder->read();
$templateFolders = $contents[0];

$paths[] = $core;

foreach ($paths as $i => $path) {
$paths[$i] = rtrim($path, DS) . DS;
}

$this->_io->verbose('Found the following bake templates:');

$templates = [];
foreach ($paths as $path) {
$Folder = new Folder($path . 'Bake', false);
$contents = $Folder->read();
$subDirs = $contents[0];
foreach ($subDirs as $dir) {
$Folder = new Folder($path . 'Bake' . DS . $dir);
$contents = $Folder->read();
$subDirs = $contents[0];
if (array_intersect($contents[0], $templateFolders)) {
$templateDir = $path . 'Bake' . DS . $dir . DS;
$templates[$dir] = $templateDir;

$this->_io->verbose(sprintf("- %s -> %s", $dir, $templateDir));
}
}
}
return $templates;
public function initialize() {
$this->View = new BakeView(new Request(), new Response());
}

/**
* Runs the template
*
* @param string $directory directory / type of thing you want
* @param string $filename template name
* @param string $template bake template to render
* @param array $vars Additional vars to set to template scope.
* @return string contents of generated code template
*/
public function generate($directory, $filename, $vars = null) {
public function generate($template, $vars = null) {
if ($vars !== null) {
$this->set($vars);
}
if (empty($this->templatePaths)) {
if (empty($this->View)) {
$this->initialize();
}
$templatePath = $this->getTemplatePath();
$templateFile = $this->_findTemplate($templatePath, $directory, $filename);
if ($templateFile) {
extract($this->viewVars);
ob_start();
ob_implicit_flush(0);
include $templateFile;
$content = ob_get_clean();
return $content;
}
return '';
}

/**
* Find the template name for the current operation.
* If there is only one template in $templatePaths it will be used.
* If there is a -template param in the cli args, it will be used.
* If there is more than one installed template user interaction will happen
*
* @return string returns the path to the selected template.
* @throws \RuntimeException When the chosen template cannot be found.
*/
public function getTemplatePath() {
if (empty($this->params['template'])) {
$this->params['template'] = 'default';
}
if (!isset($this->templatePaths[$this->params['template']])) {
$msg = sprintf('Unable to locate "%s" bake template', $this->params['template']);
throw new \RuntimeException($msg);
}
$this->_io->verbose(sprintf('Using "%s" bake template', $this->params['template']));
return $this->templatePaths[$this->params['template']];
}

/**
* Find a template inside a directory inside a path.
* Will scan all other template dirs if the template is not found in the first directory.
*
* @param string $path The initial path to look for the file on. If it is not found fallbacks will be used.
* @param string $directory Subdirectory to look for ie. 'views', 'objects'
* @param string $filename lower_case_underscored filename you want.
* @return string filename will exit program if template is not found.
*/
protected function _findTemplate($path, $directory, $filename) {
$templateFile = $path . $directory . DS . $filename . '.ctp';
if (file_exists($templateFile)) {
return $templateFile;
}
foreach ($this->templatePaths as $path) {
$templatePath = $path . $directory . DS . $filename . '.ctp';
if (file_exists($templatePath)) {
return $templatePath;
}
}
$this->err('Could not find template for %s', $filename);
return false;
$this->View->set($this->viewVars);
return $this->View->render($template);
}

}
2 changes: 1 addition & 1 deletion src/Shell/Task/TestTask.php
Expand Up @@ -202,7 +202,7 @@ public function bake($type, $className) {
'realType', 'preConstruct', 'postConstruct', 'construction',
'uses', 'baseNamespace', 'subNamespace', 'namespace'
));
$out = $this->Template->generate('classes', 'test');
$out = $this->Template->generate('tests/test_case');

$filename = $this->testCaseFileName($type, $fullClassName);
$emptyFile = $this->getPath() . $this->getSubspacePath($type) . DS . 'empty';
Expand Down
40 changes: 1 addition & 39 deletions src/Shell/Task/ViewTask.php
Expand Up @@ -365,45 +365,7 @@ public function getContent($action, $vars = null) {
$this->Template->set('action', $action);
$this->Template->set('plugin', $this->plugin);
$this->Template->set($vars);
$template = $this->getTemplate($action);
if ($template) {
return $this->Template->generate('views', $template);
}
return false;
}

/**
* Gets the template name based on the action name
*
* @param string $action name
* @return string template name
*/
public function getTemplate($action) {
if ($action != $this->template && in_array($action, $this->noTemplateActions)) {
return false;
}
if (!empty($this->template) && $action != $this->template) {
return $this->template;
}
$templatePath = $this->Template->getTemplatePath();

if (!empty($this->params['prefix'])) {
$prefixed = Inflector::underscore($this->params['prefix']) . '_' . $action;
if (file_exists($templatePath . 'views/' . $prefixed . '.ctp')) {
return $prefixed;
}
$generic = preg_replace('/(.*)(_add|_edit)$/', '\1_form', $prefixed);
if (file_exists($templatePath . 'views/' . $generic . '.ctp')) {
return $generic;
}
}
if (file_exists($templatePath . 'views/' . $action . '.ctp')) {
return $action;
}
if (in_array($action, ['add', 'edit'])) {
return 'form';
}
return $action;
return $this->Template->generate("Template/$action");
}

/**
Expand Down

0 comments on commit d8ba6c2

Please sign in to comment.