Skip to content

Commit a90311d

Browse files
author
Igor Melnikov
committed
MAGETWO-67871: Requesting autogenerated classes that are not in constructor cause fatal errors in production mode
- move dependencies on autogenerated classes to constructor - add static test
1 parent f63b0a1 commit a90311d

File tree

71 files changed

+1946
-1178
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1946
-1178
lines changed

app/code/Magento/Bundle/Model/Product/Price.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Bundle\Model\Product;
87

98
use Magento\Customer\Api\GroupManagementInterface;
109
use Magento\Framework\Pricing\PriceCurrencyInterface;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Catalog\Api\Data\ProductTierPriceExtensionFactory;
1112

1213
/**
13-
* Bundle Price Model
14-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1514
* @api
15+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1616
*/
1717
class Price extends \Magento\Catalog\Model\Product\Type\Price
1818
{
@@ -48,7 +48,7 @@ class Price extends \Magento\Catalog\Model\Product\Type\Price
4848
private $serializer;
4949

5050
/**
51-
* Price constructor.
51+
* Constructor
5252
*
5353
* @param \Magento\CatalogRule\Model\ResourceModel\RuleFactory $ruleFactory
5454
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -61,6 +61,7 @@ class Price extends \Magento\Catalog\Model\Product\Type\Price
6161
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
6262
* @param \Magento\Catalog\Helper\Data $catalogData
6363
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
64+
* @param ProductTierPriceExtensionFactory|null $tierPriceExtensionFactory
6465
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6566
*/
6667
public function __construct(
@@ -74,10 +75,11 @@ public function __construct(
7475
\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory,
7576
\Magento\Framework\App\Config\ScopeConfigInterface $config,
7677
\Magento\Catalog\Helper\Data $catalogData,
77-
\Magento\Framework\Serialize\Serializer\Json $serializer = null
78+
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
79+
ProductTierPriceExtensionFactory $tierPriceExtensionFactory = null
7880
) {
7981
$this->_catalogData = $catalogData;
80-
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
82+
$this->serializer = $serializer ?: ObjectManager::getInstance()
8183
->get(\Magento\Framework\Serialize\Serializer\Json::class);
8284
parent::__construct(
8385
$ruleFactory,
@@ -88,7 +90,8 @@ public function __construct(
8890
$priceCurrency,
8991
$groupManagement,
9092
$tierPriceFactory,
91-
$config
93+
$config,
94+
$tierPriceExtensionFactory
9295
);
9396
}
9497

app/code/Magento/Bundle/Test/Unit/Model/Product/PriceTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
namespace Magento\Bundle\Test\Unit\Model\Product;
77

88
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
9+
use Magento\Catalog\Api\Data\ProductTierPriceExtensionFactory;
910

1011
/**
11-
* Test for Model ProductPrice.
12-
*
1312
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1413
*/
1514
class PriceTest extends \PHPUnit_Framework_TestCase
@@ -114,7 +113,10 @@ function ($value) {
114113
return json_decode($value, true);
115114
}
116115
);
117-
116+
$tierPriceExtensionFactoryMock = $this->getMockBuilder(ProductTierPriceExtensionFactory::class)
117+
->setMethods(['create'])
118+
->disableOriginalConstructor()
119+
->getMock();
118120
$objectManagerHelper = new ObjectManagerHelper($this);
119121
$this->model = $objectManagerHelper->getObject(
120122
\Magento\Bundle\Model\Product\Price::class,
@@ -129,7 +131,8 @@ function ($value) {
129131
'tierPriceFactory' => $tpFactory,
130132
'config' => $scopeConfig,
131133
'catalogData' => $this->catalogHelperMock,
132-
'serializer' => $this->serializer
134+
'serializer' => $this->serializer,
135+
'tierPriceExtensionFactory' => $tierPriceExtensionFactoryMock
133136
]
134137
);
135138
}

app/code/Magento/Catalog/Controller/Adminhtml/Product/AddAttributeToTemplate.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
@@ -73,18 +72,24 @@ class AddAttributeToTemplate extends \Magento\Catalog\Controller\Adminhtml\Produ
7372
protected $extensionAttributesFactory;
7473

7574
/**
75+
* Constructor
76+
*
7677
* @param \Magento\Backend\App\Action\Context $context
7778
* @param Builder $productBuilder
7879
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
80+
* @param \Magento\Eav\Api\Data\AttributeGroupInterfaceFactory|null $attributeGroupFactory
7981
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8082
*/
8183
public function __construct(
8284
\Magento\Backend\App\Action\Context $context,
8385
\Magento\Catalog\Controller\Adminhtml\Product\Builder $productBuilder,
84-
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
86+
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
87+
\Magento\Eav\Api\Data\AttributeGroupInterfaceFactory $attributeGroupFactory = null
8588
) {
8689
parent::__construct($context, $productBuilder);
8790
$this->resultJsonFactory = $resultJsonFactory;
91+
$this->attributeGroupFactory = $attributeGroupFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
92+
->get(\Magento\Eav\Api\Data\AttributeGroupInterfaceFactory::class);
8893
}
8994

9095
/**
@@ -125,7 +130,7 @@ public function execute()
125130
$attributeGroup = reset($attributeGroupItems);
126131
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
127132
/** @var AttributeGroupInterface $attributeGroup */
128-
$attributeGroup = $this->getAttributeGroupFactory()->create();
133+
$attributeGroup = $this->attributeGroupFactory->create();
129134
}
130135

