Ejemplo n.º 1
0
 public function execute(IOStream $io)
 {
     $fileType = $this->input('fileType');
     $name = $this->input('name');
     if (!$fileType || !$name) {
         $io->showErr("Please specify the 'fileType' and 'name' for the given file. See below help for details");
         $this->showHelp($io);
         return;
     }
     $fileSystem = $this->application()->getFileSystem();
     $real = $this->getRealFolder($this->application()->appPath(), $fileType, $io);
     $appFolder = $real['folder'];
     $namespace = $real['namespace'];
     $name = $this->formatName($name, $fileType);
     $replace = ['{{$namespace}}' => $namespace, '{{$fileName}}' => $name, '{{$className}}' => $name];
     $stub = $fileSystem->getContents(__DIR__ . "/../Stubs/{$fileType}.stub");
     $content = str_replace(array_keys($replace), array_values($replace), $stub);
     $filePath = rtrim($appFolder, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name . '.php';
     if ($fileType === 'migration') {
         $migration = new Migration(['migration' => $name, 'batch' => 0]);
         $migration->save();
     }
     if ($fileSystem->write($filePath, $content)) {
         $io->writeln($name . ' file created successfully', 'green');
     } else {
         throw new \RuntimeException('Unable to create file ' . $filePath);
     }
 }
Ejemplo n.º 2
0
 private function migrateRun($namespacedClass, $class)
 {
     $save = false;
     /** @var AbstractMigration $migrationClass */
     $migrationClass = new $namespacedClass($this->application()->getMapper());
     $migrationClass->{$this->direction}();
     if (strContains($this->namespace, $namespacedClass)) {
         $migrationName = $class;
     } else {
         $migrationName = $namespacedClass;
     }
     $migrationModel = Migration::findOne(['migration' => $migrationName]);
     if (!$migrationModel) {
         $migrationModel = new Migration(['migration' => $migrationName, 'batch' => 0]);
         $save = true;
     }
     if ($this->direction === 'up') {
         $migrationModel->batch++;
     } elseif ($this->direction === 'down' && $migrationModel->count !== 0) {
         $migrationModel->batch--;
     }
     if ($save) {
         return $migrationModel->save();
     } else {
         return $migrationModel->update();
     }
 }