@postgresdev

Не получается сделать инъекцию через метод в Symfony 3.2, как правильно?

Есть код:
app.stock_transformer:
    class: AppBundle\DataTransformer\InboundApi\StockTransformer
    calls:
      - method: setLogger
        arguments:
          - '@logger'


В метод setLogger вообще не заходит, в чем проблема?
  • Вопрос задан
  • 99 просмотров
Пригласить эксперта
Ответы на вопрос 1
IgorPI
@IgorPI
# JsonRequest
    App\Service\JsonRequest:
        calls:
            - [setRequest, ['@request_stack']]


<?php


namespace App\Service;


use Exception;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Class JsonRequest
 * @package App\Service
 */
class JsonRequest
{

    private $json_object;

    public function __construct()
    {
    }

    /**
     * @param string $key
     * @param bool $default
     * @return mixed
     */
    public function get(string $key, $default = null)
    {
        try {
            $properties = explode(".", $key);
            $buf = $this->json_object;
            foreach ($properties as $property) {
                if (property_exists($buf, $property)) {
                    $buf = $buf->{$property};
                } else {
                    return $default;
                }
            }
            return $buf;
        } catch (Exception $e) {
            return $default;
        }
    }

    /**
     * @param string $key
     * @return bool
     */
    public function has(string $key)
    {
        return property_exists($this->json_object, $key);
    }


    /**
     * @param RequestStack $request
     */
    public function setRequest(RequestStack $request): void
    {
        $this->json_object = json_decode($request->getCurrentRequest()->getContent());
    }
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы