PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Foundation/Console/AppNameCommand.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 329 lines | 280 code | 12 blank | 37 comment | 0 complexity | ffa70c5adcb3dbe7a30553164b2d7443 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Foundation\Console;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Foundation\Composer;
  5. use Symfony\Component\Finder\Finder;
  6. use Illuminate\Filesystem\Filesystem;
  7. use Symfony\Component\Console\Input\InputArgument;
  8. class AppNameCommand extends Command
  9. {
  10. /**
  11. * The console command name.
  12. *
  13. * @var string
  14. */
  15. protected $name = 'app:name';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Set the application namespace';
  22. /**
  23. * The Composer class instance.
  24. *
  25. * @var \Illuminate\Foundation\Composer
  26. */
  27. protected $composer;
  28. /**
  29. * The filesystem instance.
  30. *
  31. * @var \Illuminate\Filesystem\Filesystem
  32. */
  33. protected $files;
  34. /**
  35. * Current root application namespace.
  36. *
  37. * @var string
  38. */
  39. protected $currentRoot;
  40. /**
  41. * Create a new key generator command.
  42. *
  43. * @param \Illuminate\Foundation\Composer $composer
  44. * @param \Illuminate\Filesystem\Filesystem $files
  45. * @return void
  46. */
  47. public function __construct(Composer $composer, Filesystem $files)
  48. {
  49. parent::__construct();
  50. $this->files = $files;
  51. $this->composer = $composer;
  52. }
  53. /**
  54. * Execute the console command.
  55. *
  56. * @return void
  57. */
  58. public function fire()
  59. {
  60. $this->currentRoot = trim($this->laravel->getNamespace(), '\\');
  61. $this->setBootstrapNamespaces();
  62. $this->setAppDirectoryNamespace();
  63. $this->setConfigNamespaces();
  64. $this->setComposerNamespace();
  65. $this->setDatabaseFactoryNamespaces();
  66. $this->setPhpSpecNamespace();
  67. $this->info('Application namespace set!');
  68. $this->composer->dumpAutoloads();
  69. $this->call('clear-compiled');
  70. }
  71. /**
  72. * Set the namespace on the files in the app directory.
  73. *
  74. * @return void
  75. */
  76. protected function setAppDirectoryNamespace()
  77. {
  78. $files = Finder::create()
  79. ->in($this->laravel['path'])
  80. ->name('*.php');
  81. foreach ($files as $file) {
  82. $this->replaceNamespace($file->getRealPath());
  83. }
  84. }
  85. /**
  86. * Replace the App namespace at the given path.
  87. *
  88. * @param string $path
  89. * @return void
  90. */
  91. protected function replaceNamespace($path)
  92. {
  93. $search = [
  94. 'namespace '.$this->currentRoot.';',
  95. $this->currentRoot.'\\',
  96. ];
  97. $replace = [
  98. 'namespace '.$this->argument('name').';',
  99. $this->argument('name').'\\',
  100. ];
  101. $this->replaceIn($path, $search, $replace);
  102. }
  103. /**
  104. * Set the bootstrap namespaces.
  105. *
  106. * @return void
  107. */
  108. protected function setBootstrapNamespaces()
  109. {
  110. $search = [
  111. $this->currentRoot.'\\Http',
  112. $this->currentRoot.'\\Console',
  113. $this->currentRoot.'\\Exceptions',
  114. ];
  115. $replace = [
  116. $this->argument('name').'\\Http',
  117. $this->argument('name').'\\Console',
  118. $this->argument('name').'\\Exceptions',
  119. ];
  120. $this->replaceIn($this->getBootstrapPath(), $search, $replace);
  121. }
  122. /**
  123. * Set the PSR-4 namespace in the Composer file.
  124. *
  125. * @return void
  126. */
  127. protected function setComposerNamespace()
  128. {
  129. $this->replaceIn(
  130. $this->getComposerPath(), str_replace('\\', '\\\\', $this->currentRoot).'\\\\', str_replace('\\', '\\\\', $this->argument('name')).'\\\\'
  131. );
  132. }
  133. /**
  134. * Set the namespace in the appropriate configuration files.
  135. *
  136. * @return void
  137. */
  138. protected function setConfigNamespaces()
  139. {
  140. $this->setAppConfigNamespaces();
  141. $this->setAuthConfigNamespace();
  142. $this->setServicesConfigNamespace();
  143. }
  144. /**
  145. * Set the application provider namespaces.
  146. *
  147. * @return void
  148. */
  149. protected function setAppConfigNamespaces()
  150. {
  151. $search = [
  152. $this->currentRoot.'\\Providers',
  153. $this->currentRoot.'\\Http\\Controllers\\',
  154. ];
  155. $replace = [
  156. $this->argument('name').'\\Providers',
  157. $this->argument('name').'\\Http\\Controllers\\',
  158. ];
  159. $this->replaceIn($this->getConfigPath('app'), $search, $replace);
  160. }
  161. /**
  162. * Set the authentication User namespace.
  163. *
  164. * @return void
  165. */
  166. protected function setAuthConfigNamespace()
  167. {
  168. $this->replaceIn(
  169. $this->getAuthConfigPath(), $this->currentRoot.'\\User', $this->argument('name').'\\User'
  170. );
  171. }
  172. /**
  173. * Set the services User namespace.
  174. *
  175. * @return void
  176. */
  177. protected function setServicesConfigNamespace()
  178. {
  179. $this->replaceIn(
  180. $this->getServicesConfigPath(), $this->currentRoot.'\\User', $this->argument('name').'\\User'
  181. );
  182. }
  183. /**
  184. * Set the PHPSpec configuration namespace.
  185. *
  186. * @return void
  187. */
  188. protected function setPhpSpecNamespace()
  189. {
  190. if ($this->files->exists($path = $this->getPhpSpecConfigPath())) {
  191. $this->replaceIn($path, $this->currentRoot, $this->argument('name'));
  192. }
  193. }
  194. /**
  195. * Set the namespace in database factory files.
  196. *
  197. * @return void
  198. */
  199. protected function setDatabaseFactoryNamespaces()
  200. {
  201. $this->replaceIn(
  202. $this->laravel->databasePath().'/factories/ModelFactory.php', $this->currentRoot, $this->argument('name')
  203. );
  204. }
  205. /**
  206. * Replace the given string in the given file.
  207. *
  208. * @param string $path
  209. * @param string|array $search
  210. * @param string|array $replace
  211. * @return void
  212. */
  213. protected function replaceIn($path, $search, $replace)
  214. {
  215. $this->files->put($path, str_replace($search, $replace, $this->files->get($path)));
  216. }
  217. /**
  218. * Get the path to the bootstrap/app.php file.
  219. *
  220. * @return string
  221. */
  222. protected function getBootstrapPath()
  223. {
  224. return $this->laravel->basePath().'/bootstrap/app.php';
  225. }
  226. /**
  227. * Get the path to the Composer.json file.
  228. *
  229. * @return string
  230. */
  231. protected function getComposerPath()
  232. {
  233. return $this->laravel->basePath().'/composer.json';
  234. }
  235. /**
  236. * Get the path to the given configuration file.
  237. *
  238. * @param string $name
  239. * @return string
  240. */
  241. protected function getConfigPath($name)
  242. {
  243. return $this->laravel['path.config'].'/'.$name.'.php';
  244. }
  245. /**
  246. * Get the path to the authentication configuration file.
  247. *
  248. * @return string
  249. */
  250. protected function getAuthConfigPath()
  251. {
  252. return $this->getConfigPath('auth');
  253. }
  254. /**
  255. * Get the path to the services configuration file.
  256. *
  257. * @return string
  258. */
  259. protected function getServicesConfigPath()
  260. {
  261. return $this->getConfigPath('services');
  262. }
  263. /**
  264. * Get the path to the PHPSpec configuration file.
  265. *
  266. * @return string
  267. */
  268. protected function getPhpSpecConfigPath()
  269. {
  270. return $this->laravel->basePath().'/phpspec.yml';
  271. }
  272. /**
  273. * Get the console command arguments.
  274. *
  275. * @return array
  276. */
  277. protected function getArguments()
  278. {
  279. return [
  280. ['name', InputArgument::REQUIRED, 'The desired namespace.'],
  281. ];
  282. }
  283. }