M
M
Mark2021-02-04 15:29:42
Yii
Mark, 2021-02-04 15:29:42

How to negotiate with the container so that it forwards itself to the constructor?

Purpose: I want to use the Container component in my class to initialize objects. Application - console .
Problem: An exception is thrown when using the get() method:

Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: yii\di\Container::params'

Description of the initialization of the Container class in the Boostrap component:
$container = \Yii::$container;
$container->setSingleton(Container::class, function () use ($container) {
  return $container;
});

The class in which the method is called:
class ObjectCreator
{
    private $container;

    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    public function creator($searchKey, array $map, $implementClass)
    {
        foreach ($map as $key => $class) {
            if ($searchKey === $key) {
                $targetObject = $this->container->get($class); // <<<<<<<< Вызов здесь

                if (!$targetObject instanceof $implementClass) {
                    throw new \RuntimeException(get_class($targetObject) . " must be instance of " . $implementClass);
                }
                return $targetObject;
            }
        }
        throw new \InvalidArgumentException("Unsupported key");
    }
}


Stack trace


Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: yii\di\Container::params'

in /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/base/Component.php:155

Stack trace:
#0 /home/admin/web/prolet.loc/public_html/common/bootstrap/SetUp.php(39): yii\base\Component->__get('params')
#1 [internal function]: common\bootstrap\SetUp->common\bootstrap\{closure}(Object(yii\di\Container), Array, Array)
#2 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/di/Container.php(178): call_user_func(Object(Closure), Object(yii\di\Container), Array, Array)
#3 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/di/Instance.php(170): yii\di\Container->get('core\\lib\\matra...')
#4 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/di/Container.php(589): yii\di\Instance->get(Object(yii\di\Container))
#5 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/di/Container.php(407): yii\di\Container->resolveDependencies(Array, Object(ReflectionClass))
#6 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/di/Container.php(171): yii\di\Container->build('core\\lib\\matra...', Array, Array)
#7 /home/admin/web/prolet.loc/public_html/core/lib/goodsProvider/ProviderFactory.php(71): yii\di\Container->get('core\\lib\\matra...')
#8 /home/admin/web/prolet.loc/public_html/core/lib/goodsProvider/ProviderFactory.php(64): core\lib\goodsProvider\ProviderFactory->creator('matra', Array, 'core\\lib\\goodsP...')
#9 /home/admin/web/prolet.loc/public_html/core/services/order/ProviderManager.php(29): core\lib\goodsProvider\ProviderFactory->createBuyClass('matra')
#10 /home/admin/web/prolet.loc/public_html/console/controllers/OrderController.php(31): core\services\order\ProviderManager->buy(Object(core\entities\Order\Order))
#11 [internal function]: console\controllers\OrderController->actionPurchase()
#12 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#13 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/base/Controller.php(181): yii\base\InlineAction->runWithParams(Array)
#14 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/console/Controller.php(184): yii\base\Controller->runAction('purchase', Array)
#15 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('purchase', Array)
#16 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module->runAction('/order/purchase', Array)
#17 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/console/Application.php(148): yii\console\Application->runAction('/order/purchase', Array)
#18 /home/admin/web/prolet.loc/public_html/vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application->handleRequest(Object(yii\console\Request))
#19 /home/admin/web/prolet.loc/public_html/yii(23): yii\base\Application->run()
#20 {main}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2021-02-05
@MarkLb

Remove this:

$container = \Yii::$container;
$container->setSingleton(Container::class, function () use ($container) {
  return $container;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question