PageRenderTime 30ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/zendframework/zend-code/src/Generator/TraitUsageGenerator.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 353 lines | 236 code | 53 blank | 64 comment | 34 complexity | a4bb3099acd108fdfd78768a779a9199 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Code\Generator;
  10. use Reflection;
  11. use ReflectionMethod;
  12. class TraitUsageGenerator extends AbstractGenerator
  13. {
  14. /**
  15. * @var ClassGenerator
  16. */
  17. protected $classGenerator;
  18. /**
  19. * @var array Array of trait names
  20. */
  21. protected $traits = array();
  22. /**
  23. * @var array Array of trait aliases
  24. */
  25. protected $traitAliases = array();
  26. /**
  27. * @var array Array of trait overrides
  28. */
  29. protected $traitOverrides = array();
  30. /**
  31. * @var array Array of string names
  32. */
  33. protected $uses = array();
  34. public function __construct(ClassGenerator $classGenerator)
  35. {
  36. $this->classGenerator = $classGenerator;
  37. }
  38. /**
  39. * @inherit Zend\Code\Generator\TraitUsageInterface
  40. */
  41. public function addUse($use, $useAlias = null)
  42. {
  43. if (! empty($useAlias)) {
  44. $use .= ' as ' . $useAlias;
  45. }
  46. $this->uses[$use] = $use;
  47. return $this;
  48. }
  49. /**
  50. * @inherit Zend\Code\Generator\TraitUsageInterface
  51. */
  52. public function getUses()
  53. {
  54. return array_values($this->uses);
  55. }
  56. /**
  57. * @inherit Zend\Code\Generator\TraitUsageInterface
  58. */
  59. public function addTrait($trait)
  60. {
  61. $traitName = $trait;
  62. if (is_array($trait)) {
  63. if (! array_key_exists('traitName', $trait)) {
  64. throw new Exception\InvalidArgumentException('Missing required value for traitName');
  65. }
  66. $traitName = $trait['traitName'];
  67. if (array_key_exists('aliases', $trait)) {
  68. foreach ($trait['aliases'] as $alias) {
  69. $this->addAlias($alias);
  70. }
  71. }
  72. if (array_key_exists('insteadof', $trait)) {
  73. foreach ($trait['insteadof'] as $insteadof) {
  74. $this->addTraitOverride($insteadof);
  75. }
  76. }
  77. }
  78. if (! $this->hasTrait($traitName)) {
  79. $this->traits[] = $traitName;
  80. }
  81. return $this;
  82. }
  83. /**
  84. * @inherit Zend\Code\Generator\TraitUsageInterface
  85. */
  86. public function addTraits(array $traits)
  87. {
  88. foreach ($traits as $trait) {
  89. $this->addTrait($trait);
  90. }
  91. return $this;
  92. }
  93. /**
  94. * @inherit Zend\Code\Generator\TraitUsageInterface
  95. */
  96. public function hasTrait($traitName)
  97. {
  98. return in_array($traitName, $this->traits);
  99. }
  100. /**
  101. * @inherit Zend\Code\Generator\TraitUsageInterface
  102. */
  103. public function getTraits()
  104. {
  105. return $this->traits;
  106. }
  107. /**
  108. * @inherit Zend\Code\Generator\TraitUsageInterface
  109. */
  110. public function removeTrait($traitName)
  111. {
  112. $key = array_search($traitName, $this->traits);
  113. if (false !== $key) {
  114. unset($this->traits[$key]);
  115. }
  116. return $this;
  117. }
  118. /**
  119. * @inherit Zend\Code\Generator\TraitUsageInterface
  120. */
  121. public function addTraitAlias($method, $alias, $visibility = null)
  122. {
  123. $traitAndMethod = $method;
  124. if (is_array($method)) {
  125. if (! array_key_exists('traitName', $method)) {
  126. throw new Exception\InvalidArgumentException('Missing required argument "traitName" for $method');
  127. }
  128. if (! array_key_exists('method', $method)) {
  129. throw new Exception\InvalidArgumentException('Missing required argument "method" for $method');
  130. }
  131. $traitAndMethod = $method['traitName'] . '::' . $method['method'];
  132. }
  133. // Validations
  134. if (false === strpos($traitAndMethod, "::")) {
  135. throw new Exception\InvalidArgumentException(
  136. 'Invalid Format: $method must be in the format of trait::method'
  137. );
  138. }
  139. if (! is_string($alias)) {
  140. throw new Exception\InvalidArgumentException('Invalid Alias: $alias must be a string or array.');
  141. }
  142. if ($this->classGenerator->hasMethod($alias)) {
  143. throw new Exception\InvalidArgumentException('Invalid Alias: Method name already exists on this class.');
  144. }
  145. if (null !== $visibility
  146. && $visibility !== ReflectionMethod::IS_PUBLIC
  147. && $visibility !== ReflectionMethod::IS_PRIVATE
  148. && $visibility !== ReflectionMethod::IS_PROTECTED
  149. ) {
  150. throw new Exception\InvalidArgumentException(
  151. 'Invalid Type: $visibility must of ReflectionMethod::IS_PUBLIC,'
  152. . ' ReflectionMethod::IS_PRIVATE or ReflectionMethod::IS_PROTECTED'
  153. );
  154. }
  155. list($trait, $method) = explode('::', $traitAndMethod);
  156. if (! $this->hasTrait($trait)) {
  157. throw new Exception\InvalidArgumentException('Invalid trait: Trait does not exists on this class');
  158. }
  159. $this->traitAliases[$traitAndMethod] = array(
  160. 'alias' => $alias,
  161. 'visibility' => $visibility
  162. );
  163. return $this;
  164. }
  165. /**
  166. * @inherit Zend\Code\Generator\TraitUsageInterface
  167. */
  168. public function getTraitAliases()
  169. {
  170. return $this->traitAliases;
  171. }
  172. /**
  173. * @inherit Zend\Code\Generator\TraitUsageInterface
  174. */
  175. public function addTraitOverride($method, $traitsToReplace)
  176. {
  177. if (false === is_array($traitsToReplace)) {
  178. $traitsToReplace = array($traitsToReplace);
  179. }
  180. $traitAndMethod = $method;
  181. if (is_array($method)) {
  182. if (! array_key_exists('traitName', $method)) {
  183. throw new Exception\InvalidArgumentException('Missing required argument "traitName" for $method');
  184. }
  185. if (! array_key_exists('method', $method)) {
  186. throw new Exception\InvalidArgumentException('Missing required argument "method" for $method');
  187. }
  188. $traitAndMethod = (string) $method['traitName'] . '::' . (string) $method['method'];
  189. }
  190. // Validations
  191. if (false === strpos($traitAndMethod, "::")) {
  192. throw new Exception\InvalidArgumentException(
  193. 'Invalid Format: $method must be in the format of trait::method'
  194. );
  195. }
  196. list($trait, $method) = explode("::", $traitAndMethod);
  197. if (! $this->hasTrait($trait)) {
  198. throw new Exception\InvalidArgumentException('Invalid trait: Trait does not exists on this class');
  199. }
  200. if (! array_key_exists($traitAndMethod, $this->traitOverrides)) {
  201. $this->traitOverrides[$traitAndMethod] = array();
  202. }
  203. foreach ($traitsToReplace as $traitToReplace) {
  204. if (! is_string($traitToReplace)) {
  205. throw new Exception\InvalidArgumentException(
  206. 'Invalid Argument: $traitToReplace must be a string or array of strings'
  207. );
  208. }
  209. if (! in_array($traitToReplace, $this->traitOverrides[$traitAndMethod])) {
  210. $this->traitOverrides[$traitAndMethod][] = $traitToReplace;
  211. }
  212. }
  213. return $this;
  214. }
  215. /**
  216. * @inherit Zend\Code\Generator\TraitUsageInterface
  217. */
  218. public function removeTraitOverride($method, $overridesToRemove = null)
  219. {
  220. if (! array_key_exists($method, $this->traitOverrides)) {
  221. return $this;
  222. }
  223. if (null === $overridesToRemove) {
  224. unset($this->traitOverrides[$method]);
  225. return $this;
  226. }
  227. $overridesToRemove = (! is_array($overridesToRemove))
  228. ? array($overridesToRemove)
  229. : $overridesToRemove;
  230. foreach ($overridesToRemove as $traitToRemove) {
  231. $key = array_search($traitToRemove, $this->traitOverrides[$method]);
  232. if (false !== $key) {
  233. unset($this->traitOverrides[$method][$key]);
  234. }
  235. }
  236. return $this;
  237. }
  238. /**
  239. * @inherit Zend\Code\Generator\TraitUsageInterface
  240. */
  241. public function getTraitOverrides()
  242. {
  243. return $this->traitOverrides;
  244. }
  245. /**
  246. * @inherit Zend\Code\Generator\GeneratorInterface
  247. */
  248. public function generate()
  249. {
  250. $output = '';
  251. $indent = $this->getIndentation();
  252. $traits = $this->getTraits();
  253. if (empty($traits)) {
  254. return $output;
  255. }
  256. $output .= $indent . 'use ' . implode(', ', $traits);
  257. $aliases = $this->getTraitAliases();
  258. $overrides = $this->getTraitOverrides();
  259. if (empty($aliases) && empty($overrides)) {
  260. $output .= ";" . self::LINE_FEED . self::LINE_FEED;
  261. return $output;
  262. }
  263. $output .= ' {' . self::LINE_FEED;
  264. foreach ($aliases as $method => $alias) {
  265. $visibility = (null !== $alias['visibility'])
  266. ? current(Reflection::getModifierNames($alias['visibility'])) . ' '
  267. : '';
  268. // validation check
  269. if ($this->classGenerator->hasMethod($alias['alias'])) {
  270. throw new Exception\RuntimeException(sprintf(
  271. 'Generation Error: Aliased method %s already exists on this class',
  272. $alias['alias']
  273. ));
  274. }
  275. $output .=
  276. $indent
  277. . $indent
  278. . $method
  279. . ' as '
  280. . $visibility
  281. . $alias['alias']
  282. . ';'
  283. . self::LINE_FEED;
  284. }
  285. foreach ($overrides as $method => $insteadofTraits) {
  286. foreach ($insteadofTraits as $insteadofTrait) {
  287. $output .=
  288. $indent
  289. . $indent
  290. . $method
  291. . ' insteadof '
  292. . $insteadofTrait
  293. . ';'
  294. . self::LINE_FEED;
  295. }
  296. }
  297. $output .= self::LINE_FEED . $indent . '}' . self::LINE_FEED . self::LINE_FEED;
  298. return $output;
  299. }
  300. }