NestedAccessor: add getService and hasService #480

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+24 −2
Diff settings

Always

Just for now

@@ -43,6 +43,28 @@ public function __construct(Container $container, $namespace)
+ /**
+ * @param string service name
+ * @return object
+ */
+ public function getService($name)
+ {
+ return $this->container->getService($this->namespace . $name);
+ }
+
+
+
+ /**
+ * @param string service name
+ * @return bool
+ */
+ public function hasService($name)
+ {
+ return $this->container->hasService($this->namespace . $name);
+ }
+
+
+
/**
* @return object
*/
@@ -64,7 +86,7 @@ public function __call($name, $args)
*/
public function &__get($name)
{
- $service = $this->container->getService($this->namespace . $name);
+ $service = $this->getService($name);
return $service;

This comment has been minimized.

Show comment Hide comment
@mCzolko

mCzolko Mar 26, 2012

return $this->getService($name); není tam ta proměnná $service zbytečná?

@mCzolko

mCzolko Mar 26, 2012

return $this->getService($name); není tam ta proměnná $service zbytečná?

This comment has been minimized.

Show comment Hide comment
@Majkl578

Majkl578 Mar 26, 2012

Contributor

Je.

@Majkl578

Majkl578 Mar 26, 2012

Contributor

Je.

This comment has been minimized.

Show comment Hide comment
@kravco

kravco Mar 26, 2012

Contributor

Nie, nie je.

Keďže Nette\Object deklaruje, že __get() vracia referenciu, musia takto fungovať i podedené triedy (aj Nette\DI\NestedAccessor). Takáto konštrukcia zabezpečí, že __get() nevráti referenciu na službu, ale len referenciu na kópiu v premennej $service.

@kravco

kravco Mar 26, 2012

Contributor

Nie, nie je.

Keďže Nette\Object deklaruje, že __get() vracia referenciu, musia takto fungovať i podedené triedy (aj Nette\DI\NestedAccessor). Takáto konštrukcia zabezpečí, že __get() nevráti referenciu na službu, ale len referenciu na kópiu v premennej $service.

This comment has been minimized.

Show comment Hide comment
@Majkl578

Majkl578 Mar 26, 2012

Contributor

Pravda, to jsem si neuvědomil. Bez toho by to řvalo E_NOTICE.

@Majkl578

Majkl578 Mar 26, 2012

Contributor

Pravda, to jsem si neuvědomil. Bez toho by to řvalo E_NOTICE.

This comment has been minimized.

Show comment Hide comment
@mCzolko

mCzolko Mar 28, 2012

sorry, nevšiml jsem si ukazatele. a i tak díky za vysvětlení :)

@mCzolko

mCzolko Mar 28, 2012

sorry, nevšiml jsem si ukazatele. a i tak díky za vysvětlení :)

}
@@ -85,7 +107,7 @@ public function __set($name, $service)
*/
public function __isset($name)
{
- return $this->container->hasService($this->namespace . $name);
+ return $this->hasService($name);
}