files = $files; $this->composer = $composer; } /** * Execute the console command. * * @return void */ public function handle() { $name = Str::snake(trim($this->input->getArgument('name'))); $this->writeSeeder($name); $this->composer->dumpAutoloads(); } /** * Write the seeder file to disk. * * @param string $name * @return string */ protected function writeSeeder($name) { $file = $this->create( $name, $this->getSeederPath() ); $file = pathinfo($file, PATHINFO_FILENAME); $this->line("Created Seeder: {$file}"); } /** * Get seeder path. * * @return string */ protected function getSeederPath() { return $this->laravel->databasePath().DIRECTORY_SEPARATOR.'seeders'; } /** * Create a new seeder at the given path. * * @param string $name * @param string $path * @return string * * @throws \Exception */ public function create($name, $path) { $dateTime = new DateTime(); $className = $this->getClassName($dateTime, $name); $this->ensureSeederDoesntAlreadyExist($className, $path); $stub = $this->getStub(); $path = $this->getPath($dateTime, $name, $path); $this->files->ensureDirectoryExists(dirname($path)); $this->files->put( $path, $this->populateStub($className, $stub) ); return $path; } /** * Ensure that a seeder with the given name doesn't already exist. * * @param string $name * @param string $seederPath * @return void * * @throws \InvalidArgumentException */ protected function ensureSeederDoesntAlreadyExist($className, $seederPath = null) { if (! empty($seederPath)) { $seederFiles = $this->files->glob($seederPath.'/*.php'); foreach ($seederFiles as $seederFile) { $this->files->requireOnce($seederFile); } } if (class_exists($className)) { throw new InvalidArgumentException("A {$className} class already exists."); } } /** * Get the seeder stub file. * * @return string */ protected function getStub() { $stub = $this->stubPath().'/seeder.stub'; return $this->files->get($stub); } /** * Populate the place-holders in the seeder stub. * * @param string $className * @param string $stub * @return string */ protected function populateStub($className, $stub) { $stub = str_replace( ['DummyClass', '{{ class }}', '{{class}}'], $className, $stub ); return $stub; } /** * Get the class name of a seeder name. * * @param string $name * @return string */ protected function getClassName($dateTime, $name) { return Str::studly($name).'_'.$dateTime->format('YmdHis'); } /** * Get the full path to the seeder. * * @param string $name * @param string $path * @return string */ protected function getPath($dateTime, $name, $path) { return $path.'/'.$dateTime->format('Y_m_d_His').'_'.$name.'.php'; } /** * Get the path to the stubs. * * @return string */ public function stubPath() { return $this->laravel->basePath().'/stubs'; } }