Source for file trax_generator.php
Documentation is available at trax_generator.php
* File containing the TraxGenerator class
* @version $Id: trax_generator.php 260 2006-09-02 07:19:54Z john $
* @copyright (c) 2005 John Peterson
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* Generate application files in the Trax work area
* Implements the commands of {@link generate.php script/generate.php}
* <li>{@link generate_controller() controller}</li>
* <li>{@link generate_model() model}</li>
* <li>{@link generate_scaffold() scaffold}</li>
* Filesystem path to the app/views directory in the Trax work area
* Filesystem path to the app/controllers directory in the Trax work area
* Filesystem path to the app/helpers directory in the Trax work area
* Filesystem path to the app/model directory in the Trax work area
* Generated subdirectories in the Trax work area
* When a controller is generated with a name that includes '/',
* $extra_path is set to the implied subdirectories.
* Platform-dependent command to make a directory
* Filesystem path to the templates/controller.php file
* Filesystem path to the templates/helper.php file
* Filesystem path to the templates/view.phtml file
* Filesystem path to the templates/model.php file
* Filesystem path to templates/scaffolds/generator_templates directory
* Filesystem path to the app/views/layouts/ directory in the
* @todo Document this variable
* Value is set by {@link generate_controller()} and used by
* {@link generate_scaffold()}
* CamelCase name of the controller class
* Constructor for the TraxGenerator object
* Compute and store filesystem paths to the various
* subdirectories of the Trax work area and the template files
* used to generate application files
* @uses controller_template_file
* @uses helper_template_file
* @uses model_template_file
* @uses scaffold_template_path
* @uses view_template_file
TRAX_LIB_ROOT . "/templates/controller.php";
$this->mailer_view_template_file =
$this->mailer_model_template_file =
if (substr(PHP_OS, 0, 3) == 'WIN') {
* Parse command line and carry out the command
* Command line arguments, if any are in $_SERVER['argv']
* @uses controller_help()
* @uses generate_controller()
* @uses generate_scaffold()
// If command line arguments exist, parse them
$command_name = $_SERVER["argv"][2];
// Execute command or output a diagnostic
// Process "controller" command
if(empty($command_name)) {
&& ($_SERVER["argv"][3] != "")) {
for($i= 3;$i < count($_SERVER["argv"]);$i++ ) {
// Process "model" command
// $command_name is the name of the model
if(empty($command_name)) {
// Process "scaffold" command
// $command_name has the name of the model
// $_SERVER['argv'][3] has the name of the controller
if(empty($command_name)) {
&& ($_SERVER["argv"][3] != "")) {
for($i= 3;$i < count($_SERVER["argv"]);$i++ ) {
// Model name is required
if( empty($command_name) ) {
echo "Error: name of model omitted\n";
// Controller name is optional
$controller_name = $_SERVER["argv"][3];
// Views are optional following controller name
&& ($_SERVER["argv"][4] != "")) {
for($i= 4;$i < count($_SERVER["argv"]);$i++ ) {
$controller_name, $views);
* Implement "generate controller" command
* <p>Example:<br><samp>php script/generate.php controller</samp>
* <samp>app/controllers/</samp><i>some_name</i><samp>_controller.php</samp><br>
* containing the class definition<br>
* <samp>class</samp> <i>SomeName</i><samp>Controller extends
* ApplicationController {}</samp></li>
* <samp>app/helpers/</samp><i>some_name</i><samp>_helper.php</samp></li>
* <samp>app/views/</samp><i>some_name</i></li>
* <p>Optionally, one or more views can be appended to the command:<br>
* <samp>php script/generate.php controller</samp>
* <i>SomeName view1 view2</i><br>
* which will additionally generate files:<br>
* <samp>app/views/</samp><i>some_name/view1</i><samp>.phtml</samp><br>
* <samp>app/views/</samp><i>some_name/view2</i><samp>.phtml</samp></p>
* @param string $name Name in CamelCase of the controller to generate.
* The value may include '/' which will cause
* creation of subdirectories indicated to
* hold the controller and view files.
* @param string $views Optional list of views to generate
* @param boolean $scaffolding
* @uses Inflector::underscore()
* @uses $controller_class Set during call
* @uses $controller_path Must be set before call.
* @uses create_controller()
* @uses $extra_path Set during call
* @uses $helper_path Must be set before call.
* @uses $layouts_path Must be set before call.
* @uses $layout_filename Set during call
* @uses $view_path Must be set before call.
# Set the View and Controller extra path info
# Create the extra folders for View / Controller
# Create the actual controller/helper files
# Create view files if any
foreach($views as $view) {
} elseif(!empty($views)) {
$template = str_replace('[class_name]',$model_class,$template);
# There are some views, add a method for each
# Multiple views in an array
foreach($views as $view) {
$method = "\tfunction $view() {\n";
$method .= "\t\t\$this->subject = '". $model_class. "->". $view. "';\n";
$method .= "\t\t\$this->recipients = '';\n";
$method .= "\t\t\$this->from = '';\n";
$method .= "\t\t\$this->headers = array();\n";
$method .= "\t\t\$this->body = array();\n";
$class_methods[] = $method;
$class_methods = implode("\n\n",$class_methods);
$class_methods = "\tfunction $views() {\n";
$class_methods .= "\t\t\$this->subject = '". $model_class. "->". $views. "';\n";
$class_methods .= "\t\t\$this->recipients = '';\n";
$class_methods .= "\t\t\$this->from = '';\n";
$class_methods .= "\t\t\$this->headers = array();\n";
$class_methods .= "\t\t\$this->body = array();\n";
$template = str_replace('[class_methods]', $class_methods, $template);
# No view methods to add, so remove unneeded template
$template = str_replace('[class_methods]', '', $template);
# Write the mailer model to disk
echo "error creating mailer model file: $model_file\n";
echo "create $model_file\n";
echo "error mailer model template file doesn't exist: $this->mailer_model_template_file\n";
echo "exists $model_file\n";
# Now create the view files
# Create the extra folders for View / Controller
# Create view files if any
foreach($views as $view) {
} elseif(!empty($views)) {
* Implement the "generate model" command
* <p>Example:<br><samp>php script/generate.php model</samp>
* <samp>app/models/</samp><i>some_name</i><samp>.php</samp><br>
* containing the class definition<br>
* <samp>class</samp> <i>SomeName</i> <samp>extends
* @param string $name Name of the model. May be in either
* under_score or CamelCase. If no '_' exists in
* $name it is treated as CamelCase.
* @uses Inflector::underscore()
* @uses model_path Must be set before call.
* Not changed during call.
* @uses model_template_file Must be set before call.
* Not changed during call.
$template = str_replace('[class_name]',$model_class,$template);
echo "error creating model file: $model_file\n";
echo "create $model_file\n";
echo "exists $model_file\n";
* Implement the "generate scaffold" command
* @param string $model_name
* @param string $controller_name
* @uses generate_controller()
* @uses Inflector::classify()
* @uses Inflector::humanize()
* @uses Inflector::pluralize()
* @uses Inflector::singularize()
* @uses Inflector::underscore()
* @uses $layout_filename Set as output from
* @uses fix_php_brackets()
//echo 'generate_scaffold("'.$model_name.'", "'
// .$controller_name.'", "'.$views.'")'."\n";
echo "Error - Can't create Model: $model_name.\n";
Trax::$current_controller_object = & $this;
$model_class_name = Inflector::classify($model_name);
$this->{$singular_model_name} = new $model_class_name();
} catch (ActiveRecordError $e) {
echo "Can't create model.\n";
echo $e->getMessage(). "\n";
. ActiveRecord::$database_settings[TRAX_ENV]['database']
. ActiveRecord::$database_settings[TRAX_ENV]['hostspec']
. ActiveRecord::$database_settings[TRAX_ENV]['username']
. "'\nDid you configure file "
. "/database.ini correctly?\n";
if(empty($controller_name)) {
Trax::$current_controller_name = $controller_name;
$controller_file = "$this->controller_path/" . $controller_name. "_controller.php";
Trax::$current_controller_path = $controller_file;
$non_scaffolded_actions = array();
$illegal_views = array("index","add","edit","show");
foreach($views as $view) {
$non_scaffolded_actions[] = $view;
$non_scaffolded_actions, true);
if(stristr($controller_name, "/")) {
strrpos($controller_name, "/")+ 1));
strrpos($controller_name, "/")+ 1));
# Generate the controller
include("$this->scaffold_template_path/ controller. php");
echo "error creating controller class file: $controller_file\n";
echo "create $controller_file\n";
echo "exists $controller_file\n";
# Generate the index.phtml view
include("$this->scaffold_template_path/ view_index. phtml");
echo "error creating view file: $view_file\n";
echo "create $view_file\n";
echo "exists $view_file\n";
# Generate the add.phtml view
include("$this->scaffold_template_path/ view_add. phtml");
echo "error creating view file: $view_file\n";
echo "create $view_file\n";
echo "exists $view_file\n";
# Generate the edit.phtml view
include("$this->scaffold_template_path/ view_edit. phtml");
echo "error creating view file: $view_file\n";
echo "create $view_file\n";
echo "exists $view_file\n";
# Generate the show.phtml view
include("$this->scaffold_template_path/ view_show. phtml");
echo "error creating view file: $view_file\n";
echo "create $view_file\n";
echo "exists $view_file\n";
# Generate the partial containing the form elments from the database
require "$this->scaffold_template_path/ form_scaffolding. phtml";
echo "error creating view file: $view_file\n";
echo "create $view_file\n";
echo "exists $view_file\n";
# Generate the layout for the scaffolding
include("$this->scaffold_template_path/ layout. phtml");
echo "error creating layout file: $layout_file\n";
echo "create $layout_file\n";
echo "exists $layout_file\n";
* Create a controller file with optional view methods
* @param string $controller Name of the controller
* @param string[] $views Name(s) of view(s), if any
* @uses controller_class Must be set before call.
* Not changed during call.
* @uses controller_path Must be set before call.
* Not changed during call.
* @uses controller_template_file Must be set before call.
* Not changed during call.
* @todo Should return succeed/fail indication
. $controller . "_controller.php";
// There are some views, add a method for each
// Multiple views in an array
foreach($views as $view) {
$class_methods[] = "\tfunction $view() {\n\t}";
$class_methods = implode("\n\n",$class_methods);
$class_methods = "\tfunction $views() {\n\t}\n\n";
$class_methods,$template);
// No view methods to add, so remove unneeded template
$template = str_replace('[class_methods]', '',$template);
echo "error creating controller class file: "
. $controller_file . "\n";
echo "create $controller_file\n";
echo "error controller template file doesn't exist: "
echo "exists $controller_file\n";
* Create a helper file for a controller
* @param string $controller Name of the controller
* @uses controller_class Must be set before call.
* Not changed during call.
* @uses helper_path Must be set before call.
* Not changed during call.
* @uses helper_template_file Must be set before call.
* Not changed during call.
* @todo Should return succeed/fail indication
$helper_file = "$this->helper_path/". $controller. "_helper.php";
echo "error creating helper file: $helper_file\n";
echo "create $helper_file\n";
echo "error helper template file doesn't exist: "
echo "exists $helper_file\n";
* Create a view file if it doesn't exist
* Create a view file in the Trax work area if the required file
* does not yet exist. Generate the view file contents by
* customizing the view template file with information about the
* controller and view names.
* @param string $view Name of the view
* @param string $controller Name of the controller
* @uses controller_class Must be set before call.
* Not changed during call.
* @uses view_path Must be set before call.
* Not changed during call.
* @uses view_template_file Must be set before call.
* Not changed during call.
* @todo Should return succeed/fail indication
$view_file = "$this->view_path/". $view. ".". Trax::$views_extension;
if(!file_exists($view_file)) {
$template = str_replace('[controller]',$controller,$template);
echo "error creating view file: $view_file\n";
echo "create $view_file\n";
echo "error view template file doesn't exist: "
echo "exists $view_file\n";
* Execute an operating system command
* @param string $cmd Command to be executed
* @todo Replace with calls to filesystem methods
if (substr(PHP_OS, 0, 3) == 'WIN') {
* Replace "< ?php ... ? >" with "<?php ... ?>"
* @param string $string String to be edited
* @return string Edited input string
* Output console help message for "generate controller"
echo "Usage: php generate.php controller ControllerName [view1 view2 ...]\n\n";
echo "\tThe controller generator creates functions for a new controller and\n";
echo "\tThe generator takes a controller name and a list of views as arguments.\n";
echo "\tThe controller name may be given in CamelCase or under_score and should\n";
echo "\tnot be suffixed with 'Controller'. To create a controller within a\n";
echo "\tmodule, specify the controller name as 'folder/controller'.\n";
echo "\tThe generator creates a controller class in app/controllers with view\n";
echo "\ttemplates in app/views/controller_name.\n\n";
echo "\tphp script/generate.php controller CreditCard open debit credit close\n\n";
echo "\tCredit card controller with URLs like /credit_card/debit.\n";
echo "\t\tController: app/controllers/credit_card_controller.php\n";
echo "\t\tViews: app/views/credit_card/debit.phtml [...]\n";
echo "\t\tHelper: app/helpers/credit_card_helper.php\n\n";
echo "Module/Folders Example:\n";
echo "\tphp script/generate.php controller 'admin/credit_card' suspend late_fee\n\n";
echo "\tCredit card admin controller with URLs /admin/credit_card/suspend.\n";
echo "\t\tController: app/controllers/admin/credit_card_controller.php\n";
echo "\t\tViews: app/views/admin/credit_card/suspend.phtml [...]\n";
echo "\t\tHelper: app/helpers/credit_card_helper.php\n\n";
* Output console help message for "generate model"
echo "Usage: php generate.php model ModelName\n";
echo "\tThe model generator creates functions for a new model.\n";
echo "\tThe generator takes a model name as its argument. The model name\n";
echo "\tmay be given in CamelCase or under_score and should not be suffixed\n";
echo "\twith 'Model'. The generator creates a model class in app/models.\n";
echo "\tphp script/generate.php model Account\n";
echo "\tThis will create an Account model:\n";
echo "\t\tModel: app/models/account.php\n\n";
* Output console help message for "generate mailer"
echo "Usage: php script/generate.php mailer MailerName [view1 view2 ...]\n\n";
echo "\tThe mailer generator creates class methods for a new mailer and its views.\n\n";
echo "\tThe generator takes a mailer name and a list of views as arguments.\n";
echo "\tThe mailer name may be given in CamelCase or under_score.\n\n";
echo "\tThe generator creates a mailer class in app/models with view templates\n";
echo "\tin app/views/mailer_name.\n\n";
echo "\tphp script/generate.php mailer Notifications signup forgot_password invoice\n\n";
echo "\tThis will create a Notifications mailer class:\n";
echo "\t\tMailer: app/models/notifications.php\n";
echo "\t\tViews: app/views/notifications/signup.phtml [...]\n\n";
* Output console help message for "generate scaffold"
echo "Usage: php script/generate.php scaffold ModelName [ControllerName] [view1 view2 ...]\n\n";
echo "\tThe scaffold generator creates a controller to interact with a model.\n";
echo "\tIf the model does not exist, it creates the model as well. The\n";
echo "\tgenerated code is equivalent to the ( public \$scaffold = \"model\"; )\n";
echo "\tdeclaration, making it easy to migrate when you wish to customize\n";
echo "\tyour controller and views.\n\n";
echo "\tThe generator takes a model name, an optional controller name, and a\n";
echo "\tlist of views as arguments. Scaffolded actions and views are created\n";
echo "\tautomatically.\n\n";
echo "\tThe auto scaffolded actions and views are:\n";
echo "\t\tindex, show, add, edit, delete\n\n";
echo "\tIf a controller name is not given, the plural form of the model name\n";
echo "\twill be used. The model and controller names may be given in CamelCase\n";
echo "\tor under_score and should not be suffixed with 'Model' or 'Controller'.\n\n";
echo "\tphp script/generate.php scaffold Account Bank debit credit\n\n";
echo "\tThis will generate an Account model and BankController with a basic\n";
echo "\tuser interface. Now create the accounts table in your database and\n";
echo "\t browse to http://localhost/bank/. Voila, you're on Trax!\n\n";
echo "Module/Folders Example:\n";
echo "\tphp script/generate.php scaffold CreditCard 'admin/credit_card' suspend late_fee\n\n";
echo "\tThis will generate a CreditCard model and CreditCardController\n";
echo "\tcontroller in the admin module.\n";
* Output console help message for unrecognized command
echo "Generate Controller:\n";
echo "php script/generate.php controller controller_name [view1 view2 ...]\n";
echo "for more controller info php script/generate.php controller\n\n";
echo "Generate Model:\n";
echo "php script/generate.php model ModelName\n";
echo "for more model info php script/generate.php model\n\n";
echo "Generate Mailer:\n";
echo "php script/generate.php mailer MailerName [view1 view2 ...]\n";
echo "for more mailer info php script/generate.php mailer\n\n";
echo "Generate Scaffold:\n";
echo "php script/generate.php scaffold ModelName [controller_name] [view1 view2 ...]\n";
echo "for more scaffold info php script/generate.php scaffold\n\n";
// -- set Emacs parameters --
// c-hanging-comment-ender-p: nil
|