PHPonTrax
[ class tree: PHPonTrax ] [ index: PHPonTrax ] [ all elements ]

Source for file trax_generator.php

Documentation is available at trax_generator.php

  1. <?php
  2. /**
  3. * File containing the TraxGenerator class
  4. *
  5. * (PHP 5)
  6. *
  7. * @package PHPonTrax
  8. * @version $Id$
  9. * @copyright (c) 2005 John Peterson
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining
  12. * a copy of this software and associated documentation files (the
  13. * "Software"), to deal in the Software without restriction, including
  14. * without limitation the rights to use, copy, modify, merge, publish,
  15. * distribute, sublicense, and/or sell copies of the Software, and to
  16. * permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be
  20. * included in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. *
  30. * @package PHPonTrax
  31. */
  32.  
  33. /**
  34. * Generate application files in the Trax work area
  35. *
  36. * Implements the commands of {@link generate.php script/generate.php}
  37. * <p>Legal commands:</p>
  38. * <ul>
  39. * <li>{@link generate_controller() controller}</li>
  40. * <li>{@link generate_model() model}</li>
  41. * <li>{@link generate_scaffold() scaffold}</li>
  42. * </ul>
  43. */
  44. class TraxGenerator {
  45.  
  46. /**
  47. * Filesystem path to the app/views directory in the Trax work area
  48. * @var string
  49. */
  50. private $view_path;
  51.  
  52. /**
  53. * Filesystem path to the app/controllers directory in the Trax work area
  54. * @var string
  55. */
  56. private $controller_path;
  57.  
  58. /**
  59. * Filesystem path to the app/helpers directory in the Trax work area
  60. * @var string
  61. */
  62. private $helper_path;
  63.  
  64. /**
  65. * Filesystem path to the app/model directory in the Trax work area
  66. * @var string
  67. */
  68. private $model_path;
  69.  
  70. /**
  71. * Generated subdirectories in the Trax work area
  72. *
  73. * When a controller is generated with a name that includes '/',
  74. * $extra_path is set to the implied subdirectories.
  75. * @var string
  76. */
  77. private $extra_path;
  78.  
  79. /**
  80. * Platform-dependent command to make a directory
  81. * @var string
  82. */
  83. private $mkdir_cmd;
  84.  
  85. /**
  86. * Filesystem path to the templates/controller.php file
  87. * @var string
  88. */
  89. private $controller_template_file;
  90.  
  91. /**
  92. * Filesystem path to the templates/helper.php file
  93. * @var string
  94. */
  95. private $helper_template_file;
  96.  
  97. /**
  98. * Filesystem path to the templates/view.phtml file
  99. * @var string
  100. */
  101. private $view_template_file;
  102.  
  103. /**
  104. * Filesystem path to the templates/model.php file
  105. * @var string
  106. */
  107. private $model_template_file;
  108.  
  109. /**
  110. * Filesystem path to templates/scaffolds/generator_templates directory
  111. * @var string
  112. */
  113. private $scaffold_template_path;
  114.  
  115. /**
  116. * Filesystem path to the app/views/layouts/ directory in the
  117. * Trax work area
  118. * @var string
  119. */
  120. private $layouts_path;
  121.  
  122. /**
  123. * @todo Document this variable
  124. *
  125. * Value is set by {@link generate_controller()} and used by
  126. * {@link generate_scaffold()}
  127. * @var string
  128. */
  129. private $layout_filename;
  130.  
  131. /**
  132. * CamelCase name of the controller class
  133. * @var string
  134. */
  135. private $controller_class;
  136.  
  137. /**
  138. * Value of the view files extension (usually '.phtml')
  139. * @var string
  140. */
  141. public $view_file_extention = TRAX_VIEWS_EXTENTION;
  142.  
  143. /**
  144. * Constructor for the TraxGenerator object
  145. *
  146. * Compute and store filesystem paths to the various
  147. * subdirectories of the Trax work area and the template files
  148. * used to generate application files
  149. * @uses $GLOBALS['TRAX_INCLUDES']
  150. * @global string[] $GLOBALS['TRAX_INCLUDES'] Array of paths to
  151. * various Trax directories
  152. * @uses controller_path
  153. * @uses controller_template_file
  154. * @uses helper_path
  155. * @uses helper_template_file
  156. * @uses layouts_path
  157. * @uses model_path
  158. * @uses model_template_file
  159. * @uses scaffold_template_path
  160. * @uses view_path
  161. * @uses view_template_file
  162. */
  163. function __construct() {
  164. $this->view_path =
  165. TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['views'];
  166. $this->controller_path =
  167. TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['controllers'];
  168. $this->helper_path =
  169. TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['helpers'];
  170. $this->model_path =
  171. TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['models'];
  172. $this->layouts_path =
  173. TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['layouts'];
  174. $this->controller_template_file =
  175. TRAX_LIB_ROOT . "/templates/controller.php";
  176. $this->helper_template_file =
  177. TRAX_LIB_ROOT . "/templates/helper.php";
  178. $this->view_template_file =
  179. TRAX_LIB_ROOT . "/templates/view.".$this->view_file_extention;
  180. $this->model_template_file =
  181. TRAX_LIB_ROOT . "/templates/model.php";
  182. $this->scaffold_template_path =
  183. TRAX_LIB_ROOT . "/templates/scaffolds/generator_templates";
  184.  
  185. if (substr(PHP_OS, 0, 3) == 'WIN') {
  186. $this->mkdir_cmd = "mkdir";
  187. } else {
  188. $this->mkdir_cmd = "mkdir -p";
  189. }
  190. }
  191.  
  192. /**
  193. * Parse command line and carry out the command
  194. *
  195. * Command line arguments, if any are in $_SERVER['argv']
  196. * @uses controller_help()
  197. * @uses generate_controller()
  198. * @uses generate_model()
  199. * @uses generate_scaffold()
  200. * @uses generator_help()
  201. * @uses model_help()
  202. * @uses scaffold_help()
  203. */
  204. function run() {
  205.  
  206. // If command line arguments exist, parse them
  207. if (array_key_exists('argv', $_SERVER)) {
  208. if (array_key_exists(1, $_SERVER['argv'])) {
  209. $command = strtolower($_SERVER["argv"][1]);
  210. }
  211. if (array_key_exists(2, $_SERVER['argv'])) {
  212. $command_name = $_SERVER["argv"][2];
  213. }
  214. }
  215.  
  216. // Execute command or output a diagnostic
  217. if(empty($command)) {
  218. $this->generator_help();
  219. } else {
  220. switch($command) {
  221.  
  222. // Process "controller" command
  223. case "controller":
  224. if(empty($command_name)) {
  225. $this->controller_help();
  226. } else {
  227. $views = array();
  228. if(array_key_exists(3, $_SERVER['argv'])
  229. && ($_SERVER["argv"][3] != "")) {
  230. for($i=3;$i < count($_SERVER["argv"]);$i++) {
  231. $views[] = strtolower($_SERVER["argv"][$i]);
  232. }
  233. }
  234. $this->generate_controller($command_name, $views);
  235. }
  236. break;
  237.  
  238. // Process "model" command
  239. // $command_name is the name of the model
  240. case "model":
  241. if(empty($command_name)) {
  242. $this->model_help();
  243. } else {
  244. $this->generate_model($command_name);
  245. }
  246. break;
  247.  
  248. // Process "scaffold" command
  249. // $command_name has the name of the model
  250. // $_SERVER['argv'][3] has the name of the controller
  251. case "scaffold":
  252.  
  253. // Model name is required
  254. if( empty($command_name) ) {
  255. echo "Error: name of model omitted\n";
  256. $this->scaffold_help();
  257. break;
  258. }
  259.  
  260. // Controller name is optional
  261. if (array_key_exists(3, $_SERVER["argv"])) {
  262. $controller_name = $_SERVER["argv"][3];
  263. } else {
  264. $controller_name = null;
  265. }
  266. // Views are optional following controller name
  267. $views = array();
  268. if (array_key_exists(4, $_SERVER["argv"])
  269. && ($_SERVER["argv"][4] != "")) {
  270. for($i=4;$i < count($_SERVER["argv"]);$i++) {
  271. $views[] = strtolower($_SERVER["argv"][$i]);
  272. }
  273. }
  274. $this->generate_scaffold($command_name,
  275. $controller_name, $views);
  276. break;
  277.  
  278. default:
  279. $this->generator_help();
  280. } // switch($command)
  281. }
  282. return;
  283. }
  284.  
  285. /**
  286. * Implement "generate controller" command
  287. *
  288. * <p>Example:<br><samp>php script/generate.php controller</samp>
  289. * <i>SomeName</i><br>
  290. * will generate:</p>
  291. * <ul>
  292. * <li>a file
  293. * <samp>app/controllers/</samp><i>some_name</i><samp>_controller.php</samp><br>
  294. * containing the class definition<br>
  295. * <samp>class</samp> <i>SomeName</i><samp>Controller extends
  296. * ApplicationController {}</samp></li>
  297. * <li>a file
  298. * <samp>app/helpers/</samp><i>some_name</i><samp>_helper.php</samp></li>
  299. * <li>a directory
  300. * <samp>app/views/</samp><i>some_name</i></li>
  301. * </ul>
  302. *
  303. * <p>Optionally, one or more views can be appended to the command:<br>
  304. * <samp>php script/generate.php controller</samp>
  305. * <i>SomeName view1 view2</i><br>
  306. * which will additionally generate files:<br>
  307. * <samp>app/views/</samp><i>some_name/view1</i><samp>.phtml</samp><br>
  308. * <samp>app/views/</samp><i>some_name/view2</i><samp>.phtml</samp></p>
  309. *
  310. * @param string $name Name in CamelCase of the controller to generate.
  311. * The value may include '/' which will cause
  312. * creation of subdirectories indicated to
  313. * hold the controller and view files.
  314. * @param string $views Optional list of views to generate
  315. * @param boolean $scaffolding
  316. * @uses Inflector::underscore()
  317. * @uses $controller_class Set during call
  318. * @uses $controller_path Must be set before call.
  319. * @uses create_controller()
  320. * @uses create_helper()
  321. * @uses create_view()
  322. * @uses $extra_path Set during call
  323. * @uses $helper_path Must be set before call.
  324. * @uses $layouts_path Must be set before call.
  325. * @uses $layout_filename Set during call
  326. * @uses $view_path Must be set before call.
  327. */
  328. function generate_controller($name, $views="", $scaffolding = false) {
  329.  
  330. # Set the View and Controller extra path info
  331. if(stristr($name, "/")) {
  332. $this->extra_path = substr($name,0,strrpos($name, "/"));
  333. $name = Inflector::underscore(substr($name,strrpos($name, "/")+1));
  334. $this->view_path .= "/$this->extra_path/$name";
  335. $this->layouts_path .= "/$this->extra_path";
  336. $this->controller_path .= "/$this->extra_path";
  337. $this->helper_path .= "/$this->extra_path";
  338. } else {
  339. $name = Inflector::underscore($name);
  340. $this->view_path .= "/$name";
  341. }
  342. $this->layout_filename = $name;
  343. $this->controller_class = Inflector::camelize($name);
  344.  
  345. # Create the extra folders for View / Controller
  346. if(file_exists($this->view_path)) {
  347. echo "exists $this->view_path\n";
  348. } else{
  349. $this->exec("$this->mkdir_cmd $this->view_path");
  350. echo "create $this->view_path\n";
  351. }
  352.  
  353. if(file_exists($this->controller_path)) {
  354. echo "exists $this->controller_path\n";
  355. } else {
  356. $this->exec("$this->mkdir_cmd $this->controller_path");
  357. echo "create $this->controller_path\n";
  358. }
  359.  
  360. if(file_exists($this->helper_path)) {
  361. echo "exists $this->helper_path\n";
  362. } else {
  363. $this->exec("$this->mkdir_cmd $this->helper_path");
  364. echo "create $this->helper_path\n";
  365. }
  366.  
  367. # Create the actual controller/helper files
  368. if(!$scaffolding) {
  369. $this->create_controller($name, $views);
  370. }
  371. $this->create_helper($name);
  372.  
  373. if($this->extra_path) {
  374. $name = $this->extra_path."/".$name;
  375. }
  376.  
  377. # Create view files if any
  378. if(is_array($views)) {
  379. foreach($views as $view) {
  380. $this->create_view($view,$name);
  381. }
  382. } elseif(!empty($views)) {
  383. $this->create_view($views,$name);
  384. }
  385. }
  386.  
  387. /**
  388. * Implement the "generate model" command
  389. *
  390. * <p>Example:<br><samp>php script/generate.php model</samp>
  391. * <i>SomeName</i><br>
  392. * will generate a file
  393. * <samp>app/models/</samp><i>some_name</i><samp>.php</samp><br>
  394. * containing the class definition<br>
  395. * <samp>class</samp> <i>SomeName</i> <samp>extends
  396. * ActiveRecord {}</samp>
  397. * @param string $name Name of the model. May be in either
  398. * under_score or CamelCase. If no '_' exists in
  399. * $name it is treated as CamelCase.
  400. * @uses Inflector::underscore()
  401. * @uses model_path Must be set before call.
  402. * Not changed during call.
  403. * @uses model_template_file Must be set before call.
  404. * Not changed during call.
  405. */
  406. function generate_model($name) {
  407.  
  408. if(stristr($name, "_")) {
  409. $model_file = $this->model_path."/".strtolower($name).".php";
  410. } else {
  411. $model_file = $this->model_path."/".Inflector::underscore($name).".php";
  412. }
  413.  
  414. $model_class = Inflector::camelize($name);
  415.  
  416. if(!file_exists($model_file)) {
  417. if(file_exists($this->model_template_file)) {
  418. $template = file_get_contents($this->model_template_file);
  419. $template = str_replace('[class_name]',$model_class,$template);
  420. if(!file_put_contents($model_file,$template)) {
  421. echo "error creating model file: $model_file\n";
  422. } else {
  423. echo "create $model_file\n";
  424. return true;
  425. }
  426. } else {
  427. echo "error model template file doesn't exist: $this->model_template_file\n";
  428. }
  429. } else {
  430. echo "exists $model_file\n";
  431. return true;
  432. }
  433. return false;
  434. }
  435. /**
  436. * Implement the "generate scaffold" command
  437. *
  438. * @param string $model_name
  439. * @param string $controller_name
  440. * @param string $views
  441. * @uses generate_controller()
  442. * @uses generate_model()
  443. * @uses Inflector::classify()
  444. * @uses Inflector::humanize()
  445. * @uses Inflector::pluralize()
  446. * @uses Inflector::singularize()
  447. * @uses Inflector::underscore()
  448. * @uses $layout_filename Set as output from
  449. * generate_controller().
  450. * Not changed afterward.
  451. * @uses fix_php_brackets()
  452. */
  453. function generate_scaffold($model_name, $controller_name, $views="") {
  454. //echo 'generate_scaffold("'.$model_name.'", "'
  455. // .$controller_name.'", "'.$views.'")'."\n";
  456. if(!$model_exists = $this->generate_model($model_name)) {
  457. echo "Error - Can't create Model: $model_name.\n";
  458. return;
  459. }
  460.  
  461. $GLOBALS['current_controller_object'] =& $this;
  462. $model_class_name = Inflector::classify($model_name);
  463. $singular_model_name = Inflector::singularize($model_name);
  464. $plural_model_name = Inflector::pluralize($model_name);
  465. $human_model_name = Inflector::humanize($model_name);
  466.  
  467. try {
  468. $this->{$singular_model_name} = new $model_class_name();
  469. } catch (ActiveRecordError $e) {
  470. echo "Can't create model.\n";
  471. echo $e->getMessage()."\n";
  472. echo "for database '"
  473. . $GLOBALS['TRAX_DB_SETTINGS'][TRAX_MODE]['database']
  474. . "' on host '"
  475. . $GLOBALS['TRAX_DB_SETTINGS'][TRAX_MODE]['hostspec']
  476. . "' as user '"
  477. . $GLOBALS['TRAX_DB_SETTINGS'][TRAX_MODE]['username']
  478. . "'\nDid you configure file "
  479. . TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['config']
  480. . "/database.ini correctly?\n";
  481. die();
  482. }
  483. if(empty($controller_name)) {
  484. $controller_name = Inflector::underscore($model_name);
  485. } else {
  486. $controller_name = Inflector::underscore($controller_name);
  487. }
  488. $GLOBALS['current_controller_name'] = $controller_name;
  489. $controller_file =
  490. "$this->controller_path/" . $controller_name."_controller.php";
  491. $GLOBALS['current_controller_path'] = $controller_file;
  492. $non_scaffolded_actions = array();
  493. $illegal_views = array("index","add","edit","show");
  494. if(is_array($views)) {
  495. foreach($views as $view) {
  496. if(!in_array($view, $illegal_views)) {
  497. $non_scaffolded_actions[] = $view;
  498. }
  499. }
  500. }
  501. $this->generate_controller($controller_name,
  502. $non_scaffolded_actions, true);
  503. if(stristr($controller_name, "/")) {
  504. $controller_class_name =
  505. Inflector::classify(substr($controller_name,
  506. strrpos($controller_name, "/")+1));
  507. $human_controller_name =
  508. Inflector::humanize(substr($controller_name,
  509. strrpos($controller_name, "/")+1));
  510. } else {
  511. $controller_class_name = Inflector::classify($controller_name);
  512. $human_controller_name = Inflector::humanize($controller_name);
  513. }
  514. # Generate the controller
  515. ob_start();
  516. include("$this->scaffold_template_path/controller.php");
  517. $controller_contents = $this->fix_php_brackets(ob_get_contents());
  518. ob_end_clean();
  519. if(!file_exists($controller_file)) {
  520. if(!file_put_contents($controller_file, $controller_contents)) {
  521. echo "error creating controller class file: $controller_file\n";
  522. } else {
  523. echo "create $controller_file\n";
  524. }
  525. } else {
  526. echo "exists $controller_file\n";
  527. }
  528. # Generate the index.phtml view
  529. $view_file = "$this->view_path/index.".$this->view_file_extention;
  530. ob_start();
  531. include("$this->scaffold_template_path/view_index.phtml");
  532. $index_contents = $this->fix_php_brackets(ob_get_contents());
  533. ob_end_clean();
  534. if(!file_exists($view_file)) {
  535. if(!file_put_contents($view_file, $index_contents)) {
  536. echo "error creating view file: $view_file\n";
  537. } else {
  538. echo "create $view_file\n";
  539. }
  540. } else {
  541. echo "exists $view_file\n";
  542. }
  543. # Generate the add.phtml view
  544. $view_file = "$this->view_path/add.".$this->view_file_extention;
  545. ob_start();
  546. include("$this->scaffold_template_path/view_add.phtml");
  547. $add_contents = $this->fix_php_brackets(ob_get_contents());
  548. ob_end_clean();
  549. if(!file_exists($view_file)) {
  550. if(!file_put_contents($view_file, $add_contents)) {
  551. echo "error creating view file: $view_file\n";
  552. } else {
  553. echo "create $view_file\n";
  554. }
  555. } else {
  556. echo "exists $view_file\n";
  557. }
  558. # Generate the edit.phtml view
  559. $view_file = "$this->view_path/edit.".$this->view_file_extention;
  560. ob_start();
  561. include("$this->scaffold_template_path/view_edit.phtml");
  562. $edit_contents = $this->fix_php_brackets(ob_get_contents());
  563. ob_end_clean();
  564. if(!file_exists($view_file)) {
  565. if(!file_put_contents($view_file, $edit_contents)) {
  566. echo "error creating view file: $view_file\n";
  567. } else {
  568. echo "create $view_file\n";
  569. }
  570. } else {
  571. echo "exists $view_file\n";
  572. }
  573. # Generate the show.phtml view
  574. $view_file = "$this->view_path/show.".$this->view_file_extention;
  575. ob_start();
  576. include("$this->scaffold_template_path/view_show.phtml");
  577. $show_contents = $this->fix_php_brackets(ob_get_contents());
  578. ob_end_clean();
  579. if(!file_exists($view_file)) {
  580. if(!file_put_contents($view_file, $show_contents)) {
  581. echo "error creating view file: $view_file\n";
  582. } else {
  583. echo "create $view_file\n";
  584. }
  585. } else {
  586. echo "exists $view_file\n";
  587. }
  588. # Generate the partial containing the form elments from the database
  589. $view_file = "$this->view_path/_form.".$this->view_file_extention;
  590. ob_start();
  591. require "$this->scaffold_template_path/form_scaffolding.phtml";
  592. $_form_contents = $this->fix_php_brackets(ob_get_contents());
  593. ob_end_clean();
  594. if(!file_exists($view_file)) {
  595. if(!file_put_contents($view_file, $_form_contents)) {
  596. echo "error creating view file: $view_file\n";
  597. } else {
  598. echo "create $view_file\n";
  599. }
  600. } else {
  601. echo "exists $view_file\n";
  602. }
  603. # Generate the layout for the scaffolding
  604. $layout_file = $this->layouts_path."/"
  605. . $this->layout_filename."."
  606. . $this->view_file_extention;
  607. if(!file_exists($this->layouts_path)) {
  608. mkdir($this->layouts_path);
  609. }
  610. ob_start();
  611. include("$this->scaffold_template_path/layout.phtml");
  612. $layout_contents = $this->fix_php_brackets(ob_get_contents());
  613. ob_end_clean();
  614. if(!file_exists($layout_file)) {
  615. if(!file_put_contents($layout_file, $layout_contents)) {
  616. echo "error creating layout file: $layout_file\n";
  617. } else {
  618. echo "create $layout_file\n";
  619. }
  620. } else {
  621. echo "exists $layout_file\n";
  622. }
  623. }
  624.  
  625. /**
  626. * Create a controller file with optional view methods
  627. *
  628. * @param string $controller Name of the controller
  629. * @param string[] $views Name(s) of view(s), if any
  630. * @uses controller_class Must be set before call.
  631. * Not changed during call.
  632. * @uses controller_path Must be set before call.
  633. * Not changed during call.
  634. * @uses controller_template_file Must be set before call.
  635. * Not changed during call.
  636. * @todo Should return succeed/fail indication
  637. */
  638. function create_controller($controller,$views="") {
  639.  
  640. $controller_file = "$this->controller_path/"
  641. . $controller . "_controller.php";
  642.  
  643. if(!file_exists($controller_file)) {
  644. if(file_exists($this->controller_template_file)) {
  645. $template = file_get_contents($this->controller_template_file);
  646. $template = str_replace('[class_name]',
  647. $this->controller_class,$template);
  648. // Add view methods
  649. if (!empty($views)) {
  650.  
  651. // There are some views, add a method for each
  652. if(is_array($views)) {
  653.  
  654. // Multiple views in an array
  655. foreach($views as $view) {
  656. $classMethods[] = "\tfunction $view() {\n\t}";
  657. }
  658. $classMethods = implode("\n\n",$classMethods);
  659. } else {
  660. $classMethods = "\tfunction $views() {\n\t}\n\n";
  661. }
  662. $template = str_replace('[class_methods]',
  663. $classMethods,$template);
  664. } else {
  665.  
  666. // No view methods to add, so remove unneeded template
  667. $template = str_replace('[class_methods]', '',$template);
  668. }
  669.  
  670. if(!file_put_contents($controller_file,$template)) {
  671. echo "error creating controller class file: "
  672. . $controller_file . "\n";
  673. } else {
  674. echo "create $controller_file\n";
  675. }
  676.  
  677. } else {
  678. echo "error controller template file doesn't exist: "
  679. . $this->controller_template_file . "\n";
  680. }
  681. } else {
  682. echo "exists $controller_file\n";
  683. }
  684. }
  685.  
  686. /**
  687. * Create a helper file for a controller
  688. *
  689. * @param string $controller Name of the controller
  690. * @uses controller_class Must be set before call.
  691. * Not changed during call.
  692. * @uses helper_path Must be set before call.
  693. * Not changed during call.
  694. * @uses helper_template_file Must be set before call.
  695. * Not changed during call.
  696. * @todo Should return succeed/fail indication
  697. */
  698. function create_helper($controller) {
  699. $helper_file = "$this->helper_path/".$controller."_helper.php";
  700. if(!file_exists($helper_file)) {
  701. if(file_exists($this->helper_template_file)) {
  702. $template = file_get_contents($this->helper_template_file);
  703. $template = str_replace('[class_name]',
  704. $this->controller_class,$template);
  705. if(!file_put_contents($helper_file,$template)) {
  706. echo "error creating helper file: $helper_file\n";
  707. } else {
  708. echo "create $helper_file\n";
  709. }
  710.  
  711. } else {
  712. echo "error helper template file doesn't exist: "
  713. . $this->helper_template_file . "\n";
  714. }
  715. } else {
  716. echo "exists $helper_file\n";
  717. }
  718. }
  719.  
  720. /**
  721. * Create a view file if it doesn't exist
  722. *
  723. * Create a view file in the Trax work area if the required file
  724. * does not yet exist. Generate the view file contents by
  725. * customizing the view template file with information about the
  726. * controller and view names.
  727. *
  728. * @param string $view Name of the view
  729. * @param string $controller Name of the controller
  730. * @uses controller_class Must be set before call.
  731. * Not changed during call.
  732. * @uses view_file_extension Must be set before call.
  733. * Not changed during call.
  734. * @uses view_path Must be set before call.
  735. * Not changed during call.
  736. * @uses view_template_file Must be set before call.
  737. * Not changed during call.
  738. * @todo Should return succeed/fail indication
  739. */
  740. function create_view($view, $controller) {
  741. $view_file = "$this->view_path/".$view.".".$this->view_file_extention;
  742. if(!file_exists($view_file)) {
  743. if(file_exists($this->view_template_file)) {
  744. $template = file_get_contents($this->view_template_file);
  745. $template = str_replace('[class_name]',
  746. $this->controller_class,$template);
  747. $template = str_replace('[controller]',$controller,$template);
  748. $template = str_replace('[view]',$view,$template);
  749. if(!file_put_contents($view_file,$template)) {
  750. echo "error creating view file: $view_file\n";
  751. } else {
  752. echo "create $view_file\n";
  753. }
  754. } else {
  755. echo "error view template file doesn't exist: "
  756. . $this->view_template_file . "\n";
  757. }
  758. } else {
  759. echo "exists $view_file\n";
  760. }
  761. }
  762.  
  763. /**
  764. * Execute an operating system command
  765. *
  766. * @param string $cmd Command to be executed
  767. * @todo Replace with calls to filesystem methods
  768. */
  769. function exec($cmd) {
  770. if (substr(PHP_OS, 0, 3) == 'WIN') {
  771. exec(str_replace("/","\\",$cmd));
  772. } else {
  773. exec($cmd);
  774. }
  775. }
  776. /**
  777. * Replace "< ?php ... ? >" with "<?php ... ?>"
  778. *
  779. * @param string $string String to be edited
  780. * @return string Edited input string
  781. */
  782. function fix_php_brackets($string) {
  783. return str_replace("? >", "?>",
  784. str_replace("< ?php", "<?php", $string));
  785. }
  786.  
  787. /**
  788. * Output console help message for "generate controller"
  789. */
  790. function controller_help() {
  791. echo "Usage: php generate.php controller ControllerName [view1 view2 ...]\n\n";
  792. echo "Description:\n";
  793. echo "\tThe controller generator creates functions for a new controller and\n";
  794. echo"\tits views.\n\n";
  795. echo "\tThe generator takes a controller name and a list of views as arguments.\n";
  796. echo "\tThe controller name may be given in CamelCase or under_score and should\n";
  797. echo "\tnot be suffixed with 'Controller'. To create a controller within a\n";
  798. echo "\tmodule, specify the controller name as 'folder/controller'.\n";
  799. echo "\tThe generator creates a controller class in app/controllers with view\n";
  800. echo "\ttemplates in app/views/controller_name.\n\n";
  801. echo "Example:\n";
  802. echo "\tphp script/generate.php controller CreditCard open debit credit close\n\n";
  803. echo "\tCredit card controller with URLs like /credit_card/debit.\n";
  804. echo "\t\tController: app/controllers/credit_card_controller.php\n";
  805. echo "\t\tViews: app/views/credit_card/debit.phtml [...]\n";
  806. echo "\t\tHelper: app/helpers/credit_card_helper.php\n\n";
  807. echo "Module/Folders Example:\n";
  808. echo "\tphp script/generate.php controller 'admin/credit_card' suspend late_fee\n\n";
  809. echo "\tCredit card admin controller with URLs /admin/credit_card/suspend.\n";
  810. echo "\t\tController: app/controllers/admin/credit_card_controller.php\n";
  811. echo "\t\tViews: app/views/admin/credit_card/suspend.phtml [...]\n";
  812. echo "\t\tHelper: app/helpers/credit_card_helper.php\n\n";
  813. }
  814.  
  815. /**
  816. * Output console help message for "generate model"
  817. */
  818. function model_help() {
  819. echo "Usage: php generate.php model ModelName\n";
  820. echo "Description:\n";
  821. echo "\tThe model generator creates functions for a new model.\n";
  822. echo "\tThe generator takes a model name as its argument. The model name\n";
  823. echo "\tmay be given in CamelCase or under_score and should not be suffixed\n";
  824. echo "\twith 'Model'. The generator creates a model class in app/models.\n";
  825. echo "Example:\n";
  826. echo "\tphp script/generate.php model Account\n";
  827. echo "\tThis will create an Account model:\n";
  828. echo "\t\tModel: app/models/account.php\n\n";
  829. }
  830. /**
  831. * Output console help message for "generate scaffold"
  832. */
  833. function scaffold_help() {
  834. echo "Usage: php script/generate.php scaffold ModelName [ControllerName] [view1 view2 ...]\n\n";
  835. echo "Description:\n";
  836. echo "\tThe scaffold generator creates a controller to interact with a model.\n";
  837. echo "\tIf the model does not exist, it creates the model as well. The\n";
  838. echo "\tgenerated code is equivalent to the ( public \$scaffold = \"model\"; )\n";
  839. echo "\tdeclaration, making it easy to migrate when you wish to customize\n";
  840. echo "\tyour controller and views.\n\n";
  841. echo "\tThe generator takes a model name, an optional controller name, and a\n";
  842. echo "\tlist of views as arguments. Scaffolded actions and views are created\n";
  843. echo "\tautomatically.\n\n";
  844. echo "\tThe auto scaffolded actions and views are:\n";
  845. echo "\t\tindex, show, add, edit, delete\n\n";
  846. echo "\tIf a controller name is not given, the plural form of the model name\n";
  847. echo "\twill be used. The model and controller names may be given in CamelCase\n";
  848. echo "\tor under_score and should not be suffixed with 'Model' or 'Controller'.\n\n";
  849. echo "Example:\n";
  850. echo "\tphp script/generate.php scaffold Account Bank debit credit\n\n";
  851. echo "\tThis will generate an Account model and BankController with a basic\n";
  852. echo "\tuser interface. Now create the accounts table in your database and\n";
  853. echo "\t browse to http://localhost/bank/. Voila, you're on Trax!\n\n";
  854. echo "Module/Folders Example:\n";
  855. echo "\tphp script/generate.php scaffold CreditCard 'admin/credit_card' suspend late_fee\n\n";
  856. echo "\tThis will generate a CreditCard model and CreditCardController\n";
  857. echo "\tcontroller in the admin module.\n";
  858. }
  859.  
  860. /**
  861. * Output console help message for unrecognized command
  862. */
  863. function generator_help() {
  864. echo "Usage:\n";
  865. echo "Generate Controller:\n";
  866. echo "php script/generate.php controller controller_name [view1 view2 ..]\n";
  867. echo "for more controller info php script/generate.php controller\n\n";
  868. echo "Generate Model:\n";
  869. echo "php script/generate.php model ModelName\n";
  870. echo "for more model info php script/generate.php model\n\n";
  871. echo "Generate Scaffold:\n";
  872. echo "php script/generate.php scaffold ModelName [controller_name] [view1 view2 ...]\n";
  873. echo "for more scaffold info php script/generate.php scaffold\n\n";
  874. }
  875. }
  876.  
  877. // -- set Emacs parameters --
  878. // Local variables:
  879. // tab-width: 4
  880. // c-basic-offset: 4
  881. // c-hanging-comment-ender-p: nil
  882. // indent-tabs-mode: nil
  883. // End:
  884. ?>

Documentation generated on Thu, 04 May 2006 19:48:03 -0600 by phpDocumentor 1.3.0RC4