Skip to content

Commit 9e56ca9

Browse files
committed
MAGETWO-35196: Remove Test\Unit from cached DI configuration, as it brings performance degradation
- added excluded paths to classes scaner
1 parent 90bbfe5 commit 9e56ca9

File tree

10 files changed

+178
-55
lines changed

10 files changed

+178
-55
lines changed

dev/tools/Magento/Tools/Di/App/Compiler.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,50 @@ class Compiler implements \Magento\Framework\AppInterface
3232
*/
3333
private $response;
3434

35+
/**
36+
* @var array
37+
*/
38+
private $compiledPathsList = [];
39+
40+
/**
41+
* @var array
42+
*/
43+
private $excludedPathsList = [];
44+
3545
/**
3646
* @param Task\Manager $taskManager
3747
* @param ObjectManagerInterface $objectManager
3848
* @param Response $response
49+
* @param array $compiledPathsList
50+
* @param array $excludedPathsList
3951
*/
4052
public function __construct(
4153
Task\Manager $taskManager,
4254
ObjectManagerInterface $objectManager,
43-
Response $response
55+
Response $response,
56+
$compiledPathsList = [],
57+
$excludedPathsList = []
4458
) {
4559
$this->taskManager = $taskManager;
4660
$this->objectManager = $objectManager;
4761
$this->response = $response;
62+
63+
if (empty($compiledPathsList)) {
64+
$compiledPathsList = [
65+
'application' => BP . '/' . 'app/code',
66+
'library' => BP . '/' . 'lib/internal/Magento/Framework',
67+
'generated_helpers' => BP . '/' . 'var/generation'
68+
];
69+
}
70+
$this->compiledPathsList = $compiledPathsList;
71+
72+
if (empty($excludedPathsList)) {
73+
$excludedPathsList = [
74+
'application' => '#^' . BP . '/app/code/[\\w]+/[\\w]+/Test#',
75+
'framework' => '#^' . BP . '/lib/internal/[\\w]+/[\\w]+/([\\w]+/)?Test#'
76+
];
77+
}
78+
$this->excludedPathsList = $excludedPathsList;
4879
}
4980

5081
/**
@@ -85,25 +116,43 @@ public function launch()
85116
'instance' => 'Magento\Framework\App\Interception\Cache\CompiledConfig'
86117
]
87118
]
119+
],
120+
'Magento\Tools\Di\Code\Reader\ClassesScanner' => [
121+
'arguments' => [
122+
'excludePatterns' => $this->excludedPathsList
123+
]
88124
]
89125
]
90126
);
91127

92128
$operations = [
93129
Task\OperationFactory::REPOSITORY_GENERATOR => [
94-
'path' => BP . '/' . 'app/code',
130+
'path' => $this->compiledPathsList['application'],
95131
'filePatterns' => ['di' => '/\/etc\/([a-zA-Z_]*\/di|di)\.xml$/']
96132
],
97133
Task\OperationFactory::APPLICATION_CODE_GENERATOR => [
98-
BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation'
134+
$this->compiledPathsList['application'],
135+
$this->compiledPathsList['library'],
136+
$this->compiledPathsList['generated_helpers'],
99137
],
100138
Task\OperationFactory::INTERCEPTION =>
101-
BP . '/var/generation',
139+
[
140+
'intercepted_paths' => [
141+
$this->compiledPathsList['application'],
142+
$this->compiledPathsList['library'],
143+
$this->compiledPathsList['generated_helpers'],
144+
],
145+
'path_to_store' => $this->compiledPathsList['generated_helpers'],
146+
],
102147
Task\OperationFactory::AREA_CONFIG_GENERATOR => [
103-
BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation'
148+
$this->compiledPathsList['application'],
149+
$this->compiledPathsList['library'],
150+
$this->compiledPathsList['generated_helpers'],
104151
],
105152
Task\OperationFactory::INTERCEPTION_CACHE => [
106-
BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation'
153+
$this->compiledPathsList['application'],
154+
$this->compiledPathsList['library'],
155+
$this->compiledPathsList['generated_helpers'],
107156
]
108157
];
109158

dev/tools/Magento/Tools/Di/App/Task/Operation/Interception.php

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Tools\Di\Code\Generator\InterceptionConfigurationBuilder;
1010
use Magento\Framework\Interception\Code\Generator\Interceptor;
1111
use Magento\Framework\App;
12+
use Magento\Tools\Di\Code\GeneratorFactory;
13+
use Magento\Tools\Di\Code\Reader\ClassesScanner;
1214

1315
class Interception implements OperationInterface
1416
{
@@ -23,23 +25,39 @@ class Interception implements OperationInterface
2325
private $interceptionConfigurationBuilder;
2426

2527
/**
26-
* @var string
28+
* @var array
2729
*/
28-
private $data = '';
30+
private $data = [];
31+
32+
/**
33+
* @var ClassesScanner
34+
*/
35+
private $classesScanner;
36+
37+
/**
38+
* @var GeneratorFactory
39+
*/
40+
private $generatorFactory;
2941

