Skip to content

Commit 214a270

Browse files
author
Joan He
committed
Merge remote-tracking branch 'origin/MAGETWO-30809-compiled-definitions' into develop
2 parents 7ac0f3e + 18b6ee9 commit 214a270

File tree

2 files changed

+66
-1
lines changed
  • dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Definition
  • lib/internal/Magento/Framework/ObjectManager/Definition

2 files changed

+66
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\ObjectManager\Definition;
7+
8+
class CompiledTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function testGetParametersWithUndefinedDefinition()
11+
{
12+
$objectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
13+
$undefinedDefinitionSignature = new \stdClass();
14+
$className = 'undefinedDefinition';
15+
$readerMock = $this->getMock(
16+
'\Magento\Framework\Code\Reader\ClassReader',
17+
['getConstructor'],
18+
[],
19+
'',
20+
false
21+
);
22+
$readerMock->expects($this->once())
23+
->method('getConstructor')
24+
->with($className)
25+
->willReturn($undefinedDefinitionSignature);
26+
$model = $objectManager->getObject(
27+
'Magento\Framework\ObjectManager\Definition\CompiledStub',
28+
[
29+
'definitions' => [[], []],
30+
'reader' => $readerMock
31+
]
32+
);
33+
$this->assertEquals($undefinedDefinitionSignature, $model->getParameters($className));
34+
}
35+
}
36+
37+
/**
38+
* Stub class for abstract Magento\Framework\ObjectManager\Definition
39+
*/
40+
class CompiledStub extends Compiled
41+
{
42+
43+
/**
44+
* Unpack signature
45+
*
46+
* @param string $signature
47+
* @return mixed
48+
*/
49+
protected function _unpack($signature)
50+
{
51+
return unserialize($signature);
52+
}
53+
}

lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@ abstract class Compiled implements \Magento\Framework\ObjectManager\DefinitionIn
1717
*/
1818
protected $_definitions;
1919

20+
/**
21+
* @var \Magento\Framework\Code\Reader\ClassReader
22+
*/
23+
protected $reader ;
24+
2025
/**
2126
* @param array $definitions
27+
* @param \Magento\Framework\Code\Reader\ClassReader $reader
2228
*/
23-
public function __construct(array $definitions)
29+
public function __construct(array $definitions, \Magento\Framework\Code\Reader\ClassReader $reader = null)
2430
{
2531
list($this->_signatures, $this->_definitions) = $definitions;
32+
$this->reader = $reader ?: new \Magento\Framework\Code\Reader\ClassReader();
2633
}
2734

2835
/**
@@ -51,6 +58,11 @@ abstract protected function _unpack($signature);
5158
*/
5259
public function getParameters($className)
5360
{
61+
// if the definition isn't found in the list gathered from the compiled file then using reflection to find it
62+
if (!array_key_exists($className, $this->_definitions)) {
63+
return $this->reader->getConstructor($className);
64+
}
65+
5466
$definition = $this->_definitions[$className];
5567
if ($definition !== null) {
5668
if (is_string($this->_signatures[$definition])) {

0 commit comments

Comments
 (0)