Пример #1
0
 /**
  * Removes a component from the IContainer.
  * @return void
  */
 public function removeComponent(IComponent $component)
 {
     $name = $component->getName();
     if (!isset($this->components[$name]) || $this->components[$name] !== $component) {
         throw new Nette\InvalidArgumentException("Component named '{$name}' is not located in this container.");
     }
     unset($this->components[$name]);
     $component->setParent(NULL);
 }
Пример #2
0
 protected function attachToPresenter(IComponent $component, $name = NULL)
 {
     if ($name === NULL) {
         if (!($name = $component->getName())) {
             $name = $component->getReflection()->getShortName();
         }
     }
     if (!$this->__testbench_presenterMock) {
         $this->__testbench_presenterMock = __testbench_getService('Testbench\\PresenterMock');
         $container = \Testbench\ContainerFactory::create(FALSE);
         $container->callInjects($this->__testbench_presenterMock);
     }
     $this->__testbench_presenterMock->onStartup[] = function (PresenterMock $presenter) use($component, $name) {
         try {
             $presenter->removeComponent($component);
         } catch (\Nette\InvalidArgumentException $exc) {
         }
         $presenter->addComponent($component, $name);
     };
     $this->__testbench_presenterMock->run(new ApplicationRequestMock());
 }
Пример #3
0
 /**
  * Adds the specified component to the IContainer.
  *
  * @param  IComponent
  * @param  string
  * @param  string
  *
  * @return Container  provides a fluent interface
  * @throws Nette\InvalidStateException
  */
 public function addComponent(IComponent $component, $name, $insertBefore = null)
 {
     if ($name === null) {
         $name = $component->getName();
     }
     if (is_int($name)) {
         $name = (string) $name;
     } elseif (!is_string($name)) {
         throw new Nette\InvalidArgumentException("Component name must be integer or string, " . gettype($name) . " given.");
     } elseif (!preg_match('#^[a-zA-Z0-9_]+$#', $name)) {
         throw new Nette\InvalidArgumentException("Component name must be non-empty alphanumeric string, '{$name}' given.");
     }
     if (isset($this->components[$name])) {
         throw new Nette\InvalidStateException("Component with name '{$name}' already exists.");
     }
     // check circular reference
     $obj = $this;
     do {
         if ($obj === $component) {
             throw new Nette\InvalidStateException("Circular reference detected while adding component '{$name}'.");
         }
         $obj = $obj->getParent();
     } while ($obj !== null);
     // user checking
     $this->validateChildComponent($component);
     try {
         if (isset($this->components[$insertBefore])) {
             $tmp = array();
             foreach ($this->components as $k => $v) {
                 if ($k === $insertBefore) {
                     $tmp[$name] = $component;
                 }
                 $tmp[$k] = $v;
             }
             $this->components = $tmp;
         } else {
             $this->components[$name] = $component;
         }
         $component->setParent($this, $name);
     } catch (\Exception $e) {
         unset($this->components[$name]);
         // undo
         throw $e;
     }
     return $this;
 }
 public function isPersistent(\Nette\ComponentModel\IComponent $object)
 {
     static $persistentParameters = NULL;
     if ($persistentParameters === NULL) {
         $presenter = $object instanceof \Nette\Application\IPresenter ? $object : $object->lookupPath('Nette\\Application\\IPresenter', FALSE);
         if ($presenter) {
             $persistentParameters = $presenter::getPersistentComponents();
         }
     }
     if (is_array($persistentParameters)) {
         return in_array($object->getName(), $persistentParameters);
     }
     return FALSE;
 }