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

https://gitlab.com/techniconline/kmc · PHP · 327 lines · 149 code · 47 blank · 131 comment · 1 complexity · 5beb4811e1ed50a5c93e1ea70a181c10 MD5 · raw file

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