/** * {@inheritdoc} */ public function execute() { // sanity check if ($this->bindingDescriptor->isLoaded()) { return; } $typeName = $this->bindingDescriptor->getTypeName(); $typeDescriptor = $this->typeDescriptors->contains($typeName) ? $this->typeDescriptors->getFirst($typeName) : null; $this->bindingDescriptor->load($this->containingPackage, $typeDescriptor); $uuid = $this->bindingDescriptor->getUuid(); if ($this->bindingDescriptors->contains($uuid)) { $this->previousDescriptor = $this->bindingDescriptors->get($uuid); } $this->bindingDescriptors->add($this->bindingDescriptor); }
public function testCreateWithCustomUuid() { $descriptor = new BindingDescriptor('/path', 'vendor/type', array('param' => 'value'), 'glob', $this->uuid); $this->assertSame($this->uuid, $descriptor->getUuid()); $this->assertSame('/path', $descriptor->getQuery()); $this->assertSame('glob', $descriptor->getLanguage()); $this->assertSame('vendor/type', $descriptor->getTypeName()); $this->assertSame(array('param' => 'value'), $descriptor->getParameterValues()); }
/** * Unloads and loads a binding descriptor. * * The descriptor is remembered and reloaded again in {@link postRollback()} * if the intercepted operation needs to be rolled back. * * @param BindingDescriptor $bindingDescriptor The descriptor to reload. */ protected function reloadBindingDescriptor(BindingDescriptor $bindingDescriptor) { if (!$bindingDescriptor->isLoaded()) { return; } // Keep backup of containing package before calling unload() $containingPackage = $bindingDescriptor->getContainingPackage(); $typeName = $bindingDescriptor->getTypeName(); $typeDescriptor = $this->typeDescriptors->getEnabled($typeName); // never fails with the check in the beginning $bindingDescriptor->unload(); // never fails after unloading, given that the type name matches // (which we can guarantee here) $bindingDescriptor->load($containingPackage, $typeDescriptor); $this->reloadedDescriptors[] = $bindingDescriptor; }
/** * {@inheritdoc} */ public function addRootBinding(BindingDescriptor $bindingDescriptor, $flags = 0) { $this->assertPackagesLoaded(); $typeName = $bindingDescriptor->getTypeName(); $typeExists = $this->typeDescriptors->contains($typeName); if (!($flags & self::IGNORE_TYPE_NOT_FOUND) && !$typeExists) { throw NoSuchTypeException::forTypeName($typeName); } if (!($flags & self::IGNORE_TYPE_NOT_ENABLED) && $typeExists && !$this->typeDescriptors->getFirst($typeName)->isEnabled()) { throw TypeNotEnabledException::forTypeName($typeName); } $uuid = $bindingDescriptor->getUuid(); if ($this->bindingDescriptors->contains($uuid)) { throw DuplicateBindingException::forUuid($uuid); } $tx = new Transaction(); try { $syncOp = $this->syncBindingUuid($uuid); $syncOp->takeSnapshot(); $tx->execute($this->loadBindingDescriptor($bindingDescriptor, $this->rootPackage)); $this->assertBindingValid($bindingDescriptor); $tx->execute($this->addBindingDescriptorToPackageFile($bindingDescriptor)); $tx->execute($syncOp); $this->saveRootPackageFile(); $tx->commit(); } catch (Exception $e) { $tx->rollback(); throw $e; } }
/** * {@inheritdoc} */ public function addRootBindingDescriptor(BindingDescriptor $bindingDescriptor, $flags = 0) { $this->assertModulesLoaded(); $typeName = $bindingDescriptor->getTypeName(); $typeExists = $this->typeDescriptors->contains($typeName); if (!($flags & self::IGNORE_TYPE_NOT_FOUND) && !$typeExists) { throw NoSuchTypeException::forTypeName($typeName); } if (!($flags & self::IGNORE_TYPE_NOT_ENABLED) && $typeExists && !$this->typeDescriptors->getFirst($typeName)->isEnabled()) { throw TypeNotEnabledException::forTypeName($typeName); } $uuid = $bindingDescriptor->getUuid(); $exists = $this->bindingDescriptors->contains($uuid); $existsInNonRoot = $exists ? !$this->bindingDescriptors->get($uuid)->getContainingModule() instanceof RootModule : false; // We can only override bindings in the root module if ($existsInNonRoot || $exists && !($flags & self::OVERRIDE)) { throw DuplicateBindingException::forUuid($uuid); } $tx = new Transaction(); try { $syncOp = $this->syncBindingUuid($uuid); $syncOp->takeSnapshot(); $tx->execute($this->loadBindingDescriptor($bindingDescriptor, $this->rootModule)); $this->assertBindingValid($bindingDescriptor); $tx->execute($this->addBindingDescriptorToModuleFile($bindingDescriptor)); $tx->execute($syncOp); $this->saveRootModuleFile(); $tx->commit(); } catch (Exception $e) { $tx->rollback(); throw $e; } }
private function syncBindingUuid(BindingDescriptor $enabledBefore = null, BindingDescriptor $enabledAfter = null) { if (!$enabledBefore && $enabledAfter) { $this->discovery->bind($enabledAfter->getQuery(), $enabledAfter->getTypeName(), $enabledAfter->getParameterValues(), $enabledAfter->getLanguage()); } elseif ($enabledBefore && !$enabledAfter) { $this->discovery->unbind($enabledBefore->getQuery(), $enabledBefore->getTypeName(), $enabledBefore->getParameterValues(), $enabledBefore->getLanguage()); } }
private function bindingsEqual(BindingDescriptor $binding1, BindingDescriptor $binding2) { if ($binding1->getUuid() !== $binding2->getUuid()) { return false; } if ($binding1->getTypeName() !== $binding2->getTypeName()) { return false; } if ($binding1->getQuery() !== $binding2->getQuery()) { return false; } if ($binding1->getLanguage() !== $binding2->getLanguage()) { return false; } if ($binding1->getParameterValues() !== $binding2->getParameterValues()) { return false; } return true; }