Ejemplo n.º 1
0
 /**
  * @param $entity
  * @param \Blast\Orm\Entity\DefinitionInterface|null $definition
  * @return Definition
  */
 private function transformEntityToDefinition($entity, $definition = null)
 {
     // find definition class in entity by property or method
     $definition = $this->loadDefinitionFromEntity($entity, $definition);
     $reflection = Support::getCachedReflectionClass($entity, $this->getReflectionCache());
     $configuration = $definition->getConfiguration();
     // mapper is a dependency for all other entity services
     // fetch mapper first
     if ($reflection->hasMethod('mapper')) {
         $mapperMethod = $reflection->getMethod('mapper');
         if ($mapperMethod->isStatic() && $mapperMethod->isPublic()) {
             $configuration['mapper'] = $mapperMethod->invokeArgs($entity, [$entity]);
         }
     }
     //update configuration with mapper
     $configuration = $definition->setConfiguration($configuration)->getConfiguration();
     foreach ($configuration as $key => $value) {
         //ignore entity or mapper
         if (in_array($key, ['entity', 'mapper'])) {
             continue;
         }
         if ($reflection->hasMethod($key)) {
             $method = $reflection->getMethod($key);
             if ($method->isPublic() && $method->isStatic()) {
                 $value = $method->invokeArgs($entity, [$entity, $definition->getMapper()]);
             }
         } else {
             $value = is_callable($value) ? call_user_func_array($value, [$entity, $definition->getMapper()]) : $value;
         }
         $configuration[$key] = $value;
     }
     $configuration['entityClassName'] = $reflection->getName();
     $configuration['entityShortName'] = $reflection->getShortName();
     // fetch table name from table property
     if ($reflection->hasProperty('table')) {
         $reflectionProperty = $reflection->getProperty('table');
         if ($reflectionProperty->isStatic()) {
             $configuration['tableName'] = $reflectionProperty->getValue($entity);
         }
     }
     // fetch table name from table method
     if ($reflection->hasMethod('table')) {
         $reflectionMethod = $reflection->getMethod('table');
         if ($reflectionMethod->isStatic()) {
             $configuration['tableName'] = $reflectionMethod->invoke($entity);
         }
     }
     $definition->setConfiguration($configuration);
     // set table name from reflection short name
     // if table name is unknown or FQCN and if
     // entity is no ArrayObject
     if ((empty($definition->getTableName()) || class_exists($definition->getTableName())) && !$definition->getEntity() instanceof \ArrayObject) {
         $configuration['tableName'] = Inflector::pluralize(Inflector::tableize($configuration['entityShortName']));
     }
     return $definition->setConfiguration($configuration);
 }
Ejemplo n.º 2
0
 /**
  * Todo rewrite this horrobile piece of code...
  * @param $tableName
  * @return bool|string
  */
 private function determineCacheId($tableName)
 {
     /** @var string|bool $compTableName */
     if ($tableName instanceof DefinitionInterface) {
         return $tableName->getTableName();
     }
     if (null === $tableName) {
         return false;
     }
     if (is_string($tableName)) {
         return class_exists($tableName) && false === Support::isPHPInternalClass($tableName) ? Inflector::pluralize(Inflector::tableize(Support::getCachedReflectionClass($tableName, $this->getReflectionCache())->getShortName())) : $tableName;
     }
     if (is_array($tableName)) {
         return false;
     }
     if (Support::isPHPInternalClass($tableName)) {
         return false;
     }
     if ($tableName instanceof EntityAwareInterface) {
         $compTableName = Inflector::pluralize(Inflector::tableize(Support::getCachedReflectionClass(Support::getEntityName($tableName->getEntity()), $this->getReflectionCache())->getShortName()));
         if (is_string($compTableName)) {
             return $compTableName;
         }
     }
     if (is_object($tableName)) {
         $compTableName = Inflector::pluralize(Inflector::tableize(Support::getCachedReflectionClass(Support::getEntityName($tableName), $this->getReflectionCache())->getShortName()));
         if (is_string($compTableName)) {
             return $compTableName;
         }
     }
     return false;
 }