/src/Illuminate/Foundation/Console/AppNameCommand.php

https://gitlab.com/pomirleanu.florentin/SMS-Client · PHP · 306 lines · 141 code · 44 blank · 121 comment · 1 complexity · 058bd0dfcbbe672521c42439fd756b39 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. use AppNamespaceDetectorTrait;
  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->getAppNamespace(), '\\');
  61. $this->setBootstrapNamespaces();
  62. $this->setAppDirectoryNamespace();
  63. $this->setConfigNamespaces();
  64. $this->setComposerNamespace();
  65. $this->setPhpSpecNamespace();
  66. $this->info('Application namespace set!');
  67. $this->composer->dumpAutoloads();
  68. $this->call('clear-compiled');
  69. }
  70. /**
  71. * Set the namespace on the files in the app directory.
  72. *
  73. * @return void
  74. */
  75. protected function setAppDirectoryNamespace()
  76. {
  77. $files = Finder::create()
  78. ->in($this->laravel['path'])
  79. ->name('*.php');
  80. foreach ($files as $file)
  81. {
  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. 'namespace '.$this->currentRoot.'\\',
  95. $this->currentRoot.'\\',
  96. ];
  97. $replace = [
  98. 'namespace '.$this->argument('name').';',
  99. 'namespace '.$this->argument('name').'\\',
  100. $this->argument('name').'\\',
  101. ];
  102. $this->replaceIn($path, $search, $replace);
  103. }
  104. /**
  105. * Set the bootstrap namespaces.
  106. *
  107. * @return void
  108. */
  109. protected function setBootstrapNamespaces()
  110. {
  111. $search = [
  112. $this->currentRoot.'\\Http',
  113. $this->currentRoot.'\\Console',
  114. $this->currentRoot.'\\Exceptions',
  115. ];
  116. $replace = [
  117. $this->argument('name').'\\Http',
  118. $this->argument('name').'\\Console',
  119. $this->argument('name').'\\Exceptions',
  120. ];
  121. $this->replaceIn($this->getBootstrapPath(), $search, $replace);
  122. }
  123. /**
  124. * Set the PSR-4 namespace in the Composer file.
  125. *
  126. * @return void
  127. */
  128. protected function setComposerNamespace()
  129. {
  130. $this->replaceIn(
  131. $this->getComposerPath(), $this->currentRoot.'\\\\', str_replace('\\', '\\\\', $this->argument('name')).'\\\\'
  132. );
  133. }
  134. /**
  135. * Set the namespace in the appropriate configuration files.
  136. *
  137. * @return void
  138. */
  139. protected function setConfigNamespaces()
  140. {
  141. $this->setAppConfigNamespaces();
  142. $this->setAuthConfigNamespace();
  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 PHPSpec configuration namespace.
  174. *
  175. * @return void
  176. */
  177. protected function setPhpSpecNamespace()
  178. {
  179. if ($this->files->exists($path = $this->getPhpSpecConfigPath()))
  180. {
  181. $this->replaceIn($path, $this->currentRoot, $this->argument('name'));
  182. }
  183. }
  184. /**
  185. * Replace the given string in the given file.
  186. *
  187. * @param string $path
  188. * @param string|array $search
  189. * @param string|array $replace
  190. * @return void
  191. */
  192. protected function replaceIn($path, $search, $replace)
  193. {
  194. $this->files->put($path, str_replace($search, $replace, $this->files->get($path)));
  195. }
  196. /**
  197. * Get the path to the Core User class.
  198. *
  199. * @return string
  200. */
  201. protected function getUserClassPath()
  202. {
  203. return $this->laravel['path'].'/Core/User.php';
  204. }
  205. /**
  206. * Get the path to the bootstrap/app.php file.
  207. *
  208. * @return string
  209. */
  210. protected function getBootstrapPath()
  211. {
  212. return $this->laravel['path.base'].'/bootstrap/app.php';
  213. }
  214. /**
  215. * Get the path to the Composer.json file.
  216. *
  217. * @return string
  218. */
  219. protected function getComposerPath()
  220. {
  221. return $this->laravel['path.base'].'/composer.json';
  222. }
  223. /**
  224. * Get the path to the given configuration file.
  225. *
  226. * @param string $name
  227. * @return string
  228. */
  229. protected function getConfigPath($name)
  230. {
  231. return $this->laravel['path.config'].'/'.$name.'.php';
  232. }
  233. /**
  234. * Get the path to the authentication configuration file.
  235. *
  236. * @return string
  237. */
  238. protected function getAuthConfigPath()
  239. {
  240. return $this->getConfigPath('auth');
  241. }
  242. /**
  243. * Get the path to the PHPSpec configuration file.
  244. *
  245. * @return string
  246. */
  247. protected function getPhpSpecConfigPath()
  248. {
  249. return $this->laravel['path.base'].'/phpspec.yml';
  250. }
  251. /**
  252. * Get the console command arguments.
  253. *
  254. * @return array
  255. */
  256. protected function getArguments()
  257. {
  258. return array(
  259. array('name', InputArgument::REQUIRED, 'The desired namespace.'),
  260. );
  261. }
  262. }