Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Laracasts\Generators\Commands;
  4.  
  5. use Illuminate\Console\Command;
  6. use Illuminate\Container\Container;
  7. use Illuminate\Filesystem\Filesystem;
  8. use Laracasts\Generators\Migrations\NameParser;
  9. use Laracasts\Generators\Migrations\SchemaParser;
  10. use Laracasts\Generators\Migrations\SyntaxBuilder;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Input\InputArgument;
  13.  
  14. class MigrationMakeCommand extends Command
  15. {
  16. /**
  17. * The console command name.
  18. *
  19. * @var string
  20. */
  21. protected $name = 'make:migration:schema';
  22.  
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = 'Create a new migration class and apply schema at the same time';
  29.  
  30. /**
  31. * The filesystem instance.
  32. *
  33. * @var Filesystem
  34. */
  35. protected $files;
  36.  
  37. /**
  38. * Meta information for the requested migration.
  39. *
  40. * @var array
  41. */
  42. protected $meta;
  43.  
  44. /**
  45. * @var Composer
  46. */
  47. private $composer;
  48.  
  49. /**
  50. * Create a new command instance.
  51. *
  52. * @param Filesystem $files
  53. * @param Composer $composer
  54. */
  55. public function __construct(Filesystem $files)
  56. {
  57. parent::__construct();
  58.  
  59. $this->files = $files;
  60. $this->composer = app()['composer'];
  61. }
  62.  
  63. /**
  64. * Execute the console command.
  65. *
  66. * @return mixed
  67. */
  68. public function fire()
  69. {
  70. $this->meta = (new NameParser)->parse($this->argument('name'));
  71.  
  72. $this->makeMigration();
  73. $this->makeModel();
  74. }
  75.  
  76. /**
  77. * Get the application namespace.
  78. *
  79. * @return string
  80. */
  81. protected function getAppNamespace()
  82. {
  83. return Container::getInstance()->getNamespace();
  84. }
  85.  
  86. /**
  87. * Generate the desired migration.
  88. */
  89. protected function makeMigration()
  90. {
  91. $name = $this->argument('name');
  92.  
  93. if ($this->files->exists($path = $this->getPath($name))) {
  94. return $this->error($this->type . ' already exists!');
  95. }
  96.  
  97. $this->makeDirectory($path);
  98.  
  99. $this->files->put($path, $this->compileMigrationStub());
  100.  
  101. $this->info('Migration created successfully.');
  102.  
  103. $this->composer->dumpAutoloads();
  104. }
  105.  
  106. /**
  107. * Generate an Eloquent model, if the user wishes.
  108. */
  109. protected function makeModel()
  110. {
  111. $modelPath = $this->getModelPath($this->getModelName());
  112.  
  113. if ($this->option('model') && !$this->files->exists($modelPath)) {
  114. $this->call('make:model', [
  115. 'name' => $this->getModelName()
  116. ]);
  117. }
  118. }
  119.  
  120. /**
  121. * Build the directory for the class if necessary.
  122. *
  123. * @param string $path
  124. * @return string
  125. */
  126. protected function makeDirectory($path)
  127. {
  128. if (!$this->files->isDirectory(dirname($path))) {
  129. $this->files->makeDirectory(dirname($path), 0777, true, true);
  130. }
  131. }
  132.  
  133. /**
  134. * Get the path to where we should store the migration.
  135. *
  136. * @param string $name
  137. * @return string
  138. */
  139. protected function getPath($name)
  140. {
  141. return base_path() . '/database/migrations/' . date('Y_m_d_His') . '_' . $name . '.php';
  142. }
  143.  
  144. /**
  145. * Get the destination class path.
  146. *
  147. * @param string $name
  148. * @return string
  149. */
  150. protected function getModelPath($name)
  151. {
  152. $name = str_replace($this->getAppNamespace(), '', $name);
  153.  
  154. return $this->laravel['path'] . '/' . str_replace('\\', '/', $name) . '.php';
  155. }
  156.  
  157. /**
  158. * Compile the migration stub.
  159. *
  160. * @return string
  161. */
  162. protected function compileMigrationStub()
  163. {
  164. $stub = $this->files->get(__DIR__ . '/../stubs/migration.stub');
  165.  
  166. $this->replaceClassName($stub)
  167. ->replaceSchema($stub)
  168. ->replaceTableName($stub);
  169.  
  170. return $stub;
  171. }
  172.  
  173. /**
  174. * Replace the class name in the stub.
  175. *
  176. * @param string $stub
  177. * @return $this
  178. */
  179. protected function replaceClassName(&$stub)
  180. {
  181. $className = ucwords(camel_case($this->argument('name')));
  182.  
  183. $stub = str_replace('{{class}}', $className, $stub);
  184.  
  185. return $this;
  186. }
  187.  
  188. /**
  189. * Replace the table name in the stub.
  190. *
  191. * @param string $stub
  192. * @return $this
  193. */
  194. protected function replaceTableName(&$stub)
  195. {
  196. $table = $this->meta['table'];
  197.  
  198. $stub = str_replace('{{table}}', $table, $stub);
  199.  
  200. return $this;
  201. }
  202.  
  203. /**
  204. * Replace the schema for the stub.
  205. *
  206. * @param string $stub
  207. * @return $this
  208. */
  209. protected function replaceSchema(&$stub)
  210. {
  211. if ($schema = $this->option('schema')) {
  212. $schema = (new SchemaParser)->parse($schema);
  213. }
  214.  
  215. $schema = (new SyntaxBuilder)->create($schema, $this->meta);
  216.  
  217. $stub = str_replace(['{{schema_up}}', '{{schema_down}}'], $schema, $stub);
  218.  
  219. return $this;
  220. }
  221.  
  222. /**
  223. * Get the class name for the Eloquent model generator.
  224. *
  225. * @return string
  226. */
  227. protected function getModelName()
  228. {
  229. return ucwords(str_singular(camel_case($this->meta['table'])));
  230. }
  231.  
  232. /**
  233. * Get the console command arguments.
  234. *
  235. * @return array
  236. */
  237. protected function getArguments()
  238. {
  239. return [
  240. ['name', InputArgument::REQUIRED, 'The name of the migration'],
  241. ];
  242. }
  243.  
  244. /**
  245. * Get the console command options.
  246. *
  247. * @return array
  248. */
  249. protected function getOptions()
  250. {
  251. return [
  252. ['schema', 's', InputOption::VALUE_OPTIONAL, 'Optional schema to be attached to the migration', null],
  253. ['model', null, InputOption::VALUE_OPTIONAL, 'Want a model for this table?', true],
  254. ];
  255. }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement