private function shouldAddUseStatement(File $file, FullyQualifiedName $fullyQualifiedName) : bool
 {
     $type = $fullyQualifiedName->getFullyQualifiedName();
     if (in_array($type, self::NON_OBJECT_TYPES, true)) {
         return false;
     }
     return true;
 }
 /**
  * @param string $stringType
  *
  * @return string
  */
 public function filterNamespace($stringType)
 {
     $type = new ModelType($stringType);
     if (!$type->isObject()) {
         return $stringType;
     }
     $fullyQualifiedName = new FullyQualifiedName($stringType);
     return $fullyQualifiedName->getName();
 }
 function it_does_not_insert_the_same_use_statement_twice(Editor $editor, File $file, FullyQualifiedName $fullyQualifiedName, InsertUseStatementHandler $insertUseStatementHandler)
 {
     $fullyQualifiedNames = [$fullyQualifiedName->getWrappedObject()];
     $insertUseStatements = new InsertUseStatements($file->getWrappedObject(), $fullyQualifiedNames);
     $fullyQualifiedName->getFullyQualifiedName()->willReturn('Vendor\\Project\\MyDependency');
     $editor->hasBelow($file, '/^use Vendor\\\\Project\\\\MyDependency;$/', 0)->willReturn(true);
     $insertUseStatement = Argument::Type(InsertUseStatement::class);
     $insertUseStatementHandler->handle($insertUseStatement)->shouldNotBeCalled();
     $this->handle($insertUseStatements);
 }
 function it_inserts_use_statement_at_the_end_of_use_statement_block(Editor $editor, File $file, FullyQualifiedName $fullyQualifiedName)
 {
     $insertUseStatement = new InsertUseStatement($file->getWrappedObject(), $fullyQualifiedName->getWrappedObject());
     $fullyQualifiedName->getNamespace()->willReturn(self::NAME_SPACE);
     $fullyQualifiedName->getFullyQualifiedName()->willReturn(self::FULLY_QUALIFIED_NAME);
     $editor->hasBelow($file, self::NAME_SPACE_PATTERN, 0)->willReturn(false);
     $editor->hasBelow($file, self::USE_STATEMENT_PATTERN, 0)->willReturn(false);
     $editor->jumpBelow($file, InsertUseStatementHandler::CLASS_ENDING, 0)->shouldBeCalled();
     $editor->hasAbove($file, InsertUseStatementHandler::USE_STATEMENT)->willReturn(true);
     $editor->jumpAbove($file, InsertUseStatementHandler::USE_STATEMENT)->shouldBeCalled();
     $editor->insertBelow($file, self::USE_STATEMENT)->shouldBeCalled();
     $this->handle($insertUseStatement);
 }
 /**
  * @param string $current
  * @param array  $collection
  *
  * @return string
  */
 public function align($current, $collection)
 {
     $elementLength = strlen($current);
     $longestElement = $elementLength;
     foreach ($collection as $element) {
         if ('Memio\\Model\\Phpdoc\\ParameterTag' === get_class($element)) {
             $type = $element->getType();
             $modelType = new ModelType($element->getType());
             if ($modelType->isObject()) {
                 $fullyQualifiedName = new FullyQualifiedName($type);
                 $type = $fullyQualifiedName->getName();
             }
             $longestElement = max($longestElement, strlen($type));
         }
     }
     return $current . str_repeat(' ', $longestElement - $elementLength);
 }
 /**
  * {@inheritDoc}
  */
 public function generateCode($model, array $parameters = array())
 {
     $fqcn = get_class($model);
     $name = FullyQualifiedName::make($fqcn)->getName();
     $modelName = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $name));
     $parameters[$modelName] = $model;
     return $this->templateEngine->render('phpdoc/' . $modelName, $parameters);
 }
 /**
  * {@inheritDoc}
  */
 public function validate($model)
 {
     $firstElement = current($model);
     $fqcn = get_class($firstElement);
     $modelType = FullyQualifiedName::make($fqcn)->getName();
     $nameCount = array();
     foreach ($model as $element) {
         $name = $element->getName();
         $nameCount[$name] = isset($nameCount[$name]) ? $nameCount[$name] + 1 : 1;
     }
     $messages = array();
     foreach ($nameCount as $name => $count) {
         if ($count > 1) {
             $messages[] = sprintf('Collection "%s" cannot have name "%s" duplicates (%s occurences)', $modelType, $name, $count);
         }
     }
     return empty($messages) ? new NoneViolation() : new SomeViolation(implode("\n", $messages));
 }
    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $model = $input->getArgument('model');
        $outputFile = sprintf('%s.php', $this->joinPaths($input->getOption('output'), str_replace('\\', DIRECTORY_SEPARATOR, $model)));
        $classObject = Object::make($model);
        $file = File::make($outputFile)->setStructure($classObject);
        $file->addFullyQualifiedName(FullyQualifiedName::make('Doctrine\\ODM\\MongoDB\\Mapping\\Annotations as ODM'));
        $file->addFullyQualifiedName(FullyQualifiedName::make('Hive\\Annotations as H'));
        $fields = array_merge(['id:id'], $input->getArgument('fields'));
        foreach ($fields as $field) {
            if (!preg_match('/(\\w+)(?::(string|boolean|id|reference[s]?)(?:\\[([\\w\\\\]+),(\\w+)\\])?)?(?::(\\w+))?/i', $field, $matches)) {
                continue;
            }
            @(list($all, $name, $type, $referenceType, $linkProperty, $fake) = $matches);
            /** @var Property $property */
            $property = Property::make($name);
            $property->makePublic();
            $property->setPhpdoc(new ODMPropertyPhpdoc($name, $type, $referenceType, $linkProperty, $fake));
            $classObject->addProperty($property);
        }
        $classObject->setPhpdoc(StructurePhpdoc::make()->setDescription(Description::make(<<<'EOF'
This class is generated with the hive console.

@ODM\Document @ODM\HasLifecycleCallbacks
EOF
)));
        $classObject->addMethod(Method::make('validate')->setPhpdoc(MethodPhpdoc::make()->setDescription(Description::make(<<<'EOF'
The validate method can be used to validate properties.

@ODM\PrePersist @ODM\PreUpdate
EOF
))));
        // Generate the code
        $prettyPrinter = Build::prettyPrinter();
        $prettyPrinter->addTemplatePath(realpath($this->joinPaths(dirname(__FILE__), '../../../templates/memio/')));
        $generatedCode = $prettyPrinter->generateCode($file);
        @mkdir(dirname($outputFile), 0755, true);
        file_put_contents($outputFile, html_entity_decode(html_entity_decode($generatedCode)));
        $io = new SymfonyStyle($input, $output);
        $io->success(["Model {$model} created in {$outputFile}"]);
    }
