/lib/Cake/Meta/CodeFormat/CakePHP.php

https://github.com/shama/cakephp · PHP · 278 lines · 169 code · 30 blank · 79 comment · 35 complexity · 72339f2117c4cf9aec4c19d881412114 MD5 · raw file

  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @package Cake.Meta.CodeFormat
  12. * @since CakePHP(tm) v 3.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Meta\CodeFormat;
  16. use Cake\Utility\File;
  17. use Cake\Utility\Hash;
  18. /**
  19. * CakePHP Code Format
  20. * Does the actual merging and writing in the CakePHP format for the Meta classes
  21. *
  22. * @package Cake.Meta.CodeFormat
  23. */
  24. trait CakePHP {
  25. /**
  26. * Options for code formatting
  27. *
  28. * @var array
  29. */
  30. protected $_codeFormatOptions = [
  31. 'indent' => "\t",
  32. 'newline' => "\n",
  33. ];
  34. /**
  35. * Merge other Meta objects with this one
  36. *
  37. * @return $this
  38. */
  39. public function merge() {
  40. $args = func_get_args();
  41. foreach ($args as $meta) {
  42. // TODO: Check if a Meta[Type]
  43. $this->_data = Hash::merge($meta->data, $this->_data);
  44. $merge = function($type) use ($meta) {
  45. foreach ($meta->{$type} as $key => $val) {
  46. if (array_key_exists($key, $this->{'_' . $type})) {
  47. $this->{'_' . $type}[$key]->merge($val);
  48. } else {
  49. $this->{'_' . $type}[$key] = $val;
  50. }
  51. }
  52. };
  53. if ($this->_iama === 'class') {
  54. $merge('methods');
  55. $merge('properties');
  56. } else if ($this->_iama === 'method') {
  57. $merge('parameters');
  58. }
  59. }
  60. return $this;
  61. }
  62. /**
  63. * Write code for the given _iama
  64. *
  65. * @return $this
  66. */
  67. public function write() {
  68. $code = $this->{'_write' . ucfirst($this->_iama)}();
  69. // TODO: Write the file
  70. return $this;
  71. }
  72. /**
  73. * Write a class
  74. *
  75. * @return string
  76. */
  77. protected function _writeClass() {
  78. $i = $this->indent;
  79. $nl = $this->newline;
  80. $out = '';
  81. if (!empty($this->_data['namespace'])) {
  82. $out .= 'namespace ' . $this->_data['namespace'] . ';' . $nl . $nl;
  83. }
  84. if (!empty($this->_data['uses'])) {
  85. $uses = array_unique($this->_data['uses']);
  86. foreach ($uses as $use) {
  87. $out .= 'use ' . $use . ';' . $nl;
  88. }
  89. $out .= $nl;
  90. }
  91. $out .= $this->_docblock();
  92. if ($this->_data['trait']) {
  93. $out .= 'trait ';
  94. } else {
  95. $out .= 'class ';
  96. }
  97. $out .= $this->_data['name'];
  98. if ($this->_data['extends']) {
  99. $out .= ' extends ' . $this->_data['extends'];
  100. }
  101. $out .= ' {' . $nl . $nl;
  102. foreach ($this->_properties as $property) {
  103. $out .= $property->write() . $nl;
  104. }
  105. foreach ($this->_methods as $method) {
  106. $out .= $method->write() . $nl;
  107. }
  108. return $out . '}';
  109. }
  110. /**
  111. * Write a property
  112. *
  113. * @return string
  114. */
  115. protected function _writeProperty() {
  116. $nl = $this->newline;
  117. $i = $this->indent;
  118. $out = $this->_docblock();
  119. $out .= $i . $this->_data['access'] . ' $' . $this->_data['name'] . ' = ';
  120. $out .= str_replace($nl, $nl . $i, str_replace(' ', $i, var_export($this->_data['value'], true)));
  121. $out .= ';' . $nl;
  122. return $out;
  123. }
  124. /**
  125. * Write a method
  126. *
  127. * @return string
  128. */
  129. protected function _writeMethod() {
  130. $nl = $this->newline;
  131. $i = $this->indent;
  132. $out = $this->_docblock();
  133. $out .= $i . $this->_data['access'] . ' function ' . $this->_data['name'] . '(';
  134. $params = [];
  135. foreach ($this->parameters as $parameter) {
  136. $params[] = $parameter->write();
  137. }
  138. $out .= implode(', ', $params);
  139. $out .= ') {' . $nl;
  140. $out .= $this->_data['value'] . $nl;
  141. $out .= $i . '}' . $nl;
  142. return $out;
  143. }
  144. /**
  145. * Write a parameter
  146. *
  147. * @return string
  148. */
  149. protected function _writeParameter() {
  150. $out = '';
  151. $scalar = ['integer', 'float', 'string', 'boolean'];
  152. if (!empty($this->_data['type']) && !in_array(strtolower($this->_data['type']), $scalar)) {
  153. $out .= $this->type . ' ';
  154. }
  155. $out .= '$' . $this->_data['name'];
  156. if ($this->_data['hasDefault']) {
  157. $out .= ' = ' . var_export($this->_data['default'], true);
  158. }
  159. return $out;
  160. }
  161. /**
  162. * Return a docblock
  163. *
  164. * @return string
  165. */
  166. protected function _docblock() {
  167. if (!empty($this->_data['docblock'])) {
  168. $docblock = $this->_data['docblock'];
  169. if (!is_array($docblock)) {
  170. $docblock = explode($this->newline, $docblock);
  171. }
  172. if (strpos($docblock[0], '/*') === false) {
  173. array_walk($docblock, function(&$line) {
  174. $line = ' * ' . $line;
  175. });
  176. array_unshift($docblock, '/**');
  177. $docblock[] = ' */';
  178. }
  179. return implode($this->newline, $docblock) . $this->newline;
  180. }
  181. return '';
  182. }
  183. /**
  184. * For setting properties of Meta classes
  185. *
  186. * @param string $name
  187. * @param array $args
  188. * @return $this
  189. */
  190. public function __call($name, $args) {
  191. if ($name === 'data') {
  192. $this->_data = Hash::merge($this->_data, $args[0]);
  193. }
  194. if ($name === 'methods' && isset($this->_methods)) {
  195. $this->_methods = array_merge($this->_methods, $args[0]);
  196. }
  197. if ($name === 'properties' && isset($this->_properties)) {
  198. $this->_properties = array_merge($this->_properties, $args[0]);
  199. }
  200. if ($name === 'parameters' && isset($this->_parameters)) {
  201. $this->_parameters = array_merge($this->_parameters, $args[0]);
  202. }
  203. if (isset($this->_data[$name])) {
  204. if (isset($args[0])) {
  205. $this->_data[$name] = $args[0];
  206. }
  207. }
  208. if (isset($this->_codeFormatOptions[$name])) {
  209. $this->_codeFormatOptions[$name] = $args[0];
  210. }
  211. return $this;
  212. }
  213. /**
  214. * For returning properties of Meta classes
  215. *
  216. * @param string $name
  217. * @return mixed
  218. */
  219. public function __get($name) {
  220. if ($name === 'data') {
  221. return $this->_data;
  222. }
  223. if ($name === 'methods' && isset($this->_methods)) {
  224. return $this->_methods;
  225. }
  226. if ($name === 'properties' && isset($this->_properties)) {
  227. return $this->_properties;
  228. }
  229. if ($name === 'parameters' && isset($this->_parameters)) {
  230. return $this->_parameters;
  231. }
  232. if (isset($this->_data[$name])) {
  233. return $this->_data[$name];
  234. }
  235. if (isset($this->_codeFormatOptions[$name])) {
  236. return $this->_codeFormatOptions[$name];
  237. }
  238. return false;
  239. }
  240. /**
  241. * For returning code instead of writing
  242. *
  243. * @return string
  244. */
  245. public function __toString() {
  246. return $this->{'_write' . ucfirst($this->_iama)}();
  247. }
  248. }