; use Kaliop\eZMigrationBundle\Interfaces\VersionInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Auto-generated Migration definition: Please modify to your needs! */ class _place_holder implements VersionInterface, ContainerAwareInterface { /** * The dependency injection container * @var ContainerInterface */ private $container; /** * @inheritdoc */ public function execute() { // @TODO This method is auto generated, please modify to your needs. } /** * @inheritdoc */ public function setContainer(ContainerInterface $container = null) { $this->container = $container; } } '; private $ymlTemplate = ' # Auto-generated Migration definition: Please modify to your needs! - mode: [create/update/delete] type: [content/content_type/user/user_group/role] '; private $availableMigrationTypes = array( 'yml', 'php', 'sql' ); /** * @var OutputInterface */ private $output; /** * Configure the console command */ protected function configure() { $this->setName('kaliop:migration:generate') ->setDescription('Generate a blank migration definition file.') ->addOption('type', null, InputOption::VALUE_OPTIONAL, 'The type of migration file to generate. (yml, php, sql)', 'yml') ->addOption('dbserver', null, InputOption::VALUE_OPTIONAL, 'The type of the database server the sql migration is for. (mysql, postgre)', 'mysql') ->addArgument('bundle', InputOption::VALUE_REQUIRED, 'The bundle to generate the migration definition file in. eg.: AcmeMigrationBundle' ) ->setHelp(<<kaliop:migration:generate command generates a skeleton migration definition file: ./ezpublish/console kaliop:migration:generate bundlename You can optionally specify the file type to generate with --type: ./ezpublish/console kaliop:migration:generate --type=yml bundlename For SQL type migration you can optionally specify the database server type the migration is for with --dbserver: ./ezpublish/console kaliop:migration:generate --type=sql --dbserver=mysql bundlename EOT ); } /** * Run the command and display the results. * * @throws \InvalidArgumentException When an unsupported file type is selected * @param InputInterface $input * @param OutputInterface $output * @return void */ public function execute( InputInterface $input, OutputInterface $output ) { $fileType = $input->getOption('type'); $dbServer = $input->getOption('dbserver'); $this->output = $output; if (!in_array($fileType, $this->availableMigrationTypes)) { throw new \InvalidArgumentException( 'Unsupported migration file type ' . $fileType ); } $configuration = $this->getConfiguration( $input, $output ); $version = date('YmdHis'); $bundleName = $input->getArgument('bundle'); $path = $this->generateMigrationFile($configuration, $version, $bundleName, $fileType, $dbServer); $output->writeln(sprintf("Generated new migration file to %s", $path)); } /** * Generates a migration definition file. * * @throws \InvalidArgumentException When the destination directory does not exists * @param Configuration $configuration * @param string $version The version string in YYYYMMDDHHMMSS format * @param string $bundleName The name of the bundle to generate the migration file for * @param string $fileType The type of migration file to generate * @param string $dbServer The type of database server the SQL migration is for. * @return string The path to the migration file */ protected function generateMigrationFile(Configuration $configuration, $version, $bundleName, $fileType, $dbServer = 'mysql') { /** @var $bundle \Symfony\Component\HttpKernel\Bundle\BundleInterface */ $bundle = $this->getApplication()->getKernel()->getBundle($bundleName); $container = $this->getApplication()->getKernel()->getContainer(); $versionDirectory = $container->getParameter( 'kaliop_bundle_migration.version_directory' ); $bundleVersionDirectory = $bundle->getPath() . '/' . $versionDirectory; if($fileType == 'sql') { $path = $bundleVersionDirectory . '/' . $version . '_' . $dbServer . '_place_holder.' . $fileType; } else { $path = $bundleVersionDirectory . '/' . $version .'_place_holder.' . $fileType; } switch($fileType) { case 'php': $template = $this->phpTemplate; break; case 'sql': $template = "-- Autogenerated migration file. Please customise for your needs."; break; case 'yml': default: $template = $this->ymlTemplate; } $placeholders = array( '', '' ); $replacements = array( $configuration->versionDirectory . "\\" . $bundleName, $version ); $code = str_replace($placeholders, $replacements, $template); if(!file_exists($bundleVersionDirectory)) { $this->output->writeln( sprintf( "Migrations directory %s does not exist. I will create one now....", $bundleVersionDirectory ) ); if (mkdir($bundleVersionDirectory, self::DIR_CREATE_PERMISSIONS, true)) { $this->output->writeln( sprintf( "Migrations directory %s has been created", $bundleVersionDirectory ) ); } else { throw new FileException( sprintf( "Failed to create migrations directory %s.", $bundleVersionDirectory ) ); } } file_put_contents($path, $code); return $path; } }