public function setup() { $diHelper = self::getContainerFromAnnotations(); $container = $diHelper->getContainer(); // check service namee if (!($serviceName = $this->getServiceName($diHelper))) { // does this container have a service defined with this class name? // run up class heiracy $r = $diHelper->reflector; while (!$serviceName and $r and $r->getName() !== __CLASS__) { $_class = \Bond\get_unqualified_class($r->getName()); if ($container->has($_class)) { $serviceName = $_class; } $r = $r->getParentClass(); } if (!$serviceName) { return $container; //throw new NoServiceDefinedException($reflector); throw new NoServiceDefinedException($reflector); } } $clone = $container->get($serviceName); foreach ($diHelper->reflector->getProperties() as $property) { if ($property->getDeclaringClass()->getName() !== \PHPUnit_Framework_TestCase::class and !$property->isStatic()) { // We need a different 'getter' reflection object because we can then work // with objects of different classes. try { $reflGetter = new \ReflectionProperty($clone, $property->getName()); } catch (\ReflectionException $e) { continue; } $reflGetter->setAccessible(true); $value = $reflGetter->getValue($clone); $property->setAccessible(true); $property->setValue($this, $value); } } return $container; }
/** * Standard constructor */ public function __construct($fullyQualifiedClass, array $registrations = []) { $this->class = new PhpClass(\Bond\get_unqualified_class($fullyQualifiedClass), \Bond\get_namespace($fullyQualifiedClass), false); if (!$registrations) { return; } foreach ($registrations as &$registration) { $registration = sprintf("\$em->register( '%s', '%s' );", addslashes($registration[0]), addslashes($registration[1])); } $registrations = new Format($registrations); $this->class->classComponents[] = new FunctionDeclaration('__construct', new Sformatf(<<<'PHP' /** * Register a set of entities with a EntityManager * @inheritDoc */ public function __construct( \Bond\EntityManager $em ) { %s } PHP , $registrations->indent(20))); }
/** * Return array of entities which are referenced by $entity * @param Base|Container $entity * @return array */ public function referencedBy($entity) { if (!($entity instanceof Base || $entity instanceof Container)) { throw new \InvalidArgumentException("You can only lookup references by Entity or Container"); } $class = $entity instanceof Container ? $entity->class : $entity; $properties = $this->propertiesOfTypeGet($class); // found a entity? if (!$properties) { throw new \LogicException(sprintf("Can't find which entity property for repository `%s` references entity of type `%s`", $this->entity, $entityClass)); } elseif (count($properties) > 1) { throw new \LogicException(sprintf("Multiple columns [%s] reference a entity of this type [%s]. This is ambiguous. Can't help you here.", implode(",", $properties), \Bond\get_unqualified_class($entity))); } $property = array_pop($properties); // get references to this entity $filterComponent = $this->getFindFilterFactory()->get($property, null, $entity); return $this->findByFilterComponents(FindFilterComponentFactory::FIND_ALL, array($property => $filterComponent)); }
public function testgetUnQualifiedClass() { $this->assertSame(\Bond\get_unqualified_class("Bond\\RecordManager"), "RecordManager"); $this->assertSame(\Bond\get_unqualified_class("RecordManager"), "RecordManager"); $this->assertSame(\Bond\get_unqualified_class(new \Some\Weird\WonderFull\stdclass()), "stdclass"); $this->assertSame(\Bond\get_unqualified_class(new stdClass()), "stdClass"); }
/** * Is registered? * @param Entity|String $key */ public function isRegistered($entity) { if (is_object($entity)) { return isset($this->registrations[get_class($entity)]); } return isset($this->registrations[$entity]) || isset($this->names[\Bond\get_unqualified_class($entity)]); }