Beispiel #9
0
 /**
  * {@inheritDoc}
  */
 public function getNamespace()
 {
     return $this->fullyQualifiedName->getNamespace();
 }
 public function testWithAlias()
 {
     $fullyQualifiedName = FullyQualifiedName::make('\\ArrayObject')->setAlias('StdArray');
     $generatedCode = $this->prettyPrinter->generateCode($fullyQualifiedName);
     $this->assertSame('use ArrayObject as StdArray;', $generatedCode);
 }
Beispiel #11
0
 /**
  * @param Object $object
  * @param $filePath
  * @param array $fqns
  * @return $this
  */
 protected function dumpObject(Object $object, $filePath, array $fqns = [])
 {
     $fileDir = dirname($filePath);
     $file = File::make($filePath);
     if ($object->hasParent()) {
         $fqns[] = $object->getParent()->getFullyQualifiedName();
     }
     foreach ($object->allContracts() as $contract) {
         $fqns[] = $contract->getFullyQualifiedName();
     }
     sort($fqns);
     foreach ($fqns as $name) {
         if (is_array($name)) {
             $keys = array_keys($name);
             $fqn = new FullyQualifiedName($name[$keys[0]]);
             $fqn->setAlias($keys[0]);
             $file->addFullyQualifiedName($fqn);
         } else {
             $file->addFullyQualifiedName(new FullyQualifiedName($name));
         }
     }
     $file->setStructure($object);
     $this->filesystem->mkdir($fileDir, 0755);
     $this->filesystem->dumpFile($filePath, Build::prettyPrinter()->generateCode($file), 0755);
     return $this;
 }