131136
$extensionAttributes = $attributeGroup->getExtensionAttributes()
@@ -221,18 +226,6 @@ private function getAttributeGroupRepository()
221226
return $this->attributeGroupRepository;
222227
}
223228

224-
/**
225-
* @return AttributeGroupInterfaceFactory
226-
*/
227-
private function getAttributeGroupFactory()
228-
{
229-
if (null === $this->attributeGroupFactory) {
230-
$this->attributeGroupFactory = \Magento\Framework\App\ObjectManager::getInstance()
231-
->get(\Magento\Eav\Api\Data\AttributeGroupInterfaceFactory::class);
232-
}
233-
return $this->attributeGroupFactory;
234-
}
235-
236229
/**
237230
* @return SearchCriteriaBuilder
238231
*/

app/code/Magento/Catalog/Controller/Adminhtml/Product/Builder.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
@@ -41,21 +40,27 @@ class Builder
4140
protected $storeFactory;
4241

4342
/**
43+
* Constructor
44+
*
4445
* @param ProductFactory $productFactory
4546
* @param Logger $logger
4647
* @param Registry $registry
4748
* @param WysiwygModel\Config $wysiwygConfig
49+
* @param StoreFactory|null $storeFactory
4850
*/
4951
public function __construct(
5052
ProductFactory $productFactory,
5153
Logger $logger,
5254
Registry $registry,
53-
WysiwygModel\Config $wysiwygConfig
55+
WysiwygModel\Config $wysiwygConfig,
56+
StoreFactory $storeFactory = null
5457
) {
5558
$this->productFactory = $productFactory;
5659
$this->logger = $logger;
5760
$this->registry = $registry;
5861
$this->wysiwygConfig = $wysiwygConfig;
62+
$this->storeFactory = $storeFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
63+
->get(\Magento\Store\Model\StoreFactory::class);
5964
}
6065

6166
/**
@@ -70,7 +75,7 @@ public function build(RequestInterface $request)
7075
/** @var $product \Magento\Catalog\Model\Product */
7176
$product = $this->productFactory->create();
7277
$product->setStoreId($request->getParam('store', 0));
73-
$store = $this->getStoreFactory()->create();
78+
$store = $this->storeFactory->create();
7479
$store->load($request->getParam('store', 0));
7580

7681
$typeId = $request->getParam('type');
@@ -99,16 +104,4 @@ public function build(RequestInterface $request)
99104
$this->wysiwygConfig->setStoreId($request->getParam('store'));
100105
return $product;
101106
}
102-
103-
/**
104-
* @return StoreFactory
105-
*/
106-
private function getStoreFactory()
107-
{
108-
if (null === $this->storeFactory) {
109-
$this->storeFactory = \Magento\Framework\App\ObjectManager::getInstance()
110-
->get(\Magento\Store\Model\StoreFactory::class);
111-
}
112-
return $this->storeFactory;
113-
}
114107
}

app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Magento\Framework\App\ObjectManager;
1414

1515
/**
16-
* Class Helper
1716
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1817
*/
1918
class Helper
@@ -40,7 +39,6 @@ class Helper
4039

4140
/**
4241
* @var \Magento\Framework\Stdlib\DateTime\Filter\Date
43-
*
4442
* @deprecated
4543
*/
4644
protected $dateFilter;
@@ -76,28 +74,41 @@ class Helper
7674
private $dateTimeFilter;
7775

