2005-05-15 21:41:38 +00:00
|
|
|
<?PHP
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// + $Id$
|
|
|
|
// +------------------------------------------------------------------+ //
|
2005-05-16 00:52:42 +00:00
|
|
|
// + Cake <https://developers.nextco.com/cake/> + //
|
2005-05-17 21:27:56 +00:00
|
|
|
// + Copyright: (c) 2005, Cake Authors/Developers + //
|
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. + //
|
2005-05-17 21:27:56 +00:00
|
|
|
// + See: http://www.opensource.org/licenses/mit-license.php + //
|
2005-05-15 21:41:38 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
2005-05-18 10:11:28 +00:00
|
|
|
* Creates controller, model, view files, and the required directories on demand.
|
I've merged in Olle's changes.
Larry, I've merged in _some_ of your changes, I'll merge in the scaffolding and joins code when you tell me it's ready. But I don't want to break how the Controller class works, can't we really do without the constructClasses() method call? Which reminds me, with your joins code, will we be able to use constructs like $user->post->findAll() and $user->post->save()?
Also, what are your changes to the DBO_MySQL class? I mean the mysqlResultSet(), and fetchResult() methods. I didn't see any MySQL-specific code inside them, perhaps they belong to the DBO class itself?
- I've changed the headers on user-editable files in /app and /config. I hope they will constitute a compromise between readability and legality. I've left file Id, copyright, and licence notices.
- /libs/basic.php::uses() function logs included files in global $loaded. Please, consider it a note to myself. Also, I've moved the NeatArray class out of the /libs/basics.php (into /libs/neat_array.php).
- Some cleanups in the Controller and Dispatcher classes.
- DBO::Prepare() accepts strings _and_ arrays now. It's a step towards a unified params theory.
- I think I've added some comments to DBO sub-classes, but it might have been Olle.
- A fix in Model class (findAll didn't work properly)
- Object's constructor sets $this->db to &DBO, which means all Object-descendand classes have default access to the database if it's connected. We need to clean up the code accordingly (some classes set their own $this->db references).
git-svn-id: https://svn.cakephp.org/repo/trunk/cake@236 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-06-05 11:05:24 +00:00
|
|
|
* Used by /scripts/bake.php.
|
2005-05-18 10:11:28 +00:00
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @author Cake Authors/Developers
|
|
|
|
* @copyright Copyright (c) 2005, Cake Authors/Developers
|
|
|
|
* @link https://developers.nextco.com/cake/wiki/Authors Authors/Developers
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since Cake v 0.2.9
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
2005-05-18 10:11:28 +00:00
|
|
|
* Require needed libraries.
|
|
|
|
*/
|
2005-05-15 21:41:38 +00:00
|
|
|
uses('object', 'inflector');
|
|
|
|
|
|
|
|
/**
|
2005-05-18 10:11:28 +00:00
|
|
|
* Bake class creates files in configured application directories. This is a
|
|
|
|
* base class for /scripts/add.php.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since Cake v 0.2.9
|
2005-05-21 22:40:51 +00:00
|
|
|
*/
|
|
|
|
class Bake extends Object {
|
|
|
|
|
2005-05-18 22:37:03 +00:00
|
|
|
/**
|
|
|
|
* Standard input stream (php://stdin).
|
|
|
|
*
|
|
|
|
* @var resource
|
|
|
|
* @access private
|
|
|
|
*/
|
2005-05-18 22:29:02 +00:00
|
|
|
var $stdin = null;
|
|
|
|
|
2005-05-18 22:37:03 +00:00
|
|
|
/**
|
|
|
|
* Standard output stream (php://stdout).
|
|
|
|
*
|
|
|
|
* @var resource
|
|
|
|
* @access private
|
|
|
|
*/
|
2005-05-18 22:29:02 +00:00
|
|
|
var $stdout = null;
|
|
|
|
|
|
|
|
/**
|
2005-05-18 22:37:03 +00:00
|
|
|
* Standard error stream (php://stderr).
|
|
|
|
*
|
|
|
|
* @var resource
|
|
|
|
* @access private
|
|
|
|
*/
|
2005-05-18 22:29:02 +00:00
|
|
|
var $stderr = null;
|
|
|
|
|
|
|
|
/**
|
2005-05-31 23:18:22 +00:00
|
|
|
* Counts actions taken.
|
2005-05-18 22:37:03 +00:00
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
* @access private
|
|
|
|
*/
|
2005-05-18 22:29:02 +00:00
|
|
|
var $actions = null;
|
|
|
|
|
|
|
|
/**
|
2005-05-31 23:18:22 +00:00
|
|
|
* Decides whether to overwrite existing files without asking.
|
2005-05-18 22:37:03 +00:00
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
2005-05-18 22:29:02 +00:00
|
|
|
var $dontAsk = false;
|
|
|
|
|
|
|
|
/**
|
2005-05-31 23:18:22 +00:00
|
|
|
* Returns code template for PHP file generator.
|
2005-05-18 22:29:02 +00:00
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @return string
|
|
|
|
* @access private
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
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
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Baker's constructor method. Initialises bakery, and starts production.
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param array $names
|
|
|
|
* @access public
|
|
|
|
* @uses Bake::stdin Opens stream for reading.
|
|
|
|
* @uses Bake::stdout Opens stream for writing.
|
|
|
|
* @uses Bake::stderr Opens stream for writing.
|
|
|
|
* @uses Bake::newModel() Depending on the case, can create a new model.
|
|
|
|
* @uses Bake::newView() Depending on the case, can create a new view.
|
|
|
|
* @uses Bake::newController() Depending on the case, can create a new controller.
|
|
|
|
*/
|
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');
|
|
|
|
|
2005-05-22 23:24:09 +00:00
|
|
|
// Output directory name
|
|
|
|
fwrite($this->stderr, "\n".substr(ROOT,0,strlen(ROOT)-1).":\n".str_repeat('-',strlen(ROOT)+1)."\n");
|
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Creates new view in VIEWS/$controller/ directory.
|
|
|
|
*
|
|
|
|
* @param string $controller
|
|
|
|
* @param string $name
|
|
|
|
* @access private
|
2005-05-31 23:18:22 +00:00
|
|
|
* @uses Inflector::underscore() Underscores directory's name.
|
|
|
|
* @uses Bake::createDir() Creates new directory in views dir, named after the controller.
|
2005-05-18 22:29:02 +00:00
|
|
|
* @uses VIEWS
|
|
|
|
* @uses Bake::createFile() Creates view file.
|
|
|
|
* @uses Bake::template() Collects view template.
|
|
|
|
* @uses Bake::actions Adds one action for each run.
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function newView ($controller, $name) {
|
2005-06-12 20:50:12 +00:00
|
|
|
// $controller = Inflector::pluralize($controller);
|
2005-05-16 23:14:37 +00:00
|
|
|
$dir = Inflector::underscore($controller);
|
2005-05-22 23:24:09 +00:00
|
|
|
$path = $dir.DS.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;
|
2005-05-22 23:24:09 +00:00
|
|
|
$this->createFile($fn, sprintf($this->template('view'), "<p>Edit <b>app".DS."views".DS."{$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
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Creates new controller with defined actions, controller's test and helper
|
|
|
|
* with helper's test.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $actions
|
|
|
|
* @access private
|
2005-05-29 19:43:59 +00:00
|
|
|
* @uses Inflector::pluralize()
|
2005-05-18 22:29:02 +00:00
|
|
|
* @uses Bake::makeController()
|
|
|
|
* @uses Bake::makeControllerTest()
|
|
|
|
* @uses Bake::makeHelper()
|
|
|
|
* @uses Bake::makeHelperTest()
|
|
|
|
* @uses Bake::actions Adds one action for each run.
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function newController ($name, $actions=array()) {
|
2005-06-12 20:50:12 +00:00
|
|
|
// $name = Inflector::pluralize($name);
|
2005-05-16 23:14:37 +00:00
|
|
|
$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-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Creates new controller file with defined actions.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $actions
|
|
|
|
* @return boolean
|
|
|
|
* @access private
|
|
|
|
* @uses Bake::makeControllerName() CamelCase for controller's name.
|
|
|
|
* @uses Bake::makeHelperName() CamelCase for helper's name.
|
|
|
|
* @uses Bake::template() Controller's template.
|
|
|
|
* @uses Bake::getActions() Actions' templates to be included in the controller.
|
|
|
|
* @uses Bake::createFile() Creates controller's file.
|
|
|
|
* @uses Bake::makeControllerFn() Underscored name for controller's filename.
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
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-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Returns controller's name in CamelCase.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
* @access private
|
|
|
|
* @uses Inflector::camelize CamelCase for controller name.
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function makeControllerName ($name) {
|
|
|
|
return Inflector::camelize($name).'Controller';
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Returns a name for controller's file, underscored.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
* @access private
|
|
|
|
* @uses Inflector::underscore() Underscore for controller's file name.
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function makeControllerFn ($name) {
|
|
|
|
return CONTROLLERS.Inflector::underscore($name).'_controller.php';
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Creates new test for a controller.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
|
|
|
* @access private
|
|
|
|
* @uses CONTROLLER_TESTS
|
|
|
|
* @uses Inflector::underscore()
|
|
|
|
* @uses Bake::getTestBody()
|
|
|
|
* @uses Bake::makeControllerName()
|
|
|
|
* @uses Bake::createFile()
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
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-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Creates new helper.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
|
|
|
* @access private
|
|
|
|
* @uses Bake::template()
|
|
|
|
* @uses Bake::makeHelperName()
|
|
|
|
* @uses Bake::createFile()
|
|
|
|
* @uses Bake::makeHelperFn()
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
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-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Returns CamelCase name for a helper.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
* @access private
|
|
|
|
* @uses Inflector::camelize()
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function makeHelperName ($name) {
|
|
|
|
return Inflector::camelize($name).'Helper';
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Underscores file name for a helper.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
* @access private
|
|
|
|
* @uses HELPERS
|
|
|
|
* @uses Inflector::underscore()
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function makeHelperFn ($name) {
|
|
|
|
return HELPERS.Inflector::underscore($name).'_helper.php';
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Creates new test for a helper.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
|
|
|
* @access private
|
|
|
|
* @uses HELPER_TESTS
|
|
|
|
* @uses Inflector::underscore()
|
|
|
|
* @uses Bake::getTestBody()
|
|
|
|
* @uses Bake::makeHelperName()
|
|
|
|
* @uses Bake::createFile()
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
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-18 22:29:02 +00:00
|
|
|
/**
|
2005-05-31 23:18:22 +00:00
|
|
|
* Returns an array of actions' templates.
|
2005-05-18 22:29:02 +00:00
|
|
|
*
|
|
|
|
* @param array $as
|
|
|
|
* @return array
|
|
|
|
* @access private
|
|
|
|
* @uses Bake::template()
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
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
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
2005-05-31 23:18:22 +00:00
|
|
|
* Returns a test template for given class.
|
2005-05-18 22:29:02 +00:00
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @return string
|
|
|
|
* @access private
|
|
|
|
* @uses Bake::template()
|
|
|
|
*/
|
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
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Creates new model.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @access private
|
|
|
|
* @uses Bake::createFile()
|
|
|
|
* @uses Bake::getModelFn()
|
|
|
|
* @uses Bake::template()
|
|
|
|
* @uses Bake::getModelName()
|
|
|
|
* @uses Bake::makeModelTest()
|
|
|
|
* @uses Bake::actions
|
|
|
|
*/
|
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
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
2005-05-31 23:18:22 +00:00
|
|
|
* Returns an underscored filename for a model.
|
2005-05-18 22:29:02 +00:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
* @access private
|
|
|
|
* @uses MODELS
|
|
|
|
* @uses Inflector::underscore()
|
|
|
|
*/
|
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
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Creates a test for a given model.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
|
|
|
* @access private
|
|
|
|
* @uses MODEL_TESTS
|
|
|
|
* @uses Inflector::underscore()
|
|
|
|
* @uses Bake::getTestBody()
|
|
|
|
* @uses Bake::getModelName()
|
|
|
|
* @uses Bake::createFile()
|
|
|
|
*/
|
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-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Returns CamelCased name of a model.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
* @access private
|
|
|
|
* @uses Inflector::camelize()
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function getModelName ($name) {
|
|
|
|
return Inflector::camelize($name);
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Creates a file with given path and contents.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $contents
|
|
|
|
* @return boolean
|
|
|
|
* @access private
|
|
|
|
* @uses Bake::dontAsk
|
|
|
|
* @uses Bake::stdin
|
|
|
|
* @uses Bake::stdout
|
|
|
|
* @uses Bake::stderr
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function createFile ($path, $contents) {
|
2005-05-22 23:24:09 +00:00
|
|
|
$shortPath = str_replace(ROOT,null,$path);
|
2005-05-16 23:14:37 +00:00
|
|
|
|
2005-05-18 10:11:28 +00:00
|
|
|
if (is_file($path) && !$this->dontAsk) {
|
2005-05-22 23:24:09 +00:00
|
|
|
fwrite($this->stdout, "File {$shortPath} exists, overwrite? (yNaq) ");
|
2005-05-21 22:40:51 +00:00
|
|
|
$key = trim(fgets($this->stdin));
|
2005-05-22 23:24:09 +00:00
|
|
|
|
2005-05-21 22:40:51 +00:00
|
|
|
if ($key=='q') {
|
2005-05-17 20:55:27 +00:00
|
|
|
fwrite($this->stdout, "Quitting.\n");
|
2005-05-16 23:14:37 +00:00
|
|
|
exit;
|
|
|
|
}
|
2005-05-21 22:40:51 +00:00
|
|
|
elseif ($key=='a') {
|
|
|
|
$this->dont_ask = true;
|
2005-05-17 20:55:27 +00:00
|
|
|
}
|
2005-05-21 22:40:51 +00:00
|
|
|
elseif ($key=='y') {
|
2005-05-17 20:55:27 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-05-22 23:24:09 +00:00
|
|
|
fwrite($this->stdout, "Skip {$shortPath}\n");
|
2005-05-16 23:14:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($f = fopen($path, 'w')) {
|
|
|
|
fwrite($f, $contents);
|
|
|
|
fclose($f);
|
2005-05-22 23:24:09 +00:00
|
|
|
fwrite($this->stdout, "Wrote {$shortPath}\n");
|
2005-05-16 23:14:37 +00:00
|
|
|
// debug ("Wrote {$path}");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
2005-05-22 23:24:09 +00:00
|
|
|
fwrite($this->stderr, "Error! Couldn't open {$shortPath} for writing.\n");
|
2005-05-16 23:14:37 +00:00
|
|
|
// debug ("Error! Couldn't open {$path} for writing.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-18 22:29:02 +00:00
|
|
|
/**
|
|
|
|
* Creates a directory with given path.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return boolean
|
|
|
|
* @access private
|
|
|
|
* @uses Bake::stdin
|
|
|
|
* @uses Bake::stdout
|
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function createDir ($path) {
|
|
|
|
if (is_dir($path))
|
|
|
|
return true;
|
|
|
|
|
2005-05-22 23:24:09 +00:00
|
|
|
$shortPath = str_replace(ROOT, null, $path);
|
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
if (mkdir($path)) {
|
2005-05-22 23:24:09 +00:00
|
|
|
fwrite($this->stdout, "Created {$shortPath}\n");
|
2005-05-16 23:14:37 +00:00
|
|
|
// debug ("Created {$path}");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
2005-05-22 23:24:09 +00:00
|
|
|
fwrite($this->stderr, "Error! Couldn't create dir {$shortPath}\n");
|
2005-05-16 23:14:37 +00:00
|
|
|
// debug ("Error! Couldn't create dir {$path}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-15 21:41:38 +00:00
|
|
|
}
|
|
|
|
|
2005-05-18 10:11:28 +00:00
|
|
|
?>
|