2005-05-15 21:41:38 +00:00
|
|
|
<?PHP
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// + $Id$
|
|
|
|
// +------------------------------------------------------------------+ //
|
2005-05-16 00:52:42 +00:00
|
|
|
// + Cake <https://developers.nextco.com/cake/> + //
|
|
|
|
// + Copyright: (c) 2005 Cake Authors/Developers + //
|
2005-05-15 21:41:38 +00:00
|
|
|
// + + //
|
2005-05-16 00:52:42 +00:00
|
|
|
// + Author(s): Michal Tatarynowicz aka Pies <tatarynowicz@gmail.com> + //
|
|
|
|
// + Larry E. Masters aka PhpNut <nut@phpnut.com> + //
|
|
|
|
// + Kamil Dzielinski aka Brego <brego.dk@gmail.com> + //
|
2005-05-15 21:41:38 +00:00
|
|
|
// + + //
|
|
|
|
// +------------------------------------------------------------------+ //
|
2005-05-16 00:52:42 +00:00
|
|
|
// + Licensed under The MIT License + //
|
|
|
|
// + Redistributions of files must retain the above copyright notice. + //
|
|
|
|
// + You may not use this file except in compliance with the License. + //
|
|
|
|
// + + //
|
|
|
|
// + You may obtain a copy of the License at: + //
|
|
|
|
// + License page: http://www.opensource.org/licenses/mit-license.php + //
|
2005-05-15 21:41:38 +00:00
|
|
|
// +------------------------------------------------------------------+ //
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purpose: Bake
|
|
|
|
* Creates controller, model, view files, and the required directories on demand.
|
|
|
|
* Used by scripts/add.php
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @author Michal Tatarynowicz <tatarynowicz@gmail.com>
|
2005-05-16 00:52:42 +00:00
|
|
|
* @author Larry E. Masters aka PhpNut <nut@phpnut.com>
|
|
|
|
* @author Kamil Dzielinski aka Brego <brego.dk@gmail.com>
|
|
|
|
* @copyright Copyright (c) 2005, Cake Authors/Developers
|
|
|
|
* @link https://developers.nextco.com/cake/wiki/Authors Authors/Developers
|
2005-05-15 21:41:38 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since Cake v 0.2.9
|
|
|
|
* @version $Revision$
|
2005-05-16 00:52:42 +00:00
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2005-05-15 21:41:38 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
uses('object', 'inflector');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since Cake v 0.2.9
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Bake extends Object {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
|
|
|
var $stdin = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
|
|
|
var $stdout = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
|
|
|
var $stderr = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
|
|
|
var $actions = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
var $dont_ask = false;
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $type
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function template ($type) {
|
|
|
|
switch ($type) {
|
2005-05-17 20:55:27 +00:00
|
|
|
case 'view': return "%s";
|
2005-05-16 23:14:37 +00:00
|
|
|
case 'model': return "<?PHP\n\nclass %s extends AppModel {\n}\n\n?>";
|
|
|
|
case 'action': return "\n\tfunction %s () {\n\t}\n";
|
|
|
|
case 'ctrl': return "<?PHP\n\nclass %s extends %s {\n%s\n}\n\n?>";
|
|
|
|
case 'helper': return "<?PHP\n\nclass %s extends AppController {\n}\n\n?>";
|
|
|
|
case 'test': return '<?PHP
|
|
|
|
|
|
|
|
class %sTest extends TestCase {
|
|
|
|
var $abc;
|
|
|
|
|
|
|
|
// called before the tests
|
|
|
|
function setUp() {
|
|
|
|
$this->abc = new %s ();
|
|
|
|
}
|
|
|
|
|
|
|
|
// called after the tests
|
|
|
|
function tearDown() {
|
|
|
|
unset($this->abc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
function testFoo () {
|
|
|
|
$result = $this->abc->Foo();
|
|
|
|
$expected = \'\';
|
|
|
|
$this->assertEquals($result, $expected);
|
|
|
|
}
|
|
|
|
*/
|
2005-05-15 21:41:38 +00:00
|
|
|
}
|
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
?>';
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2005-05-16 23:14:37 +00:00
|
|
|
* @param unknown_type $type
|
|
|
|
* @param unknown_type $names
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function __construct ($type, $names) {
|
|
|
|
|
|
|
|
$this->stdin = fopen('php://stdin', 'r');
|
|
|
|
$this->stdout = fopen('php://stdout', 'w');
|
|
|
|
$this->stderr = fopen('php://stderr', 'w');
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
|
|
|
|
case 'model':
|
|
|
|
case 'models':
|
|
|
|
foreach ($names as $model_name)
|
|
|
|
$this->newModel($model_name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'controller':
|
|
|
|
case 'ctrl':
|
|
|
|
$controller = array_shift($names);
|
|
|
|
|
|
|
|
$add_actions = array();
|
|
|
|
foreach ($names as $action) {
|
|
|
|
$add_actions[] = $action;
|
|
|
|
$this->newView($controller, $action);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->newController($controller, $add_actions);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'view':
|
|
|
|
case 'views':
|
|
|
|
$r = null;
|
|
|
|
foreach ($names as $model_name) {
|
|
|
|
if (preg_match('/^([a-z0-9_]+(?:\/[a-z0-9_]+)*)\/([a-z0-9_]+)$/i', $model_name, $r)) {
|
|
|
|
$this->newView($r[1], $r[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->actions)
|
|
|
|
fwrite($this->stderr, "Nothing to do, quitting.\n");
|
|
|
|
|
2005-05-15 21:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2005-05-16 23:14:37 +00:00
|
|
|
* @param unknown_type $controller
|
|
|
|
* @param unknown_type $name
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function newView ($controller, $name) {
|
|
|
|
$dir = Inflector::underscore($controller);
|
2005-05-17 20:55:27 +00:00
|
|
|
$path = "{$dir}/".strtolower($name).".thtml";
|
2005-05-16 23:14:37 +00:00
|
|
|
$this->createDir(VIEWS.$dir);
|
2005-05-17 20:55:27 +00:00
|
|
|
$fn = VIEWS.$path;
|
|
|
|
$this->createFile($fn, sprintf($this->template('view'), "<p>Edit <b>app/views/{$path}</b> to change this message.</p>"));
|
2005-05-16 23:14:37 +00:00
|
|
|
$this->actions++;
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2005-05-16 23:47:54 +00:00
|
|
|
* @param unknown_type $name
|
2005-05-16 23:14:37 +00:00
|
|
|
* @param array $actions
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function newController ($name, $actions=array()) {
|
|
|
|
$this->makeController($name, $actions);
|
|
|
|
$this->makeControllerTest($name);
|
|
|
|
$this->makeHelper($name);
|
|
|
|
$this->makeHelperTest($name);
|
|
|
|
$this->actions++;
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2005-05-16 23:47:54 +00:00
|
|
|
* @param unknown_type $name
|
2005-05-16 23:14:37 +00:00
|
|
|
* @param array $actions
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function makeController ($name, $actions) {
|
|
|
|
$ctrl = $this->makeControllerName($name);
|
|
|
|
$helper = $this->makeHelperName($name);
|
|
|
|
$body = sprintf($this->template('ctrl'), $ctrl, $helper, join('', $this->getActions($actions)));
|
|
|
|
return $this->createFile($this->makeControllerFn($name), $body);
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $name
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function makeControllerName ($name) {
|
|
|
|
return Inflector::camelize($name).'Controller';
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $name
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function makeControllerFn ($name) {
|
|
|
|
return CONTROLLERS.Inflector::underscore($name).'_controller.php';
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $name
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function makeControllerTest ($name) {
|
|
|
|
$fn = CONTROLLER_TESTS.Inflector::underscore($name).'_controller_test.php';
|
|
|
|
$body = $this->getTestBody($this->makeControllerName($name));
|
|
|
|
return $this->createFile($fn, $body);
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $name
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function makeHelper ($name) {
|
|
|
|
$body = sprintf($this->template('helper'), $this->makeHelperName($name));
|
|
|
|
return $this->createFile($this->makeHelperFn($name), $body);
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $name
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function makeHelperName ($name) {
|
|
|
|
return Inflector::camelize($name).'Helper';
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2005-05-16 23:47:54 +00:00
|
|
|
* @param unknown_type $name
|
2005-05-16 23:14:37 +00:00
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function makeHelperFn ($name) {
|
|
|
|
return HELPERS.Inflector::underscore($name).'_helper.php';
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $name
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function makeHelperTest ($name) {
|
|
|
|
$fn = HELPER_TESTS.Inflector::underscore($name).'_helper_test.php';
|
|
|
|
$body = $this->getTestBody($this->makeHelperName($name));
|
|
|
|
return $this->createFile($fn, $body);
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $as
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function getActions ($as) {
|
|
|
|
$out = array();
|
|
|
|
foreach ($as as $a)
|
|
|
|
$out[] = sprintf($this->template('action'), $a);
|
|
|
|
return $out;
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2005-05-16 23:14:37 +00:00
|
|
|
* @param unknown_type $class
|
|
|
|
* @return unknown
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function getTestBody ($class) {
|
|
|
|
return sprintf($this->template('test'), $class, $class);
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $name
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function newModel ($name) {
|
|
|
|
$this->createFile($this->getModelFn($name), sprintf($this->template('model'), $this->getModelName($name)));
|
|
|
|
$this->makeModelTest ($name);
|
|
|
|
$this->actions++;
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $name
|
2005-05-16 23:14:37 +00:00
|
|
|
* @return unknown
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function getModelFn ($name) {
|
|
|
|
return MODELS.Inflector::underscore($name).'.php';
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $name
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function makeModelTest ($name) {
|
|
|
|
$fn = MODEL_TESTS.Inflector::underscore($name).'_test.php';
|
|
|
|
$body = $this->getTestBody($this->getModelName($name));
|
|
|
|
return $this->createFile($fn, $body);
|
|
|
|
}
|
|
|
|
|
2005-05-15 21:41:38 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $name
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function getModelName ($name) {
|
|
|
|
return Inflector::camelize($name);
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $path
|
|
|
|
* @param unknown_type $contents
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function createFile ($path, $contents) {
|
|
|
|
|
|
|
|
if (is_file($path) && !$this->dont_ask) {
|
|
|
|
fwrite($this->stdout, "File {$path} exists, overwrite? (yNaq) ");
|
|
|
|
$key = fgets($this->stdin);
|
|
|
|
|
2005-05-17 20:55:27 +00:00
|
|
|
if (preg_match("/^q$/", $key)) {
|
|
|
|
fwrite($this->stdout, "Quitting.\n");
|
2005-05-16 23:14:37 +00:00
|
|
|
exit;
|
|
|
|
}
|
2005-05-17 20:55:27 +00:00
|
|
|
elseif (preg_match("/^a$/", $key)) {
|
|
|
|
$this->dont_ask = true;
|
|
|
|
}
|
|
|
|
elseif (preg_match("/^y$/", $key)) {
|
|
|
|
}
|
|
|
|
else {
|
2005-05-16 23:14:37 +00:00
|
|
|
fwrite($this->stdout, "Skip {$path}\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($f = fopen($path, 'w')) {
|
|
|
|
fwrite($f, $contents);
|
|
|
|
fclose($f);
|
|
|
|
fwrite($this->stdout, "Wrote {$path}\n");
|
|
|
|
// debug ("Wrote {$path}");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fwrite($this->stderr, "Error! Couldn't open {$path} for writing.\n");
|
|
|
|
// debug ("Error! Couldn't open {$path} for writing.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $path
|
2005-05-16 23:14:37 +00:00
|
|
|
* @return unknown
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function createDir ($path) {
|
|
|
|
if (is_dir($path))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (mkdir($path)) {
|
|
|
|
fwrite($this->stdout, "Created {$path}\n");
|
|
|
|
// debug ("Created {$path}");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fwrite($this->stderr, "Error! Couldn't create dir {$path}\n");
|
|
|
|
// debug ("Error! Couldn't create dir {$path}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-15 21:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|