/** * Do some stuff on file * * @param array $configuration * @param FileContainer $fileContainer * @return void * @throws \UnexpectedValueException */ public function generate(array $configuration, FileContainer $fileContainer) { /** @var ClassFileDefinition[] $entityFiles */ $entityFiles = $fileContainer->getFilesByTag(Tag::ENTITY()); foreach ($entityFiles as $entityFile) { foreach ($entityFile->getProperties() as $property) { $propertyConfiguration = $property->getSourceConfiguration(); if ("relation" !== $propertyConfiguration['type']) { continue; } $cardinality = $this->getCardinality($configuration, $property); $reversePropertyIsMany = $cardinality->hasSourceMany(); if (true === $cardinality->hasTargetMany()) { $reverseProperty = $this->getReversePropertyName($configuration, $property); $this->initializeProperty($entityFile, $property); $entityFile->addMethod($this->createAddMethod($property, $reverseProperty, $reversePropertyIsMany)); $entityFile->addMethod($this->createRemoveMethod($property, $reverseProperty, $reversePropertyIsMany)); $entityFile->addMethod($this->createAccessorMethod($property)); } else { $entityFile->addMethod($this->createSetMethod($property)); $entityFile->addMethod($this->createAccessorMethod($property)); } } } }
/** * Do some stuff on file * * @param array $configuration * @param FileContainer $entityFileContainer * @return void */ public function generate(array $configuration, FileContainer $entityFileContainer) { /** @var ClassFileDefinition[] $entityFiles */ $entityFiles = $entityFileContainer->getFilesByTag(Tag::ENTITY()); foreach ($entityFiles as $entityFile) { $entityFile->addTrait(new FullyQualifiedClassName('CCMBenchmark\\Ting\\Entity\\NotifyProperty')); $entityFile->addInterface(new FullyQualifiedClassName('CCMBenchmark\\Ting\\Entity\\NotifyPropertyInterface')); $prefixes = ['set', 'add', 'remove']; foreach ($entityFile->getProperties() as $property) { if ('relation' === $property->getSourceConfiguration()['type']) { continue; } $names = [$property->getName()->toUpperCamelCase(), $property->getName()->singularize()->toUpperCamelCase()]; foreach ($entityFile->getMethods() as $method) { $name = $method->getName()->toLowerCamelCase(); if (false !== ($prefix = $this->getPrefixComposedBy($name, $prefixes, $names))) { if ($prefix === 'set') { $method->addLineOnBeginning(sprintf('$this->propertyChanged(\'%1$s\', $this->%1$s, $%1$s);', $property->getName()->toLowerCamelCase())); } else { $method->addLineOnBeginning(sprintf('$old = $this->%1$s;', $property->getName()->toLowerCamelCase())); $method->addLine(sprintf('$this->propertyChanged(\'%1$s\', $old, $this->%1$s);', $property->getName()->toLowerCamelCase())); } } } } } }
/** * Do some stuff on file * * @param array $configuration * @param FileContainer $entityFileContainer * @return void */ public function generate(array $configuration, FileContainer $entityFileContainer) { /** @var ClassFileDefinition[] $entityFiles */ $entityFiles = $entityFileContainer->getFilesByTag(Tag::ENTITY()); foreach ($entityFiles as $entityFile) { foreach ($entityFile->getMethods() as $method) { //Param foreach ($method->getParameters() as $parameter) { $type = $parameter->getType(); if ($type === null) { $typeName = 'mixed'; } else { $typeName = $type->getPhpTypeName(); } $method->addAnnotation(sprintf('@param %s $%s', $typeName, $parameter->getName()->toLowerCamelCase())); } //Return $returnValue = $method->getReturnValue(); if ($returnValue !== null) { if ($returnValue === '$this') { $types = ['$this']; } else { $propertyName = str_replace('$this->', '', $returnValue); $property = $entityFile->getProperty(lcfirst($propertyName)); if ($property !== null) { $types = $this->resolvePropertyTypes($property); } else { $types = ['mixed']; } } $method->addAnnotation(sprintf('@return %s', implode('|', $types))); } } } }
/** * Do some stuff on file * * @param array $configuration * @param FileContainer $entityFileContainer * @return void */ public function generate(array $configuration, FileContainer $entityFileContainer) { /** @var ClassFileDefinition[] $entityFiles */ $entityFiles = $entityFileContainer->getFilesByTag(Tag::ENTITY()); foreach ($entityFiles as $entityFile) { foreach ($entityFile->getProperties() as $property) { $property->addAnnotation(sprintf('@var %s', implode('|', $this->resolvePropertyTypes($property)))); } } }
/** * Do some stuff on file. * * @param array $configuration * @param FileContainer $fileContainer * * @return void */ public function generate(array $configuration, FileContainer $fileContainer) { $this->configuration = $configuration; /** @var ClassFileDefinition[] $entityFiles */ $entityFiles = $fileContainer->getFilesByTag(Tag::ENTITY()); foreach ($entityFiles as $entityFile) { $entityFile->addUse("Doctrine\\ORM\\Mapping as ORM"); if ($entityFile->getPrefix() === ClassPrefix::ABSTRACT_PREFIX) { $entityFile->addAnnotation('@ORM\\MappedSuperclass'); } else { $hasEntityAnnotation = false; foreach ($entityFile->getAnnotations() as $key => $annotation) { if (false !== strpos($annotation, '@ORM\\Entity')) { $hasEntityAnnotation = true; } } if (false === $hasEntityAnnotation) { $entityFile->addAnnotation('@ORM\\Entity()'); } $entityFile->addAnnotation(sprintf('@ORM\\Table(name="%s")', $entityFile->getFullyQualifiedClassName()->getClassName()->toLowerSnakeCase())); if ($this->entityHasChild($configuration, $entityFile)) { $entityFile->addAnnotation(sprintf('@ORM\\InheritanceType("%s")', $configuration['doctrine']['InheritanceType'])); $entityFile->addAnnotation('@ORM\\DiscriminatorColumn(name="discriminator", type="string")'); } } foreach ($entityFile->getProperties() as $property) { $definition = $property->getSourceConfiguration(); if (true === $definition['primary']) { $property->addAnnotation('@ORM\\Id'); } if (true === $definition['autoincrement']) { $property->addAnnotation('@ORM\\GeneratedValue'); } if ('relation' === $definition['type']) { $this->generateRelationAnnotation($entityFile, $property); } elseif ('enum' === $definition['type']) { $property->addAnnotation(sprintf('@ORM\\Column(name="%s", type="string")', $property->getName()->toLowerSnakeCase())); } else { $property->addAnnotation($this->generateColumnAnnotation($property)); } } } }
/** * Do some stuff on file * * @param array $configuration * @param FileContainer $fileContainer * @return void */ public function generate(array $configuration, FileContainer $fileContainer) { /** @var ClassFileDefinition[] $entityFiles */ $entityFiles = $fileContainer->getFilesByTag(Tag::ENTITY()); foreach ($entityFiles as $entityFile) { foreach ($entityFile->getProperties() as $property) { $propertyConfiguration = $property->getSourceConfiguration(); if ("Relation" !== $propertyConfiguration['type']) { if ($property->getType() instanceof ArrayType) { $entityFile->addMethod($this->createAddMethod($property)); $entityFile->addMethod($this->createRemoveMethod($property)); $entityFile->addMethod($this->createAccessorMethod($property)); } else { $entityFile->addMethod($this->createSetMethod($property)); $entityFile->addMethod($this->createAccessorMethod($property)); } } } } }
/** * @param array $configuration * @param FileContainer $fileContainer * @throws \Exception */ public function generate(array $configuration, FileContainer $fileContainer) { /** @var ClassFileDefinition[] $entityFiles */ $entityFiles = $fileContainer->getFilesByTag(Tag::ENTITY()); foreach ($entityFiles as $entityFile) { if ($entityFile->getPrefix() === ClassPrefix::ABSTRACT_PREFIX) { continue; } $repositoryFqcn = $this->createRepositoryFqcn($configuration, $entityFile->getFullyQualifiedClassName()->getName()); $annotations = $entityFile->getAnnotations(); foreach ($annotations as $key => $annotation) { if (false !== strpos($annotation, '@ORM\\Entity')) { unset($annotations[$key]); } } $annotations[] = sprintf('@ORM\\Entity(repositoryClass="%s")', $repositoryFqcn->getFullQualifiedClassName()); $entityFile->setAnnotations($annotations); $repositoryFile = new ClassFileDefinition($entityFile->getSourceName(), $entityFile->getSourceConfiguration(), $repositoryFqcn); $repositoryFile->setParent(new FullyQualifiedClassName('Doctrine\\ORM\\EntityRepository')); $fileContainer->addFile($repositoryFile, [Tag::REPOSITORY()]); } }
/** * Do some stuff on file * * @param array $configuration * @param FileContainer $fileContainer * @throws \Exception */ public function generate(array $configuration, FileContainer $fileContainer) { $this->configuration = $configuration; foreach ($configuration['entities'] as $entityName => $config) { $fqcn = $this->createEntityFqcn($this->configuration, $entityName); $entityFile = new ClassFileDefinition($entityName, $config, $fqcn); // Parent if (null !== $config['parent']) { if (false === array_key_exists($config['parent'], $this->configuration['entities'])) { throw new \Exception(sprintf('Entity %s has for parent %s, but this entity is not defined in config file', $entityName, $config['parent'])); } $entityFile->setParent($this->createEntityFqcn($this->configuration, $config['parent'])); } // Abstract if (true === $config['abstract']) { $entityFile->setPrefix(ClassPrefix::ABSTRACT_PREFIX()); } //Properties $construct = $this->getConstructor($entityFile); foreach ($config['properties'] as $propertyName => $propertyConfig) { $property = $this->createProperty($propertyName, $propertyConfig); $property->setVisibility(new Visibility($this->configuration['global']['fieldVisibility'])); $entityFile->addProperty($property); if ($property->getDefaultValue() instanceof ObjectValue) { $construct->addLine(sprintf('$this->%s = %s;', $property->getName()->toLowerCamelCase(), $property->getDefaultValue()->getPhpFormatedValue())); } } if (0 !== count($construct->getLines())) { $entityFile->addMethod($construct); } //Create $fileContainer->addFile($entityFile, [Tag::ENTITY()]); } }