command); if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig', 'Fixture'))) { $path = Inflector::underscore(Inflector::pluralize($this->command)); $this->{$task}->path = $this->params['working'] . DS . $path . DS; if (isset($this->params['connection'])) { $this->{$task}->connection = $this->params['connection']; } if (isset($this->params['plugin'])) { $this->{$task}->plugin = $this->params['plugin']; } if (!is_dir($this->{$task}->path)) { $this->err(sprintf(__("%s directory could not be found.\nBe sure you have created %s", true), $task, $this->{$task}->path)); $this->_stop(); } } } /** * Override main() to handle action * * @access public */ function main() { if (!is_dir($this->DbConfig->path)) { if ($this->Project->execute()) { $this->DbConfig->path = $this->params['working'] . DS . 'config' . DS; } } if (!config('database')) { $this->out(__("Your database configuration was not found. Take a moment to create one.", true)); $this->args = null; return $this->DbConfig->execute(); } $this->out('Interactive Bake Shell'); $this->hr(); $this->out('[D]atabase Configuration'); $this->out('[M]odel'); $this->out('[V]iew'); $this->out('[C]ontroller'); $this->out('[P]roject'); $this->out('[F]ixture'); $this->out('[Q]uit'); $classToBake = strtoupper($this->in(__('What would you like to Bake?', true), array('D', 'M', 'V', 'C', 'P', 'Q'))); switch ($classToBake) { case 'D': $this->DbConfig->execute(); break; case 'M': $this->Model->execute(); break; case 'V': $this->View->execute(); break; case 'C': $this->Controller->execute(); break; case 'P': $this->Project->execute(); break; case 'F': $this->Fixture->execute(); break; case 'Q': exit(0); break; default: $this->out(__('You have made an invalid selection. Please choose a type of class to Bake by entering D, M, V, or C.', true)); } $this->hr(); $this->main(); } /** * Quickly bake the MVC * * @access public */ function all() { $ds = 'default'; $this->hr(); $this->out('Bake All'); $this->hr(); if (isset($this->params['connection'])) { $ds = $this->params['connection']; } if (empty($this->args)) { $name = $this->Model->getName($ds); } if (!empty($this->args[0])) { $name = $this->args[0]; $this->Model->listAll($ds, false); } $modelExists = false; $model = $this->_modelName($name); if (App::import('Model', $model)) { $object = new $model(); $modelExists = true; } else { App::import('Model'); $object = new Model(array('name' => $name, 'ds' => $ds)); } $modelBaked = $this->Model->bake($object, false); if ($modelBaked && $modelExists === false) { $this->out(sprintf(__('%s Model was baked.', true), $model)); if ($this->_checkUnitTest()) { $this->Model->bakeTest($model); } $modelExists = true; } if ($modelExists === true) { $controller = $this->_controllerName($name); if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) { $this->out(sprintf(__('%s Controller was baked.', true), $name)); if ($this->_checkUnitTest()) { $this->Controller->bakeTest($controller); } } if (App::import('Controller', $controller)) { $this->View->args = array($controller); $this->View->execute(); } $this->out(__('Bake All complete')); array_shift($this->args); } else { $this->err(__('Bake All could not continue without a valid model', true)); } if (empty($this->args)) { $this->all(); } $this->_stop(); } /** * Displays help contents * * @access public */ function help() { $this->out('CakePHP Bake:'); $this->hr(); $this->out('The Bake script generates controllers, views and models for your application.'); $this->out('If run with no command line arguments, Bake guides the user through the class'); $this->out('creation process. You can customize the generation process by telling Bake'); $this->out('where different parts of your application are using command line arguments.'); $this->hr(); $this->out("Usage: cake bake ..."); $this->hr(); $this->out('Params:'); $this->out("\t-app Absolute/Relative path to your app folder.\n"); $this->out('Commands:'); $this->out("\n\tbake help\n\t\tshows this help message."); $this->out("\n\tbake all \n\t\tbakes complete MVC. optional of a Model"); $this->out("\n\tbake project \n\t\tbakes a new app folder in the path supplied\n\t\tor in current directory if no path is specified"); $this->out("\n\tbake plugin \n\t\tbakes a new plugin folder in the path supplied\n\t\tor in current directory if no path is specified."); $this->out("\n\tbake db_config\n\t\tbakes a database.php file in config directory."); $this->out("\n\tbake model\n\t\tbakes a model. run 'bake model help' for more info"); $this->out("\n\tbake view\n\t\tbakes views. run 'bake view help' for more info"); $this->out("\n\tbake controller\n\t\tbakes a controller. run 'bake controller help' for more info"); $this->out(""); } } /** * Similar to View but has no dependancy on controller * **/ class CodeGenerator { /** * variables to add to template scope * * @var array **/ var $templateVars = array(); /** * set the paths for the code generator to search for templates * * @param array $paths Array of paths to look in * @access public * @return void **/ function setPaths($paths) { $this->_paths = $paths; } /** * Find a template * * @param string $directory Subdirectory to look for ie. 'views', 'objects' * @param string $filename lower_case_underscored filename you want. * @access public * @return string filename or false if scan failed. **/ function _findTemplate($directory, $filename) { foreach ($this->_paths as $path) { $templatePath = $path . 'templates' . DS . $directory . DS . $filename . '.ctp'; if (file_exists($templatePath) && is_file($templatePath)) { return $templatePath; } } return false; } /** * Set variable values to the template scope * * @param mixed $one A string or an array of data. * @param mixed $two Value in case $one is a string (which then works as the key). * Unused if $one is an associative array, otherwise serves as the values to $one's keys. * @return void */ function set($one, $two = null) { $data = null; if (is_array($one)) { if (is_array($two)) { $data = array_combine($one, $two); } else { $data = $one; } } else { $data = array($one => $two); } if ($data == null) { return false; } foreach ($data as $name => $value) { $this->templateVars[$name] = $value; } } /** * Runs the template * * @param string $directory directory / type of thing you want * @param string $filename template name * @param string $vars Additional vars to set to template scope. * @access public * @return contents of generated code template **/ function generate($directory, $filename, $vars = null) { if ($vars !== null) { $this->set($vars); } $templateFile = $this->_findTemplate($directory, $filename); if ($templateFile) { extract($this->templateVars); ob_start(); ob_implicit_flush(0); include($templateFile); $content = ob_get_clean(); return $content; } return ''; } } ?>