private function generate($version) { $generator = new ClassGenerator(); $docblock = DocBlockGenerator::fromArray(array('shortDescription' => 'PDO Simple Migration Class', 'longDescription' => 'Add your queries below')); $generator->setName('PDOSimpleMigration\\Migrations\\Migration' . $version)->setExtendedClass('AbstractMigration')->addUse('PDOSimpleMigration\\Library\\AbstractMigration')->setDocblock($docblock)->addProperties(array(array('description', 'Migration description', PropertyGenerator::FLAG_STATIC)))->addMethods(array(MethodGenerator::fromArray(array('name' => 'up', 'parameters' => array(), 'body' => '//$this->addSql(/*Sql instruction*/);', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Migrate up', 'longDescription' => null)))), MethodGenerator::fromArray(array('name' => 'down', 'parameters' => array(), 'body' => '//$this->addSql(/*Sql instruction*/);', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Migrate down', 'longDescription' => null)))))); $file = FileGenerator::fromArray(array('classes' => array($generator))); return $file->generate(); }
protected function createRealCommandQuery($commandPath, $name, $moduleName) { $this->directoryService->makeSureDirExist($commandPath); $this->directoryService->createAllNamespacedDir($name, $commandPath); $nameParts = explode('/', trim($name, '/')); $className = sprintf('%s%s', $nameParts[count($nameParts) - 1], $this->getSuffixClass()); $handlerName = sprintf('%s%s', $className, 'Handler'); $handlerFactoryName = sprintf('%s%s', $handlerName, 'Factory'); unset($nameParts[count($nameParts) - 1]); //Set namespace for generated files $namespace = sprintf('%s\\%s\\%s', $moduleName, $this->getDirectory(), implode('\\', $nameParts)); $classGenerator = new ClassGenerator(); $classGenerator->setNamespaceName(trim($namespace, '\\')); $handlerGenerator = new ClassGenerator(); $handlerGenerator->setNamespaceName(trim($namespace, '\\')); $handlerFactoryGenerator = new ClassGenerator(); $handlerFactoryGenerator->setNamespaceName(trim($namespace, '\\')); //Set basic properties for command $commandQueryInterfaceToImplement = $this->getCommandQueryInterfaceToImplement(); $classGenerator->setName($className); // $classGenerator->addUse($commandQueryInterfaceToImplement); $tmpRef = new \ReflectionClass($commandQueryInterfaceToImplement); $classGenerator->setImplementedInterfaces([$tmpRef->getName()]); $this->addMethodsFromInterface($commandQueryInterfaceToImplement, $classGenerator); //Set basic properties for command handler $commandHandlerClassToImplement = $this->getAbstractHandlerClassName(); $tmpRef = new \ReflectionClass($commandHandlerClassToImplement); $handlerGenerator->setName($handlerName); $handlerGenerator->setExtendedClass($tmpRef->getName()); $this->addMethodsFromAbstractClass($commandHandlerClassToImplement, $handlerGenerator); //Set basic properties for command handler factory $commandHandlerFactoryClassToImplement = FactoryInterface::class; $handlerFactoryGenerator->setName($handlerFactoryName); $handlerFactoryGenerator->addUse($commandHandlerFactoryClassToImplement); $handlerFactoryGenerator->setImplementedInterfaces([FactoryInterface::class]); $this->addMethodsFromInterface($commandHandlerFactoryClassToImplement, $handlerFactoryGenerator); // $method = $handlerFactoryGenerator->getMethod('__invoke'); //// $method->setParameters() // $method->setBody(sprintf('return new %s();', $handlerGenerator->getName())); //GENERATE IT !!! $fileGenerator = FileGenerator::fromArray(['classes' => [$classGenerator]]); file_put_contents(sprintf('%s%s%s%s%s%s', $commandPath, DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $nameParts), DIRECTORY_SEPARATOR, $className, '.php'), $fileGenerator->generate()); $fileGenerator = FileGenerator::fromArray(['classes' => [$handlerGenerator]]); file_put_contents(sprintf('%s%s%s%s%s%s', $commandPath, DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $nameParts), DIRECTORY_SEPARATOR, $handlerName, '.php'), $fileGenerator->generate()); $fileGenerator = FileGenerator::fromArray(['classes' => [$handlerFactoryGenerator]]); file_put_contents(sprintf('%s%s%s%s%s%s', $commandPath, DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $nameParts), DIRECTORY_SEPARATOR, $handlerFactoryName, '.php'), $fileGenerator->generate()); return [sprintf('%s\\%s', $classGenerator->getNamespaceName(), $classGenerator->getName()), sprintf('%s\\%s', $handlerGenerator->getNamespaceName(), $handlerGenerator->getName()), sprintf('%s\\%s', $handlerFactoryGenerator->getNamespaceName(), $handlerFactoryGenerator->getName())]; }
public function testCreateFromArrayWithClassFromArray() { $fileGenerator = FileGenerator::fromArray(array('filename' => 'foo.php', 'class' => array('name' => 'bar'))); $class = $fileGenerator->getClass('bar'); $this->assertInstanceOf('Zend\\Code\\Generator\\ClassGenerator', $class); }
/** * Generate the code for the given className and primaryKey and write it in a file. * @param string $className The class name. * @param string $primaryKey The primary key. */ private function generateCode($className, $primaryKey) { $tags = $this->config->getTags(); $class = new ClassGenerator(); $docblock = DocBlockGenerator::fromArray(array('shortDescription' => ucfirst($className) . ' model class', 'longDescription' => 'This is a model class generated with DavidePastore\\ParisModelGenerator.', 'tags' => $tags)); $idColumn = new PropertyGenerator('_id_column'); $idColumn->setStatic(true)->setDefaultValue($primaryKey); $table = new PropertyGenerator('_table'); $table->setStatic(true)->setDefaultValue($className); $tableUseShortName = new PropertyGenerator('_table_use_short_name'); $tableUseShortName->setStatic(true)->setDefaultValue(true); $namespace = $this->config->getNamespace(); $extendedClass = '\\Model'; if (isset($namespace) && !empty($namespace)) { $class->setNamespaceName($this->config->getNamespace()); } $class->setName(ucfirst($className))->setDocblock($docblock)->setExtendedClass($extendedClass)->addProperties(array($idColumn, $table, $tableUseShortName)); $file = FileGenerator::fromArray(array('classes' => array($class), 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => ucfirst($className) . ' class file', 'longDescription' => null, 'tags' => $tags)))); $generatedCode = $file->generate(); $directory = $this->config->getDestinationFolder() . $namespace; if (!file_exists($directory)) { mkdir($directory, 0777, true); } $filePath = $directory . "/" . $class->getName() . ".php"; if (file_exists($filePath) && !$this->force) { $helper = $this->getHelper('question'); $realPath = realpath($filePath); $this->output->writeln("\n"); $question = new ConfirmationQuestion('Do you want to overwrite the file "' . $realPath . '"?', false); if ($helper->ask($this->input, $this->output, $question)) { $this->writeInFile($filePath, $generatedCode); } } else { $this->writeInFile($filePath, $generatedCode); } }
public function testFileLineEndingsAreAlwaysLineFeed() { $codeGenFile = FileGenerator::fromArray(array('requiredFiles' => array('SampleClass.php'), 'class' => array('abstract' => true, 'name' => 'SampleClass', 'extendedClass' => 'ExtendedClassName', 'implementedInterfaces' => array('Iterator', 'Traversable')))); // explode by newline, this would leave CF in place if it were generated $lines = explode("\n", $codeGenFile->generate()); $targetLength = strlen('require_once \'SampleClass.php\';'); $this->assertEquals($targetLength, strlen($lines[2])); $this->assertEquals(';', $lines[2][$targetLength - 1]); }
/** * Write a module configuration. * * @param string $configPath Path to write to * @param string $config Configuration array to write * * @return void * @throws \Exception */ protected function writeModuleConfig($configPath, $config) { $generator = FileGenerator::fromArray(['body' => 'return ' . var_export($config, true) . ';']); if (!file_put_contents($configPath, $generator->generate())) { throw new \Exception("Cannot write to {$configPath}"); } Console::writeLine("Successfully updated {$configPath}"); }
/** * Metodo para gerar o novo form. * * Cria uma nova classe com o novo conteudo criado. * Apos criar a nova classe, cria um arquivo ou atualiza o arquivo existente, caso exista. */ private function generateNewForm() { $file = FileGenerator::fromArray(array('classes' => array($this->newForm))); $code = $file->generate(); file_put_contents($this->getStrDirPathForm() . '/' . $this->getStrFormName() . '.php', $code); }