Question

I was poking around for academic reasons on how the objects are instantiated. I figured out most of the things and the differences between factories (dev, compiled, production) but something scared me.

Magento\Framework\ObjectManager\Factory\Compiled::create().

public function create($requestedType, array $arguments = [])
{
    $args = $this->config->getArguments($requestedType);
    $type = $this->config->getInstanceType($requestedType);
    if (!$args) {
        return new $type();
    }
    foreach ($args as $key => &$argument) {
        if (isset($arguments[$key])) {
            $argument = $arguments[$key];
        } elseif (isset($argument['_i_'])) {
            $argument = $this->get($argument['_i_']);
        } elseif (isset($argument['_ins_'])) {
            $argument = $this->create($argument['_ins_']);
        } elseif (isset($argument['_v_'])) {
            $argument = $argument['_v_'];
        } elseif (isset($argument['_vac_'])) {
            $argument = $argument['_vac_'];
            $this->parseArray($argument);
        } elseif (isset($argument['_vn_'])) {
            $argument = null;
        } elseif (isset($argument['_a_'])) {
            if (isset($this->globalArguments[$argument['_a_']])) {
                $argument = $this->globalArguments[$argument['_a_']];
            } else {
                $argument = $argument['_d_'];
            }
        }
    }
    $args = array_values($args);
    return $this->createObject($type, $args);
}

What do all the underscore prefixed and suffixed elements mean and do? like _a_, _d_ and so on. An example or 2 would be awesome.

Was it helpful?

Solution

The details of those arguments can be found in the Magento\Setup\Module\Di\Compiler\ArgumentsResolver :

/**
 * Shared instance argument pattern used for configuration
 *
 * @var array
 */
private $sharedInstancePattern = [
    '_i_' => null,
];

/**
 * Instance argument pattern used for configuration
 *
 * @var array
 */
private $notSharedInstancePattern = [
    '_ins_' => null,
];

/**
 * Value argument pattern used for configuration
 *
 * @var array
 */
private $valuePattern = [
    '_v_' => null,
];

/**
 * Value null argument pattern used for configuration
 *
 * @var array
 */
private $nullValuePattern = [
    '_vn_' => true,
];

/**
 * Value configured array argument pattern used for configuration
 *
 * @var array
 */
private $configuredArrayValuePattern = [
    '_vac_' => true,
];

/**
 * Configured argument pattern used for configuration
 *
 * @var array
 */
private $configuredPattern = [
    '_a_' => null,
    '_d_' => null
];

On top of that, you can probably check the Magento\Framework\ObjectManager\Test\Unit\Factory\CompiledTest unit tests to see examples of arguments.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top