View.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\console\command\create;
  9. use lithium\util\Inflector;
  10. use lithium\util\String;
  11. /**
  12. * Generate a View file in the `--library` namespace
  13. *
  14. * `li3 create view Posts index`
  15. * `li3 create --library=li3_plugin view Posts index`
  16. *
  17. */
  18. class View extends \lithium\console\command\Create {
  19. /**
  20. * Returns the name of the controller class, minus `'Controller'`.
  21. *
  22. * @param string $request
  23. * @return string
  24. */
  25. protected function _name($request) {
  26. return Inflector::camelize(Inflector::pluralize($request->action));
  27. }
  28. /**
  29. * Get the plural data variable that is sent down from controller method.
  30. *
  31. * @param string $request
  32. * @return string
  33. */
  34. protected function _plural($request) {
  35. return Inflector::pluralize(Inflector::camelize($request->action, false));
  36. }
  37. /**
  38. * Get the singular data variable that is sent down from controller methods.
  39. *
  40. * @param string $request
  41. * @return string
  42. */
  43. protected function _singular($request) {
  44. return Inflector::singularize(Inflector::camelize($request->action, false));
  45. }
  46. /**
  47. * Override the save method to handle view specific params.
  48. *
  49. * @param array $params
  50. * @return mixed
  51. */
  52. protected function _save(array $params = array()) {
  53. $params['path'] = Inflector::underscore($this->request->action);
  54. $params['file'] = $this->request->args(0);
  55. $contents = $this->_template();
  56. $result = String::insert($contents, $params);
  57. if (!empty($this->_library['path'])) {
  58. $path = $this->_library['path'] . "/views/{$params['path']}/{$params['file']}";
  59. $file = str_replace('//', '/', "{$path}.php");
  60. $directory = dirname($file);
  61. if (!is_dir($directory)) {
  62. if (!mkdir($directory, 0755, true)) {
  63. return false;
  64. }
  65. }
  66. $directory = str_replace($this->_library['path'] . '/', '', $directory);
  67. if (file_exists($file)) {
  68. $prompt = "{$file} already exists. Overwrite?";
  69. $choices = array('y', 'n');
  70. if ($this->in($prompt, compact('choices')) !== 'y') {
  71. return "{$params['file']} skipped.";
  72. }
  73. }
  74. if (is_int(file_put_contents($file, $result))) {
  75. return "{$params['file']}.php created in {$directory}.";
  76. }
  77. }
  78. return false;
  79. }
  80. }
  81. ?>