Skip to content

Commit

Permalink
Improved performance by over 5% for resolving not set services.
Browse files Browse the repository at this point in the history
  • Loading branch information
renakdup committed Dec 1, 2023
1 parent d2fd778 commit 057d93a
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ public function set( string $id, $service ): void {
* {@inheritdoc}
*/
public function get( string $id ) {
// if ( isset( $this->resolved[ $id ] ) || array_key_exists( $id, $this->resolved ) ) {
// return $this->resolved[ $id ];
// }

$service = $this->resolve( $id );

// $this->resolved[ $id ] = $service;

return $service;
}


/**
* @param string $id
*
* @return mixed|object
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ReflectionException
*/
public function make( string $id ) {
if ( isset( $this->resolved[ $id ] ) || array_key_exists( $id, $this->resolved ) ) {
return $this->resolved[ $id ];
}
Expand All @@ -107,7 +129,12 @@ public function get( string $id ) {
}

/**
* @param string $id
*
* @return mixed
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ReflectionException
*/
protected function resolve( string $id ) {
if ( $this->has( $id ) ) {
Expand All @@ -131,6 +158,11 @@ protected function resolve( string $id ) {

/**
* @param class-string $service
*
* @return object
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ReflectionException
*/
protected function resolve_object( string $service ): object {
$reflected_class = new ReflectionClass( $service );
Expand All @@ -149,11 +181,6 @@ protected function resolve_object( string $service ): object {
$constructor_args = [];
foreach ( $params as $param ) {
if ( $param_class = $param->getClass() ) {
if ( $this->has( $param_class->getName() ) ) {
$constructor_args[] = $this->get( $param_class->getName() );
continue;
}

$constructor_args[] = $this->get( $param_class->getName() );
continue;
}
Expand Down

0 comments on commit 057d93a

Please sign in to comment.