3042
/**
3143
* @param InterceptionConfigurationBuilder $interceptionConfigurationBuilder
3244
* @param App\AreaList $areaList
33-
* @param string $data
45+
* @param ClassesScanner $classesScanner
46+
* @param GeneratorFactory $generatorFactory
47+
* @param array $data
3448
*/
3549
public function __construct(
3650
InterceptionConfigurationBuilder $interceptionConfigurationBuilder,
3751
App\AreaList $areaList,
38-
$data = ''
52+
ClassesScanner $classesScanner,
53+
GeneratorFactory $generatorFactory,
54+
$data = []
3955
) {
4056
$this->interceptionConfigurationBuilder = $interceptionConfigurationBuilder;
4157
$this->areaList = $areaList;
4258
$this->data = $data;
59+
$this->classesScanner = $classesScanner;
60+
$this->generatorFactory = $generatorFactory;
4361
}
4462

4563
/**
@@ -56,17 +74,24 @@ public function doOperation()
5674
$this->interceptionConfigurationBuilder->addAreaCode($areaCode);
5775
}
5876

77+
$classesList = [];
78+
foreach ($this->data['intercepted_paths'] as $path) {
79+
$classesList = array_merge($classesList, $this->classesScanner->getList($path));
80+
}
81+
5982
$generatorIo = new \Magento\Framework\Code\Generator\Io(
6083
new \Magento\Framework\Filesystem\Driver\File(),
61-
$this->data
84+
$this->data['path_to_store']
6285
);
63-
$generator = new \Magento\Tools\Di\Code\Generator(
64-
$generatorIo,
86+
$generator = $this->generatorFactory->create(
6587
[
66-
Interceptor::ENTITY_TYPE => 'Magento\Tools\Di\Code\Generator\Interceptor',
88+
'ioObject' => $generatorIo,
89+
'generatedEntities' => [
90+
Interceptor::ENTITY_TYPE => 'Magento\Tools\Di\Code\Generator\Interceptor',
91+
]
6792
]
6893
);
69-
$configuration = $this->interceptionConfigurationBuilder->getInterceptionConfiguration(get_declared_classes());
94+
$configuration = $this->interceptionConfigurationBuilder->getInterceptionConfiguration($classesList);
7095
$generator->generateList($configuration);
7196
}
7297
}

dev/tools/Magento/Tools/Di/Code/Generator.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace Magento\Tools\Di\Code;
99

1010
use Magento\Framework\Code\Generator as FrameworkGenerator;
11+
use Magento\Framework\Code\Generator\DefinedClasses;
12+
use Magento\Framework\ObjectManagerInterface;
1113

1214
/**
1315
* Class Generator
@@ -22,6 +24,23 @@ class Generator extends FrameworkGenerator
2224
*/
2325
private $classMethods = [];
2426

27+
/**
28+
* @param ObjectManagerInterface $objectManagerInterface
29+
* @param FrameworkGenerator\Io $ioObject
30+
* @param array $generatedEntities
31+
* @param DefinedClasses $definedClasses
32+
*/
33+
public function __construct(
34+
ObjectManagerInterface $objectManagerInterface,
35+
\Magento\Framework\Code\Generator\Io $ioObject = null,
36+
array $generatedEntities = [],
37+
DefinedClasses $definedClasses = null
38+
) {
39+
parent::__construct($ioObject, $generatedEntities, $definedClasses);
40+
$this->setObjectManager($objectManagerInterface);
41+
}
42+
43+
2544
/**
2645
* Create entity generator
2746
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Tools\Di\Code;
7+
8+
use Magento\Framework\ObjectManagerInterface;
9+
10+
class GeneratorFactory
11+
{
12+
/**
13+
* @var ObjectManagerInterface
14+
*/
15+
private $objectManager;
16+
17+
/**
18+
* @param ObjectManagerInterface $objectManager
19+
*/
20+
public function __construct(ObjectManagerInterface $objectManager)
21+
{
22+
$this->objectManager = $objectManager;
23+
}
24+
25+
/**
26+
* Creates operation
27+
*
28+
* @param array $arguments
29+
* @return Generator
30+
*/
31+
public function create($arguments = [])
32+
{
33+
return $this->objectManager->create('Magento\Tools\Di\Code\Generator', $arguments);
34+
}
35+
}

dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class ClassesScanner implements ClassesScannerInterface
1515
*/
1616
protected $excludePatterns = [];
1717

18+
/**
19+
* @param array $excludePatterns
20+
*/
21+
public function __construct(array $excludePatterns = [])
22+
{
23+
$this->excludePatterns = $excludePatterns;
24+
}
25+
1826
/**
1927
* Adds exclude patterns
2028
*

dev/tools/Magento/Tools/Di/Code/Reader/ClassesScannerInterface.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,4 @@ interface ClassesScannerInterface
2121
* @return array
2222
*/
2323
public function getList($path);
24-
25-
/**
26-
* Adds exclude patterns
27-
*
28-
* @param array $excludePatterns
29-
* @return void
30-
*/
31-
public function addExcludePatterns(array $excludePatterns);
3224
}

dev/tools/Magento/Tools/Di/Code/Reader/Decorator/Area.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ public function __construct(
3838
$this->classesScanner = $classesScanner;
3939
}
4040

41-
/**
42-
* Adds exclude patterns
43-
*
44-
* @param array $excludePatterns
45-
* @return void
46-
*/
47-
public function addExcludePatterns(array $excludePatterns)
48-
{
49-
}
50-
5141
/**
5242
* Retrieves list of classes for given path
5343
*

dev/tools/Magento/Tools/Di/Code/Reader/Decorator/Directory.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,6 @@ public function errorHandler($errorNumber, $msg)
8080
$this->log->add(Log::COMPILATION_ERROR, $this->current, '#' . $errorNumber . ' ' . $msg);
8181
}
8282

83-
/**
84-
* Adds exclude patterns
85-
*
86-
* @param array $excludePatterns
87-
* @return void
88-
*/
89-
public function addExcludePatterns(array $excludePatterns)
90-
{
91-
}
92-
9383
/**
9484
* Retrieves list of classes for given path
9585
*

dev/tools/Magento/Tools/Di/Code/Reader/Decorator/Interceptions.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ public function __construct(
5959
$this->validator->add($contextAggregationValidator);
6060
}
6161

62-
/**
63-
* Adds exclude patterns
64-
*
65-
* @param array $excludePatterns
66-
* @return void
67-
*/
68-
public function addExcludePatterns(array $excludePatterns)
69-
{
70-
}
71-
7262
/**
7363
* Retrieves list of classes for given path
7464
*

dev/tools/Magento/Tools/Di/Test/Unit/App/CompilerTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class CompilerTest extends \PHPUnit_Framework_TestCase
3131
*/
3232
private $responseMock;
3333

34+
/**
35+
* @var array
36+
*/
37+
private $compiledPathsList = [];
38+
39+
/**
40+
* @var array
41+
*/
42+
private $excludedPathsList = [];
43+
3444
protected function setUp()
3545
{
3646
$this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface')
@@ -44,6 +54,7 @@ protected function setUp()
4454
->disableOriginalConstructor()
4555
->setMethods([])
4656
->getMock();
57+
4758
$this->application = new Compiler(
4859
$this->taskManagerMock,
4960
$this->objectManagerMock,
@@ -133,6 +144,14 @@ private function getPreferences()
133144
'instance' => 'Magento\Framework\App\Interception\Cache\CompiledConfig'
134145
]
135146
]
147+
],
148+
'Magento\Tools\Di\Code\Reader\ClassesScanner' => [
149+
'arguments' => [
150+
'excludePatterns' => [
151+
'application' => '#^' . BP . '/app/code/[\\w]+/[\\w]+/Test#',
152+
'framework' => '#^' . BP . '/lib/internal/[\\w]+/[\\w]+/([\\w]+/)?Test#'
153+
]
154+
]
136155
]
137156
];
138157
}
@@ -152,8 +171,14 @@ private function getOptions()
152171
Task\OperationFactory::APPLICATION_CODE_GENERATOR => [
153172
BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation'
154173
],
155-
Task\OperationFactory::INTERCEPTION =>
156-
BP . '/var/generation',
174+
Task\OperationFactory::INTERCEPTION => [
175+
'intercepted_paths' => [
176+
BP . '/' . 'app/code',
177+
BP . '/' . 'lib/internal/Magento/Framework',
178+
BP . '/' . 'var/generation'
179+
],
180+
'path_to_store' => BP . '/var/generation',
181+
],
157182
Task\OperationFactory::AREA_CONFIG_GENERATOR => [
158183
BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation'
159184
],

0 commit comments

Comments
 (0)