PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/cake/console/libs/tasks/view.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 498 lines | 324 code | 37 blank | 137 comment | 50 complexity | a88676fe76b4644995e7c328bb8df2b7 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * The View Tasks handles creating and updating view files.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  16. * @subpackage cake.cake.console.libs.tasks
  17. * @since CakePHP(tm) v 1.2
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::import('Controller', 'Controller', false);
  21. include_once dirname(__FILE__) . DS . 'bake.php';
  22. /**
  23. * Task class for creating and updating view files.
  24. *
  25. * @package cake
  26. * @subpackage cake.cake.console.libs.tasks
  27. */
  28. class ViewTask extends BakeTask {
  29. /**
  30. * Tasks to be loaded by this Task
  31. *
  32. * @var array
  33. * @access public
  34. */
  35. var $tasks = array('Project', 'Controller', 'DbConfig', 'Template');
  36. /**
  37. * path to VIEWS directory
  38. *
  39. * @var array
  40. * @access public
  41. */
  42. var $path = VIEWS;
  43. /**
  44. * Name of the controller being used
  45. *
  46. * @var string
  47. * @access public
  48. */
  49. var $controllerName = null;
  50. /**
  51. * Path to controller to put views
  52. *
  53. * @var string
  54. * @access public
  55. */
  56. var $controllerPath = null;
  57. /**
  58. * The template file to use
  59. *
  60. * @var string
  61. * @access public
  62. */
  63. var $template = null;
  64. /**
  65. * Actions to use for scaffolding
  66. *
  67. * @var array
  68. * @access public
  69. */
  70. var $scaffoldActions = array('index', 'view', 'add', 'edit');
  71. /**
  72. * An array of action names that don't require templates. These
  73. * actions will not emit errors when doing bakeActions()
  74. *
  75. * @var array
  76. * @access public
  77. */
  78. var $noTemplateActions = array('delete');
  79. /**
  80. * Override initialize
  81. *
  82. * @access public
  83. */
  84. function initialize() {
  85. }
  86. /**
  87. * Execution method always used for tasks
  88. *
  89. * @access public
  90. */
  91. function execute() {
  92. if (empty($this->args)) {
  93. $this->__interactive();
  94. }
  95. if (empty($this->args[0])) {
  96. return;
  97. }
  98. if (!isset($this->connection)) {
  99. $this->connection = 'default';
  100. }
  101. $controller = $action = $alias = null;
  102. $this->controllerName = $this->_controllerName($this->args[0]);
  103. $this->controllerPath = $this->_controllerPath($this->controllerName);
  104. $this->Project->interactive = false;
  105. if (strtolower($this->args[0]) == 'all') {
  106. return $this->all();
  107. }
  108. if (isset($this->args[1])) {
  109. $this->template = $this->args[1];
  110. }
  111. if (isset($this->args[2])) {
  112. $action = $this->args[2];
  113. }
  114. if (!$action) {
  115. $action = $this->template;
  116. }
  117. if ($action) {
  118. return $this->bake($action, true);
  119. }
  120. $vars = $this->__loadController();
  121. $methods = $this->_methodsToBake();
  122. foreach ($methods as $method) {
  123. $content = $this->getContent($method, $vars);
  124. if ($content) {
  125. $this->bake($method, $content);
  126. }
  127. }
  128. }
  129. /**
  130. * Get a list of actions that can / should have views baked for them.
  131. *
  132. * @return array Array of action names that should be baked
  133. */
  134. function _methodsToBake() {
  135. $methods = array_diff(
  136. array_map('strtolower', get_class_methods($this->controllerName . 'Controller')),
  137. array_map('strtolower', get_class_methods('appcontroller'))
  138. );
  139. $scaffoldActions = false;
  140. if (empty($methods)) {
  141. $scaffoldActions = true;
  142. $methods = $this->scaffoldActions;
  143. }
  144. $adminRoute = $this->Project->getPrefix();
  145. foreach ($methods as $i => $method) {
  146. if ($adminRoute && isset($this->params['admin'])) {
  147. if ($scaffoldActions) {
  148. $methods[$i] = $adminRoute . $method;
  149. continue;
  150. } elseif (strpos($method, $adminRoute) === false) {
  151. unset($methods[$i]);
  152. }
  153. }
  154. if ($method[0] === '_' || $method == strtolower($this->controllerName . 'Controller')) {
  155. unset($methods[$i]);
  156. }
  157. }
  158. return $methods;
  159. }
  160. /**
  161. * Bake All views for All controllers.
  162. *
  163. * @return void
  164. */
  165. function all() {
  166. $this->Controller->interactive = false;
  167. $tables = $this->Controller->listAll($this->connection, false);
  168. $actions = null;
  169. if (isset($this->args[1])) {
  170. $actions = array($this->args[1]);
  171. }
  172. $this->interactive = false;
  173. foreach ($tables as $table) {
  174. $model = $this->_modelName($table);
  175. $this->controllerName = $this->_controllerName($model);
  176. $this->controllerPath = Inflector::underscore($this->controllerName);
  177. if (App::import('Model', $model)) {
  178. $vars = $this->__loadController();
  179. if (!$actions) {
  180. $actions = $this->_methodsToBake();
  181. }
  182. $this->bakeActions($actions, $vars);
  183. $actions = null;
  184. }
  185. }
  186. }
  187. /**
  188. * Handles interactive baking
  189. *
  190. * @access private
  191. */
  192. function __interactive() {
  193. $this->hr();
  194. $this->out(sprintf("Bake View\nPath: %s", $this->path));
  195. $this->hr();
  196. $this->DbConfig->interactive = $this->Controller->interactive = $this->interactive = true;
  197. if (empty($this->connection)) {
  198. $this->connection = $this->DbConfig->getConfig();
  199. }
  200. $this->Controller->connection = $this->connection;
  201. $this->controllerName = $this->Controller->getName();
  202. $this->controllerPath = strtolower(Inflector::underscore($this->controllerName));
  203. $prompt = sprintf(__("Would you like bake to build your views interactively?\nWarning: Choosing no will overwrite %s views if it exist.", true), $this->controllerName);
  204. $interactive = $this->in($prompt, array('y', 'n'), 'y');
  205. if (strtolower($interactive) == 'n') {
  206. $this->interactive = false;
  207. }
  208. $prompt = __("Would you like to create some CRUD views\n(index, add, view, edit) for this controller?\nNOTE: Before doing so, you'll need to create your controller\nand model classes (including associated models).", true);
  209. $wannaDoScaffold = $this->in($prompt, array('y','n'), 'y');
  210. $wannaDoAdmin = $this->in(__("Would you like to create the views for admin routing?", true), array('y','n'), 'y');
  211. if (strtolower($wannaDoScaffold) == 'y' || strtolower($wannaDoAdmin) == 'y') {
  212. $vars = $this->__loadController();
  213. if (strtolower($wannaDoScaffold) == 'y') {
  214. $actions = $this->scaffoldActions;
  215. $vars['scaffoldPrefix'] = null;
  216. $this->bakeActions($actions, $vars);
  217. }
  218. if (strtolower($wannaDoAdmin) == 'y') {
  219. $admin = $this->Project->getPrefix();
  220. $regularActions = $this->scaffoldActions;
  221. $adminActions = array();
  222. foreach ($regularActions as $action) {
  223. $adminActions[] = $admin . $action;
  224. }
  225. $vars['scaffoldPrefix'] = $admin;
  226. $this->bakeActions($adminActions, $vars);
  227. }
  228. $this->hr();
  229. $this->out();
  230. $this->out(__("View Scaffolding Complete.\n", true));
  231. } else {
  232. $this->customAction();
  233. }
  234. }
  235. /**
  236. * Loads Controller and sets variables for the template
  237. * Available template variables
  238. * 'modelClass', 'primaryKey', 'displayField', 'singularVar', 'pluralVar',
  239. * 'singularHumanName', 'pluralHumanName', 'fields', 'foreignKeys',
  240. * 'belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'
  241. *
  242. * @return array Returns an variables to be made available to a view template
  243. * @access private
  244. */
  245. function __loadController() {
  246. if (!$this->controllerName) {
  247. $this->err(__('Controller not found', true));
  248. }
  249. $import = $this->controllerName;
  250. if ($this->plugin) {
  251. $import = $this->plugin . '.' . $this->controllerName;
  252. }
  253. if (!App::import('Controller', $import)) {
  254. $file = $this->controllerPath . '_controller.php';
  255. $this->err(sprintf(__("The file '%s' could not be found.\nIn order to bake a view, you'll need to first create the controller.", true), $file));
  256. $this->_stop();
  257. }
  258. $controllerClassName = $this->controllerName . 'Controller';
  259. $controllerObj =& new $controllerClassName();
  260. $controllerObj->plugin = $this->plugin;
  261. $controllerObj->constructClasses();
  262. $modelClass = $controllerObj->modelClass;
  263. $modelObj =& $controllerObj->{$controllerObj->modelClass};
  264. if ($modelObj) {
  265. $primaryKey = $modelObj->primaryKey;
  266. $displayField = $modelObj->displayField;
  267. $singularVar = Inflector::variable($modelClass);
  268. $singularHumanName = $this->_singularHumanName($this->controllerName);
  269. $schema = $modelObj->schema(true);
  270. $fields = array_keys($schema);
  271. $associations = $this->__associations($modelObj);
  272. $scaffoldForbiddenActions = $modelObj->scaffoldForbiddenActions;
  273. } else {
  274. $primaryKey = $displayField = null;
  275. $singularVar = Inflector::variable(Inflector::singularize($this->controllerName));
  276. $singularHumanName = $this->_singularHumanName($this->controllerName);
  277. $fields = $schema = $associations = array();
  278. }
  279. $controllerMethods = get_class_methods($controllerClassName);
  280. $pluralVar = Inflector::variable($this->controllerName);
  281. $pluralHumanName = $this->_pluralHumanName($this->controllerName);
  282. return compact('modelClass', 'schema', 'primaryKey', 'displayField', 'singularVar', 'pluralVar',
  283. 'singularHumanName', 'pluralHumanName', 'fields','associations', 'scaffoldForbiddenActions');
  284. }
  285. /**
  286. * Bake a view file for each of the supplied actions
  287. *
  288. * @param array $actions Array of actions to make files for.
  289. * @return void
  290. */
  291. function bakeActions($actions, $vars) {
  292. foreach ($actions as $action) {
  293. $content = $this->getContent($action, $vars);
  294. $this->bake($action, $content);
  295. }
  296. }
  297. /**
  298. * handle creation of baking a custom action view file
  299. *
  300. * @return void
  301. */
  302. function customAction() {
  303. $action = '';
  304. while ($action == '') {
  305. $action = $this->in(__('Action Name? (use lowercase_underscored function name)', true));
  306. if ($action == '') {
  307. $this->out(__('The action name you supplied was empty. Please try again.', true));
  308. }
  309. }
  310. $this->out();
  311. $this->hr();
  312. $this->out(__('The following view will be created:', true));
  313. $this->hr();
  314. $this->out(sprintf(__('Controller Name: %s', true), $this->controllerName));
  315. $this->out(sprintf(__('Action Name: %s', true), $action));
  316. $this->out(sprintf(__('Path: %s', true), $this->params['app'] . DS . $this->controllerPath . DS . Inflector::underscore($action) . ".ctp"));
  317. $this->hr();
  318. $looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y');
  319. if (strtolower($looksGood) == 'y') {
  320. $this->bake($action);
  321. $this->_stop();
  322. } else {
  323. $this->out(__('Bake Aborted.', true));
  324. }
  325. }
  326. /**
  327. * Assembles and writes bakes the view file.
  328. *
  329. * @param string $action Action to bake
  330. * @param string $content Content to write
  331. * @return boolean Success
  332. * @access public
  333. */
  334. function bake($action, $content = '') {
  335. if ($content === true) {
  336. $content = $this->getContent($action);
  337. }
  338. if (empty($content)) {
  339. return false;
  340. }
  341. $path = $this->getPath();
  342. $filename = $path . $this->controllerPath . DS . Inflector::underscore($action) . '.ctp';
  343. return $this->createFile($filename, $content);
  344. }
  345. /**
  346. * Builds content from template and variables
  347. *
  348. * @param string $action name to generate content to
  349. * @param array $vars passed for use in templates
  350. * @return string content from template
  351. * @access public
  352. */
  353. function getContent($action, $vars = null) {
  354. if (!$vars) {
  355. $vars = $this->__loadController();
  356. }
  357. $this->Template->set('action', $action);
  358. $this->Template->set('plugin', $this->plugin);
  359. $this->Template->set($vars);
  360. $template = $this->getTemplate($action);
  361. if ($template) {
  362. return $this->Template->generate('views', $template);
  363. }
  364. return false;
  365. }
  366. /**
  367. * Gets the template name based on the action name
  368. *
  369. * @param string $action name
  370. * @return string template name
  371. * @access public
  372. */
  373. function getTemplate($action) {
  374. if ($action != $this->template && in_array($action, $this->noTemplateActions)) {
  375. return false;
  376. }
  377. if (!empty($this->template) && $action != $this->template) {
  378. return $this->template;
  379. }
  380. $template = $action;
  381. $prefixes = Configure::read('Routing.prefixes');
  382. foreach ((array)$prefixes as $prefix) {
  383. if (strpos($template, $prefix) !== false) {
  384. $template = str_replace($prefix . '_', '', $template);
  385. }
  386. }
  387. if (in_array($template, array('add', 'edit'))) {
  388. $template = 'form';
  389. } elseif (preg_match('@(_add|_edit)$@', $template)) {
  390. $template = str_replace(array('_add', '_edit'), '_form', $template);
  391. }
  392. return $template;
  393. }
  394. /**
  395. * Displays help contents
  396. *
  397. * @access public
  398. */
  399. function help() {
  400. $this->hr();
  401. $this->out("Usage: cake bake view <arg1> <arg2>...");
  402. $this->hr();
  403. $this->out('Arguments:');
  404. $this->out();
  405. $this->out("<controller>");
  406. $this->out("\tName of the controller views to bake. Can use Plugin.name");
  407. $this->out("\tas a shortcut for plugin baking.");
  408. $this->out();
  409. $this->out("<action>");
  410. $this->out("\tName of the action view to bake");
  411. $this->out();
  412. $this->out('Commands:');
  413. $this->out();
  414. $this->out("view <controller>");
  415. $this->out("\tWill read the given controller for methods");
  416. $this->out("\tand bake corresponding views.");
  417. $this->out("\tUsing the -admin flag will only bake views for actions");
  418. $this->out("\tthat begin with Routing.admin.");
  419. $this->out("\tIf var scaffold is found it will bake the CRUD actions");
  420. $this->out("\t(index,view,add,edit)");
  421. $this->out();
  422. $this->out("view <controller> <action>");
  423. $this->out("\tWill bake a template. core templates: (index, add, edit, view)");
  424. $this->out();
  425. $this->out("view <controller> <template> <alias>");
  426. $this->out("\tWill use the template specified");
  427. $this->out("\tbut name the file based on the alias");
  428. $this->out();
  429. $this->out("view all");
  430. $this->out("\tBake all CRUD action views for all controllers.");
  431. $this->out("\tRequires that models and controllers exist.");
  432. $this->_stop();
  433. }
  434. /**
  435. * Returns associations for controllers models.
  436. *
  437. * @return array $associations
  438. * @access private
  439. */
  440. function __associations(&$model) {
  441. $keys = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
  442. $associations = array();
  443. foreach ($keys as $key => $type) {
  444. foreach ($model->{$type} as $assocKey => $assocData) {
  445. $associations[$type][$assocKey]['primaryKey'] = $model->{$assocKey}->primaryKey;
  446. $associations[$type][$assocKey]['displayField'] = $model->{$assocKey}->displayField;
  447. $associations[$type][$assocKey]['foreignKey'] = $assocData['foreignKey'];
  448. $associations[$type][$assocKey]['controller'] = Inflector::pluralize(Inflector::underscore($assocData['className']));
  449. $associations[$type][$assocKey]['fields'] = array_keys($model->{$assocKey}->schema(true));
  450. $associations[$type][$assocKey]['scaffoldForbiddenActions'] = $model->{$assocKey}->scaffoldForbiddenActions;
  451. $associations[$type][$assocKey]['scaffoldActions'] = $model->{$assocKey}->scaffoldActions;
  452. }
  453. }
  454. return $associations;
  455. }
  456. }