minor #20117 [HttpKernel] Parse bundle name+namespace at once (ro0NL)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[HttpKernel] Parse bundle name+namespace at once

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | comma-separated list of tickets fixed by the PR, if any
| License       | MIT
| Doc PR        | reference to the documentation PR, if any

Commits
-------

81b2922 parse bundle name+namespace at once
This commit is contained in:
Fabien Potencier 2016-10-02 08:58:55 -07:00
commit f54cfd66c5

View File

@ -31,6 +31,7 @@ abstract class Bundle implements BundleInterface
protected $name; protected $name;
protected $extension; protected $extension;
protected $path; protected $path;
private $namespace;
/** /**
* Boots the Bundle. * Boots the Bundle.
@ -106,9 +107,11 @@ abstract class Bundle implements BundleInterface
*/ */
public function getNamespace() public function getNamespace()
{ {
$class = get_class($this); if (null === $this->namespace) {
$this->parseClassName();
}
return substr($class, 0, strrpos($class, '\\')); return $this->namespace;
} }
/** /**
@ -142,14 +145,11 @@ abstract class Bundle implements BundleInterface
*/ */
final public function getName() final public function getName()
{ {
if (null !== $this->name) { if (null === $this->name) {
return $this->name; $this->parseClassName();
} }
$name = get_class($this); return $this->name;
$pos = strrpos($name, '\\');
return $this->name = false === $pos ? $name : substr($name, $pos + 1);
} }
/** /**
@ -218,4 +218,11 @@ abstract class Bundle implements BundleInterface
return new $class(); return new $class();
} }
} }
private function parseClassName()
{
$pos = strrpos(static::class, '\\');
$this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
$this->name = false === $pos ? static::class : substr(static::class, $pos + 1);
}
} }