function __construct() { $reader = new \Doctrine\Common\Annotations\AnnotationReader(); $className = get_class($this); $reflect = new ReflectionClass($className); $props = $reflect->getProperties(); foreach ($props as $p) { $anno = $reader->getPropertyAnnotations($p); if (array_key_exists('inject', $anno)) { $classToMake = $anno['inject']->class; $class = new $classToMake(); $propName = $p->name; if ($this->{$propName} == null) { $this->{$propName} = $class; } } } }
public function describeClass($class) { $return_array = null; if (self::$_cache_directory) { $filename = self::getCacheDirectory() . str_replace("\\", '_', $class) . '_description.cache.php'; if (file_exists($filename)) { $return_array = unserialize(file_get_contents($filename)); } } if ($return_array === null) { $reflection_class = new \ReflectionClass($class); $reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache()); $reader->setEnableParsePhpImports(true); $reader->setDefaultAnnotationNamespace('RedpillLinpro\\GamineBundle\\Annotations\\'); $return_array = array(); foreach ($reflection_class->getProperties() as $property) { $is_id = false; $annotations = $reader->getPropertyAnnotations($property); foreach ($annotations as $annotation) { if (!method_exists($annotation, 'getKey')) { continue; } switch ($annotation->getKey()) { case 'id': $return_array['primary_key']['property'] = $property->name; $return_array['primary_key']['key'] = $property->name; $is_id = true; break; } $return_array['properties'][$property->name][$annotation->getKey()] = (array) $annotation; } if ($is_id && isset($return_array['properties'][$property->name]['column']['name'])) { $return_array['primary_key']['key'] = $return_array['properties'][$property->name]['column']['name']; } } if (self::$_cache_directory) { file_put_contents($filename, serialize($return_array)); } } return $return_array; }
/** * @param $reflector * @return array * returns an array property_name => property_type */ private function my_class_type($reflector) { $annotationReader = new \Doctrine\Common\Annotations\AnnotationReader(); $typeArray = array(); $tmpArray = array(); foreach ($reflector->getProperties() as $property) { $tmpArray[$property->name] = $annotationReader->getPropertyAnnotations($property); } foreach ($tmpArray as $prop => $p) { foreach ($p as $r) { if (is_a($r, 'Doctrine\\ORM\\Mapping\\Column')) { $typeArray[$prop] = $r->type; } } } return $typeArray; }