Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 93 additions & 73 deletions Metadata/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,64 +66,50 @@ public function loadMetadataForClass(\ReflectionClass $class)
return null;
}

foreach ($this->reader->getClassAnnotations($class) as $annot) {
if ($annot instanceof Service) {
if (null === $annot->id) {
$metadata->id = $this->generateId($className);
} else {
$metadata->id = $annot->id;
}
$hasInjection = false;

if($parentClass = $class->getParentClass()) {
$this->buildAnnotations($parentClass, $metadata);
$this->buildProperties($parentClass, $metadata, $hasInjection);
// temp disabledavoid test failing, only child lookup for now
// @todo fix test failing Cannot redeclare class EnhancedProxy_...SecuredController in EnhancedProxy___CG__-JMS-DiExtraBundle-Tests-Functional-Bundle-TestBundle-Controller-SecuredController.php on line 11
//$this->buildMethods($parentClass, $metadata, $hasInjection);
}

$this->buildAnnotations($class, $metadata);
$this->buildProperties($class, $metadata, $hasInjection);
$this->buildMethods($class, $metadata, $hasInjection);

$metadata->parent = $annot->parent;
$metadata->public = $annot->public;
$metadata->scope = $annot->scope;
$metadata->abstract = $annot->abstract;
} else if ($annot instanceof Tag) {
$metadata->tags[$annot->name][] = $annot->attributes;
} else if ($annot instanceof Validator) {
// automatically register as service if not done explicitly
if (null === $metadata->id) {
$metadata->id = $this->generateId($className);
}
if (null == $metadata->id && !$hasInjection) {
return null;
}

$metadata->tags['validator.constraint_validator'][] = array(
'alias' => $annot->alias,
);
} else if ($annot instanceof AbstractDoctrineListener) {
if (null === $metadata->id) {
$metadata->id = $this->generateId($className);
}
return $metadata;
}

foreach ($annot->events as $event) {
$metadata->tags[$annot->getTag()][] = array(
'event' => $event,
'connection' => $annot->connection ?: 'default',
'lazy' => $annot->lazy,
'priority' => $annot->priority,
);
}
} else if ($annot instanceof FormType) {
if (null === $metadata->id) {
$metadata->id = $this->generateId($className);
}
private function convertReferenceValue($name, AnnotReference $annot)
{
if (null === $annot->value) {
return new Reference($this->generateId($name), false !== $annot->required ? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE : ContainerInterface::NULL_ON_INVALID_REFERENCE, $annot->strict);
}

$alias = $annot->alias;
if (false === strpos($annot->value, '%')) {
return new Reference($annot->value, false !== $annot->required ? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE : ContainerInterface::NULL_ON_INVALID_REFERENCE, $annot->strict);
}

// try to extract it from the class itself
if (null === $alias) {
$instance = unserialize(sprintf('O:%d:"%s":0:{}', strlen($className), $className));
$alias = $instance->getName();
}
return $annot->value;
}

$metadata->tags['form.type'][] = array(
'alias' => $alias,
);
}
}
private function generateId($name)
{
$name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name);

$hasInjection = false;
foreach ($class->getProperties() as $property) {
if ($property->getDeclaringClass()->getName() !== $className) {
return strtolower(strtr($name, '\\', '.'));
}

private function buildProperties($class, &$metadata, &$hasInjection) {
foreach ($class->getProperties() as $property) {
if ($property->getDeclaringClass()->getName() !== $class->getName()) {
continue;
}
$name = $property->getName();
Expand All @@ -135,9 +121,11 @@ public function loadMetadataForClass(\ReflectionClass $class)
}
}
}

}

private function buildMethods($class, &$metadata, &$hasInjection) {
foreach ($class->getMethods() as $method) {
if ($method->getDeclaringClass()->getName() !== $className) {
if ($method->getDeclaringClass()->getName() !== $class->getName()) {
continue;
}
$name = $method->getName();
Expand Down Expand Up @@ -199,31 +187,63 @@ public function loadMetadataForClass(\ReflectionClass $class)
}
}
}

if (null == $metadata->id && !$hasInjection) {
return null;
}

return $metadata;
}

private function buildAnnotations($class, &$metadata) {
$className = $class->getName();
foreach ($this->reader->getClassAnnotations($class) as $annot) {
if ($annot instanceof Service) {
if (null === $annot->id) {
$metadata->id = $this->generateId($className);
} else {
$metadata->id = $annot->id;
}

private function convertReferenceValue($name, AnnotReference $annot)
{
if (null === $annot->value) {
return new Reference($this->generateId($name), false !== $annot->required ? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE : ContainerInterface::NULL_ON_INVALID_REFERENCE, $annot->strict);
}
$metadata->parent = $annot->parent;
$metadata->public = $annot->public;
$metadata->scope = $annot->scope;
$metadata->abstract = $annot->abstract;
} else if ($annot instanceof Tag) {
$metadata->tags[$annot->name][] = $annot->attributes;
} else if ($annot instanceof Validator) {
// automatically register as service if not done explicitly
if (null === $metadata->id) {
$metadata->id = $this->generateId($className);
}

if (false === strpos($annot->value, '%')) {
return new Reference($annot->value, false !== $annot->required ? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE : ContainerInterface::NULL_ON_INVALID_REFERENCE, $annot->strict);
}
$metadata->tags['validator.constraint_validator'][] = array(
'alias' => $annot->alias,
);
} else if ($annot instanceof AbstractDoctrineListener) {
if (null === $metadata->id) {
$metadata->id = $this->generateId($className);
}

return $annot->value;
}
foreach ($annot->events as $event) {
$metadata->tags[$annot->getTag()][] = array(
'event' => $event,
'connection' => $annot->connection ?: 'default',
'lazy' => $annot->lazy,
'priority' => $annot->priority,
);
}
} else if ($annot instanceof FormType) {
if (null === $metadata->id) {
$metadata->id = $this->generateId($className);
}

private function generateId($name)
{
$name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name);
$alias = $annot->alias;

return strtolower(strtr($name, '\\', '.'));
// try to extract it from the class itself
if (null === $alias) {
$instance = unserialize(sprintf('O:%d:"%s":0:{}', strlen($className), $className));
$alias = $instance->getName();
}

$metadata->tags['form.type'][] = array(
'alias' => $alias,
);
}
}
}
}