scaffold.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Oil;
  13. /**
  14. * Oil\Scaffold Class
  15. *
  16. * @package Fuel
  17. * @subpackage Oil
  18. * @category Core
  19. */
  20. class Generate_Scaffold
  21. {
  22. public static $fields_regex = '/([a-z0-9_]+):([a-z0-9_]+)(\[([0-9]+)\])?/i';
  23. public static $view_subdir = 'scaffolding/';
  24. public static $controller_prefix = '';
  25. public static $model_prefix = '';
  26. public static $controller_parent = 'Controller_Template';
  27. public static function _init()
  28. {
  29. Generate::$scaffolding = true;
  30. }
  31. /**
  32. * Forge
  33. *
  34. * @param array Fields mainly
  35. * @param string Subfolder (or admin "theme") where views are held
  36. * @return mixed
  37. */
  38. public static function forge($args, $subfolder)
  39. {
  40. $data = array();
  41. $subfolder = trim($subfolder, '/');
  42. if ( ! is_dir(\Package::exists('oil').'views/'.static::$view_subdir.$subfolder))
  43. {
  44. throw new Exception('The subfolder for admin templates does not exist or is spelled wrong: '.$subfolder.' ');
  45. }
  46. // Go through all arguments after the first and make them into field arrays
  47. $data['fields'] = array();
  48. foreach (array_slice($args, 1) as $arg)
  49. {
  50. // Parse the argument for each field in a pattern of name:type[constraint]
  51. preg_match(static::$fields_regex, $arg, $matches);
  52. if ( ! isset($matches[1]))
  53. {
  54. throw new Exception('One or more fields were badly specified. Ensure they are name:type');
  55. }
  56. $data['fields'][] = array(
  57. 'name' => \Str::lower($matches[1]),
  58. 'type' => isset($matches[2]) ? $matches[2] : 'string',
  59. 'constraint' => isset($matches[4]) ? $matches[4] : null
  60. );
  61. }
  62. $name = array_shift($args);
  63. // Replace / with _ and classify the rest. DO NOT singularize
  64. $controller_name = \Inflector::classify(static::$controller_prefix.str_replace(DS, '_', $name), false);
  65. // Replace / with _ and classify the rest. Singularize
  66. $model_name = \Inflector::classify(static::$model_prefix.str_replace(DS, '_', $name));
  67. // Either foo or folder/foo
  68. $view_path = $controller_path = str_replace(
  69. array('_', '-'),
  70. DS,
  71. \Str::lower($controller_name)
  72. );
  73. // Models are always singular, tough!
  74. $model_path = str_replace(
  75. array('_', '-'),
  76. DS,
  77. \Str::lower($model_name)
  78. );
  79. // uri's have forward slashes, DS is a backslash on Windows
  80. $uri = str_replace(DS, '/', $controller_path);
  81. $data['include_timestamps'] = ( ! \Cli::option('no-timestamp', false));
  82. // If a folder is used, the entity is the last part
  83. $name_parts = explode(DS, $name);
  84. $data['singular_name'] = \Inflector::singularize(end($name_parts));
  85. $data['plural_name'] = \Cli::option('singular') ? $data['singular_name'] : \Inflector::pluralize($data['singular_name']);
  86. $data['table'] = \Inflector::tableize($model_name);
  87. $data['controller_parent'] = static::$controller_parent;
  88. /** Generate the Migration **/
  89. $migration_args = $args;
  90. // add timestamps to the table if needded
  91. if ($data['include_timestamps'])
  92. {
  93. if (\Cli::option('mysql-timestamp', false))
  94. {
  95. $migration_args[] = 'created_at:date:null[1]';
  96. $migration_args[] = 'updated_at:date:null[1]';
  97. }
  98. else
  99. {
  100. $migration_args[] = 'created_at:int:null[1]';
  101. $migration_args[] = 'updated_at:int:null[1]';
  102. }
  103. }
  104. array_unshift($migration_args, 'create_'.\Inflector::pluralize(\Str::lower($name)));
  105. Generate::migration($migration_args, false);
  106. // Merge some other data in
  107. $data = array_merge(compact(array('controller_name', 'model_name', 'model_path', 'view_path', 'uri')), $data);
  108. /** Generate the Model **/
  109. $model = \View::forge(static::$view_subdir.$subfolder.'/model', $data);
  110. Generate::create(
  111. APPPATH.'classes/model/'.$model_path.'.php',
  112. $model,
  113. 'model'
  114. );
  115. /** Generate the Controller **/
  116. $controller = \View::forge(static::$view_subdir.$subfolder.'/controller', $data);
  117. $controller->actions = array(
  118. array(
  119. 'name' => 'index',
  120. 'params' => '',
  121. 'code' => \View::forge(static::$view_subdir.$subfolder.'/actions/index', $data),
  122. ),
  123. array(
  124. 'name' => 'view',
  125. 'params' => '$id = null',
  126. 'code' => \View::forge(static::$view_subdir.$subfolder.'/actions/view', $data),
  127. ),
  128. array(
  129. 'name' => 'create',
  130. 'params' => '',
  131. 'code' => \View::forge(static::$view_subdir.$subfolder.'/actions/create', $data),
  132. ),
  133. array(
  134. 'name' => 'edit',
  135. 'params' => '$id = null',
  136. 'code' => \View::forge(static::$view_subdir.$subfolder.'/actions/edit', $data),
  137. ),
  138. array(
  139. 'name' => 'delete',
  140. 'params' => '$id = null',
  141. 'code' => \View::forge(static::$view_subdir.$subfolder.'/actions/delete', $data),
  142. ),
  143. );
  144. Generate::create(
  145. APPPATH.'classes/controller/'.$controller_path.'.php',
  146. $controller,
  147. 'controller'
  148. );
  149. // Create each of the views
  150. foreach (array('index', 'view', 'create', 'edit', '_form') as $view)
  151. {
  152. Generate::create(
  153. APPPATH.'views/'.$controller_path.'/'.$view.'.php',
  154. \View::forge(static::$view_subdir.$subfolder.'/views/actions/'.$view, $data),
  155. 'view'
  156. );
  157. }
  158. // Add the default template if it doesnt exist
  159. if ( ! file_exists($app_template = APPPATH.'views/template.php'))
  160. {
  161. // check if there's a template in app, and if so, use that
  162. if (file_exists(APPPATH.'views/'.static::$view_subdir.$subfolder.'/views/template.php'))
  163. {
  164. Generate::create($app_template, file_get_contents(APPPATH.'views/'.static::$view_subdir.$subfolder.'/views/template.php'), 'view');
  165. }
  166. else
  167. {
  168. Generate::create($app_template, file_get_contents(\Package::exists('oil').'views/'.static::$view_subdir.'template.php'), 'view');
  169. }
  170. }
  171. Generate::build();
  172. }
  173. }
  174. /* End of file oil/classes/scaffold.php */