P
P
postgresdev2019-10-24 19:36:26
symfony
postgresdev, 2019-10-24 19:36:26

Can't inject via method in Symfony 3.2, what's the right way?

There is a code:

app.stock_transformer:
    class: AppBundle\DataTransformer\InboundApi\StockTransformer
    calls:
      - method: setLogger
        arguments:
          - '@logger'

The setLogger method does not enter at all, what's the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2019-10-24
@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());
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question