ConfiguredControllerInjectionsDriver.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace JMS\DiExtraBundle\Metadata\Driver;
  3. use JMS\DiExtraBundle\Metadata\ClassMetadata;
  4. use Metadata\Driver\DriverInterface;
  5. class ConfiguredControllerInjectionsDriver implements DriverInterface
  6. {
  7. private $delegate;
  8. private $propertyInjections;
  9. private $methodInjections;
  10. public function __construct(DriverInterface $driver, array $propertyInjections, array $methodInjections)
  11. {
  12. $this->delegate = $driver;
  13. $this->propertyInjections = $propertyInjections;
  14. $this->methodInjections = $methodInjections;
  15. }
  16. public function loadMetadataForClass(\ReflectionClass $class)
  17. {
  18. $metadata = $this->delegate->loadMetadataForClass($class);
  19. if (!preg_match('/Controller\\\(.+)Controller$/', $class->name)) {
  20. return $metadata;
  21. }
  22. if (null === $metadata) {
  23. $metadata = new ClassMetadata($class->name);
  24. }
  25. foreach ($metadata->reflection->getProperties() as $property) {
  26. // explicit injection configured?
  27. if (isset($metadata->properties[$property->name])) {
  28. continue;
  29. }
  30. // automatic injection configured?
  31. if (!isset($this->propertyInjections[$property->name])) {
  32. continue;
  33. }
  34. if ($property->getDeclaringClass()->name !== $class->name) {
  35. continue;
  36. }
  37. $metadata->properties[$property->name] = $this->propertyInjections[$property->name];
  38. }
  39. foreach ($metadata->reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
  40. // explicit injection configured?
  41. foreach ($metadata->methodCalls as $call) {
  42. if ($call[0] === $method->name) {
  43. continue 2;
  44. }
  45. }
  46. // automatic injection configured?
  47. if (!isset($this->methodInjections[$method->name])) {
  48. continue;
  49. }
  50. if ($method->getDeclaringClass()->name !== $class->name) {
  51. continue;
  52. }
  53. $metadata->methodCalls[] = array($method->name, $this->methodInjections[$method->name]);
  54. }
  55. return $metadata->properties || $metadata->methodCalls || $metadata->lookupMethods ? $metadata : null;
  56. }
  57. }