123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Setup\Patch;
- use Magento\Framework\App\ResourceConnection;
- use Magento\Framework\Module\ModuleList;
- use Magento\Framework\ObjectManagerInterface;
- use Magento\Framework\Phrase;
- use Magento\Framework\Setup\Exception;
- use Magento\Framework\Setup\ModuleDataSetupInterface;
- /**
- * Apply patches per specific module
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class PatchApplier
- {
- /**
- * Flag means, that we need to read schema patches
- */
- const SCHEMA_PATCH = 'schema';
- /**
- * Flag means, that we need to read data patches
- */
- const DATA_PATCH = 'data';
- /**
- * @var PatchRegistryFactory
- */
- private $patchRegistryFactory;
- /**
- * @var PatchReader
- */
- private $dataPatchReader;
- /**
- * @var PatchReader
- */
- private $schemaPatchReader;
- /**
- * @var ResourceConnection
- */
- private $resourceConnection;
- /**
- * @var PatchHistory
- */
- private $patchHistory;
- /**
- * @var PatchFactory
- */
- private $patchFactory;
- /**
- * @var \Magento\Framework\Setup\SetupInterface
- */
- private $schemaSetup;
- /**
- * @var ModuleDataSetupInterface
- */
- private $moduleDataSetup;
- /**
- * @var ObjectManagerInterface
- */
- private $objectManager;
- /**
- * @var PatchBackwardCompatability
- */
- private $patchBackwardCompatability;
- /**
- * @var ModuleList
- */
- private $moduleList;
- /**
- * PatchApplier constructor.
- * @param PatchReader $dataPatchReader
- * @param PatchReader $schemaPatchReader
- * @param PatchRegistryFactory $patchRegistryFactory
- * @param ResourceConnection $resourceConnection
- * @param PatchBackwardCompatability $patchBackwardCompatability
- * @param PatchHistory $patchHistory
- * @param PatchFactory $patchFactory
- * @param ObjectManagerInterface $objectManager
- * @param \Magento\Framework\Setup\SchemaSetupInterface $schemaSetup
- * @param ModuleDataSetupInterface $moduleDataSetup
- * @param ModuleList $moduleList
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- * @SuppressWarnings(Magento.TypeDuplication)
- */
- public function __construct(
- PatchReader $dataPatchReader,
- PatchReader $schemaPatchReader,
- PatchRegistryFactory $patchRegistryFactory,
- ResourceConnection $resourceConnection,
- PatchBackwardCompatability $patchBackwardCompatability,
- PatchHistory $patchHistory,
- PatchFactory $patchFactory,
- ObjectManagerInterface $objectManager,
- \Magento\Framework\Setup\SchemaSetupInterface $schemaSetup,
- \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
- ModuleList $moduleList
- ) {
- $this->patchRegistryFactory = $patchRegistryFactory;
- $this->dataPatchReader = $dataPatchReader;
- $this->schemaPatchReader = $schemaPatchReader;
- $this->resourceConnection = $resourceConnection;
- $this->patchHistory = $patchHistory;
- $this->patchFactory = $patchFactory;
- $this->schemaSetup = $schemaSetup;
- $this->moduleDataSetup = $moduleDataSetup;
- $this->objectManager = $objectManager;
- $this->patchBackwardCompatability = $patchBackwardCompatability;
- $this->moduleList = $moduleList;
- }
- /**
- * Apply all patches for one module
- *
- * @param null | string $moduleName
- * @throws Exception
- */
- public function applyDataPatch($moduleName = null)
- {
- $registry = $this->prepareRegistry($moduleName, self::DATA_PATCH);
- foreach ($registry as $dataPatch) {
- /**
- * Due to backward compatabilities reasons some patches should be skipped
- */
- if ($this->patchBackwardCompatability->isSkipableByDataSetupVersion($dataPatch, $moduleName)) {
- $this->patchHistory->fixPatch($dataPatch);
- continue;
- }
- $dataPatch = $this->objectManager->create(
- '\\' . $dataPatch,
- ['moduleDataSetup' => $this->moduleDataSetup]
- );
- if (!$dataPatch instanceof DataPatchInterface) {
- throw new Exception(
- new Phrase("Patch %1 should implement DataPatchInterface", [get_class($dataPatch)])
- );
- }
- if ($dataPatch instanceof NonTransactionableInterface) {
- $dataPatch->apply();
- $this->patchHistory->fixPatch(get_class($dataPatch));
- } else {
- try {
- $this->moduleDataSetup->getConnection()->beginTransaction();
- $dataPatch->apply();
- $this->patchHistory->fixPatch(get_class($dataPatch));
- $this->moduleDataSetup->getConnection()->commit();
- } catch (\Exception $e) {
- $this->moduleDataSetup->getConnection()->rollBack();
- throw new Exception(new Phrase($e->getMessage()));
- } finally {
- unset($dataPatch);
- }
- }
- }
- }
- /**
- * Register all patches in registry in order to manipulate chains and dependencies of patches
- * of patches
- *
- * @param string $moduleName
- * @param string $patchType
- * @return PatchRegistry
- */
- private function prepareRegistry($moduleName, $patchType)
- {
- $reader = $patchType === self::DATA_PATCH ? $this->dataPatchReader : $this->schemaPatchReader;
- $registry = $this->patchRegistryFactory->create();
- //Prepare modules to read
- if ($moduleName === null) {
- $patchNames = [];
- foreach ($this->moduleList->getNames() as $moduleName) {
- $patchNames += $reader->read($moduleName);
- }
- } else {
- $patchNames = $reader->read($moduleName);
- }
- foreach ($patchNames as $patchName) {
- $registry->registerPatch($patchName);
- }
- return $registry;
- }
- /**
- * Apply all patches for one module
- *
- * Please note: that schema patches are not revertable
- *
- * @param null | string $moduleName
- * @throws Exception
- */
- public function applySchemaPatch($moduleName = null)
- {
- $registry = $this->prepareRegistry($moduleName, self::SCHEMA_PATCH);
- foreach ($registry as $schemaPatch) {
- try {
- /**
- * Skip patches that were applied in old style
- */
- if ($this->patchBackwardCompatability->isSkipableBySchemaSetupVersion($schemaPatch, $moduleName)) {
- $this->patchHistory->fixPatch($schemaPatch);
- continue;
- }
- /**
- * @var SchemaPatchInterface $schemaPatch
- */
- $schemaPatch = $this->patchFactory->create($schemaPatch, ['schemaSetup' => $this->schemaSetup]);
- $schemaPatch->apply();
- $this->patchHistory->fixPatch(get_class($schemaPatch));
- } catch (\Exception $e) {
- throw new Exception(
- new Phrase(
- 'Unable to apply patch %1 for module %2. Original exception message: %3',
- [
- get_class($schemaPatch),
- $moduleName,
- $e->getMessage()
- ]
- )
- );
- } finally {
- unset($schemaPatch);
- }
- }
- }
- /**
- * Revert data patches for specific module
- *
- * @param null | string $moduleName
- * @throws Exception
- */
- public function revertDataPatches($moduleName = null)
- {
- $registry = $this->prepareRegistry($moduleName, self::DATA_PATCH);
- $adapter = $this->moduleDataSetup->getConnection();
- foreach ($registry->getReverseIterator() as $dataPatch) {
- $dataPatch = $this->objectManager->create(
- '\\' . $dataPatch,
- ['moduleDataSetup' => $this->moduleDataSetup]
- );
- if ($dataPatch instanceof PatchRevertableInterface) {
- try {
- $adapter->beginTransaction();
- /** @var PatchRevertableInterface|DataPatchInterface $dataPatch */
- $dataPatch->revert();
- $this->patchHistory->revertPatchFromHistory(get_class($dataPatch));
- $adapter->commit();
- } catch (\Exception $e) {
- $adapter->rollBack();
- throw new Exception(new Phrase($e->getMessage()));
- } finally {
- unset($dataPatch);
- }
- }
- }
- }
- }
|