/src/Prettus/Repository/Generators/Generator.php

https://github.com/andersao/l5-repository · PHP · 368 lines · 166 code · 61 blank · 141 comment · 13 complexity · 5e35cdb6a7af873b05b14e4f06153d69 MD5 · raw file

  1. <?php
  2. namespace Prettus\Repository\Generators;
  3. use Illuminate\Filesystem\Filesystem;
  4. use Illuminate\Support\Str;
  5. /**
  6. * Class Generator
  7. * @package Prettus\Repository\Generators
  8. * @author Anderson Andrade <contato@andersonandra.de>
  9. */
  10. abstract class Generator
  11. {
  12. /**
  13. * The filesystem instance.
  14. *
  15. * @var \Illuminate\Filesystem\Filesystem
  16. */
  17. protected $filesystem;
  18. /**
  19. * The array of options.
  20. *
  21. * @var array
  22. */
  23. protected $options;
  24. /**
  25. * The shortname of stub.
  26. *
  27. * @var string
  28. */
  29. protected $stub;
  30. /**
  31. * Create new instance of this class.
  32. *
  33. * @param array $options
  34. */
  35. public function __construct(array $options = [])
  36. {
  37. $this->filesystem = new Filesystem;
  38. $this->options = $options;
  39. }
  40. /**
  41. * Get the filesystem instance.
  42. *
  43. * @return \Illuminate\Filesystem\Filesystem
  44. */
  45. public function getFilesystem()
  46. {
  47. return $this->filesystem;
  48. }
  49. /**
  50. * Set the filesystem instance.
  51. *
  52. * @param \Illuminate\Filesystem\Filesystem $filesystem
  53. *
  54. * @return $this
  55. */
  56. public function setFilesystem(Filesystem $filesystem)
  57. {
  58. $this->filesystem = $filesystem;
  59. return $this;
  60. }
  61. /**
  62. * Get stub template for generated file.
  63. *
  64. * @return string
  65. */
  66. public function getStub()
  67. {
  68. $path = config('repository.generator.stubsOverridePath', __DIR__);
  69. if(!file_exists($path . '/Stubs/' . $this->stub . '.stub')){
  70. $path = __DIR__;
  71. }
  72. return (new Stub($path . '/Stubs/' . $this->stub . '.stub', $this->getReplacements()))->render();
  73. }
  74. /**
  75. * Get template replacements.
  76. *
  77. * @return array
  78. */
  79. public function getReplacements()
  80. {
  81. return [
  82. 'class' => $this->getClass(),
  83. 'namespace' => $this->getNamespace(),
  84. 'root_namespace' => $this->getRootNamespace()
  85. ];
  86. }
  87. /**
  88. * Get base path of destination file.
  89. *
  90. * @return string
  91. */
  92. public function getBasePath()
  93. {
  94. return base_path();
  95. }
  96. /**
  97. * Get destination path for generated file.
  98. *
  99. * @return string
  100. */
  101. public function getPath()
  102. {
  103. return $this->getBasePath() . '/' . $this->getName() . '.php';
  104. }
  105. /**
  106. * Get name input.
  107. *
  108. * @return string
  109. */
  110. public function getName()
  111. {
  112. $name = $this->name;
  113. if (Str::contains($this->name, '\\')) {
  114. $name = str_replace('\\', '/', $this->name);
  115. }
  116. if (Str::contains($this->name, '/')) {
  117. $name = str_replace('/', '/', $this->name);
  118. }
  119. return Str::studly(str_replace(' ', '/', ucwords(str_replace('/', ' ', $name))));
  120. }
  121. /**
  122. * Get application namespace
  123. *
  124. * @return string
  125. */
  126. public function getAppNamespace()
  127. {
  128. return \Illuminate\Container\Container::getInstance()->getNamespace();
  129. }
  130. /**
  131. * Get class name.
  132. *
  133. * @return string
  134. */
  135. public function getClass()
  136. {
  137. return Str::studly(class_basename($this->getName()));
  138. }
  139. /**
  140. * Get paths of namespace.
  141. *
  142. * @return array
  143. */
  144. public function getSegments()
  145. {
  146. return explode('/', $this->getName());
  147. }
  148. /**
  149. * Get root namespace.
  150. *
  151. * @return string
  152. */
  153. public function getRootNamespace()
  154. {
  155. return config('repository.generator.rootNamespace', $this->getAppNamespace());
  156. }
  157. /**
  158. * Get class-specific output paths.
  159. *
  160. * @param $class
  161. *
  162. * @return string
  163. */
  164. public function getConfigGeneratorClassPath($class, $directoryPath = false)
  165. {
  166. switch ($class) {
  167. case ('models' === $class):
  168. $path = config('repository.generator.paths.models', 'Entities');
  169. break;
  170. case ('repositories' === $class):
  171. $path = config('repository.generator.paths.repositories', 'Repositories');
  172. break;
  173. case ('interfaces' === $class):
  174. $path = config('repository.generator.paths.interfaces', 'Repositories');
  175. break;
  176. case ('presenters' === $class):
  177. $path = config('repository.generator.paths.presenters', 'Presenters');
  178. break;
  179. case ('transformers' === $class):
  180. $path = config('repository.generator.paths.transformers', 'Transformers');
  181. break;
  182. case ('validators' === $class):
  183. $path = config('repository.generator.paths.validators', 'Validators');
  184. break;
  185. case ('controllers' === $class):
  186. $path = config('repository.generator.paths.controllers', 'Http\Controllers');
  187. break;
  188. case ('provider' === $class):
  189. $path = config('repository.generator.paths.provider', 'RepositoryServiceProvider');
  190. break;
  191. case ('criteria' === $class):
  192. $path = config('repository.generator.paths.criteria', 'Criteria');
  193. break;
  194. default:
  195. $path = '';
  196. }
  197. if ($directoryPath) {
  198. $path = str_replace('\\', '/', $path);
  199. } else {
  200. $path = str_replace('/', '\\', $path);
  201. }
  202. return $path;
  203. }
  204. abstract public function getPathConfigNode();
  205. /**
  206. * Get class namespace.
  207. *
  208. * @return string
  209. */
  210. public function getNamespace()
  211. {
  212. $segments = $this->getSegments();
  213. array_pop($segments);
  214. $rootNamespace = $this->getRootNamespace();
  215. if ($rootNamespace == false) {
  216. return null;
  217. }
  218. return 'namespace ' . rtrim($rootNamespace . '\\' . implode('\\', $segments), '\\') . ';';
  219. }
  220. /**
  221. * Setup some hook.
  222. *
  223. * @return void
  224. */
  225. public function setUp()
  226. {
  227. //
  228. }
  229. /**
  230. * Run the generator.
  231. *
  232. * @return int
  233. * @throws FileAlreadyExistsException
  234. */
  235. public function run()
  236. {
  237. $this->setUp();
  238. if ($this->filesystem->exists($path = $this->getPath()) && !$this->force) {
  239. throw new FileAlreadyExistsException($path);
  240. }
  241. if (!$this->filesystem->isDirectory($dir = dirname($path))) {
  242. $this->filesystem->makeDirectory($dir, 0777, true, true);
  243. }
  244. return $this->filesystem->put($path, $this->getStub());
  245. }
  246. /**
  247. * Get options.
  248. *
  249. * @return string
  250. */
  251. public function getOptions()
  252. {
  253. return $this->options;
  254. }
  255. /**
  256. * Determinte whether the given key exist in options array.
  257. *
  258. * @param string $key
  259. *
  260. * @return boolean
  261. */
  262. public function hasOption($key)
  263. {
  264. return array_key_exists($key, $this->options);
  265. }
  266. /**
  267. * Get value from options by given key.
  268. *
  269. * @param string $key
  270. * @param string|null $default
  271. *
  272. * @return string
  273. */
  274. public function getOption($key, $default = null)
  275. {
  276. if (!$this->hasOption($key)) {
  277. return $default;
  278. }
  279. return $this->options[$key] ?: $default;
  280. }
  281. /**
  282. * Helper method for "getOption".
  283. *
  284. * @param string $key
  285. * @param string|null $default
  286. *
  287. * @return string
  288. */
  289. public function option($key, $default = null)
  290. {
  291. return $this->getOption($key, $default);
  292. }
  293. /**
  294. * Handle call to __get method.
  295. *
  296. * @param string $key
  297. *
  298. * @return string|mixed
  299. */
  300. public function __get($key)
  301. {
  302. if (property_exists($this, $key)) {
  303. return $this->{$key};
  304. }
  305. return $this->option($key);
  306. }
  307. }