Skip to content

Commit

Permalink
Merge pull request #1146 from oat-sa/fix/LSI-5144/avoid-looping-throu…
Browse files Browse the repository at this point in the history
…gh-each-service

fix: avoid looping through each service when the service ID is missing from the PSR container and has to be loaded via ServiceManager
  • Loading branch information
wazelin authored Jan 13, 2025
2 parents bed756c + a3ec73a commit 3297622
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions core/DependencyInjection/BaseContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* @author Gabriel Felipe Soares <[email protected]>
* Copyright (c) 2021-2025 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace oat\generis\model\DependencyInjection;

use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
Expand All @@ -50,12 +48,16 @@ public function __construct(ParameterBagInterface $parameterBag = null, Containe
/**
* @inheritDoc
*/
public function get($id, int $invalidBehavior = 1)
public function get($id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
{
try {
return parent::get($id, $invalidBehavior);
} catch (ServiceNotFoundException $exception) {
return $this->legacyContainer->get($id);
return parent::get($id, self::NULL_ON_INVALID_REFERENCE) ?? $this->legacyContainer->get($id);
} catch (NotFoundExceptionInterface $exception) {
if ($invalidBehavior >= self::NULL_ON_INVALID_REFERENCE) {
return null;
}

throw $exception;
}
}

Expand Down

0 comments on commit 3297622

Please sign in to comment.