7876
/**
79-
* Helper constructor.
77+
* Constructor
78+
*
8079
* @param \Magento\Framework\App\RequestInterface $request
8180
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
8281
* @param StockDataFilter $stockFilter
8382
* @param ProductLinks $productLinks
8483
* @param \Magento\Backend\Helper\Js $jsHelper
8584
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
85+
* @param \Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory|null $customOptionFactory
86+
* @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory|null $productLinkFactory
87+
* @param \Magento\Catalog\Api\ProductRepositoryInterface|null $productRepository
8688
*/
8789
public function __construct(
8890
\Magento\Framework\App\RequestInterface $request,
8991
\Magento\Store\Model\StoreManagerInterface $storeManager,
9092
StockDataFilter $stockFilter,
9193
\Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $productLinks,
9294
\Magento\Backend\Helper\Js $jsHelper,
93-
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
95+
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
96+
\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory $customOptionFactory = null,
97+
\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory = null,
98+
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository = null
9499
) {
95100
$this->request = $request;
96101
$this->storeManager = $storeManager;
97102
$this->stockFilter = $stockFilter;
98103
$this->productLinks = $productLinks;
99104
$this->jsHelper = $jsHelper;
100105
$this->dateFilter = $dateFilter;
106+
$this->customOptionFactory = $customOptionFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
107+
->get(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
108+
$this->productLinkFactory = $productLinkFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
109+
->get(\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory::class);
110+
$this->productRepository = $productRepository ?: \Magento\Framework\App\ObjectManager::getInstance()
111+
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
101112
}
102113

103114
/**
@@ -202,7 +213,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
202213
return empty($valueData['is_delete']);
203214
});
204215
}
205-
$customOption = $this->getCustomOptionFactory()->create(['data' => $customOptionData]);
216+
$customOption = $this->customOptionFactory->create(['data' => $customOptionData]);
206217
$customOption->setProductSku($product->getSku());
207218
$customOptions[] = $customOption;
208219
}
@@ -257,8 +268,8 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)
257268
continue;
258269
}
259270

260-
$linkProduct = $this->getProductRepository()->getById($linkData['id']);
261-
$link = $this->getProductLinkFactory()->create();
271+
$linkProduct = $this->productRepository->getById($linkData['id']);
272+
$link = $this->productLinkFactory->create();
262273
$link->setSku($product->getSku())
263274
->setLinkedProductSku($linkProduct->getSku())
264275
->setLinkType($linkType)
@@ -273,10 +284,10 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)
273284

274285
/**
275286
* Internal normalization
276-
* TODO: Remove this method
277287
*
278288
* @param array $productData
279289
* @return array
290+
* @todo Remove this method
280291
*/
281292
protected function normalize(array $productData)
282293
{
@@ -357,44 +368,8 @@ private function overwriteValue($optionId, $option, $overwriteOptions)
357368
}
358369

359370
/**
360-
* @return CustomOptionFactory
361-
*/
362-
private function getCustomOptionFactory()
363-
{
364-
if (null === $this->customOptionFactory) {
365-
$this->customOptionFactory = \Magento\Framework\App\ObjectManager::getInstance()
366-
->get(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
367-
}
368-
return $this->customOptionFactory;
369-
}
370-
371-
/**
372-
* @return ProductLinkFactory
373-
*/
374-
private function getProductLinkFactory()
375-
{
376-
if (null === $this->productLinkFactory) {
377-
$this->productLinkFactory = \Magento\Framework\App\ObjectManager::getInstance()
378-
->get(\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory::class);
379-
}
380-
return $this->productLinkFactory;
381-
}
382-
383-
/**
384-
* @return ProductRepository
385-
*/
386-
private function getProductRepository()
387-
{
388-
if (null === $this->productRepository) {
389-
$this->productRepository = \Magento\Framework\App\ObjectManager::getInstance()
390-
->get(\Magento\Catalog\Api\ProductRepositoryInterface\Proxy::class);
391-
}
392-
return $this->productRepository;
393-
}
394-
395-
/**
396-
* @deprecated
397371
* @return LinkResolver
372+
* @deprecated
398373
*/
399374
private function getLinkResolver()
400375
{
@@ -406,7 +381,6 @@ private function getLinkResolver()
406381

407382
/**
408383
* @return \Magento\Framework\Stdlib\DateTime\Filter\DateTime
409-
*
410384
* @deprecated
411385
*/
412386
private function getDateTimeFilter()

0 commit comments

Comments
 (0)