Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NestedAccessor: add getService and hasService #480

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions Nette/DI/NestedAccessor.php
Expand Up @@ -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
*/
Expand All @@ -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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

}

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


Expand Down