PatchReader.php 2.87 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Framework\Setup\Patch;

use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Config\ReaderInterface;
use Magento\Framework\Filesystem\Glob;
use Magento\Framework\Module\Dir;

/**
 * Allows to read all patches through the whole system
 */
class PatchReader
{
    /**
     * Folder name, where patches are
     */
    const SETUP_PATCH_FOLDER = 'Patch';

    /**
     * @var ComponentRegistrar
     */
    private $componentRegistrar;

    /**
     * @var string
     */
    private $type;

    /**
     * @param ComponentRegistrar $componentRegistrar
     * @param string $type
     */
    public function __construct(
        ComponentRegistrar $componentRegistrar,
        $type
    ) {
        $this->componentRegistrar = $componentRegistrar;
        $this->type = $type;
    }

    /**
     * Retrieve absolute path to modules patch folder
     *
     * @param string $modulePath
     * @return string
     */
    private function getPatchFolder($modulePath)
    {
        return $modulePath . DIRECTORY_SEPARATOR . Dir::MODULE_SETUP_DIR .
            DIRECTORY_SEPARATOR . self::SETUP_PATCH_FOLDER;
    }

    /**
     * Retrieve module name prepared to usage in namespaces
     *
     * @param string $moduleName
     * @return string
     */
    private function getModuleNameForNamespace($moduleName)
    {
        return str_replace('_', '\\', $moduleName);
    }

    /**
     * Depends on type we want to handle schema and data patches in different folders
     *
     * @return string
     */
    private function getTypeFolder()
    {
        return ucfirst($this->type);
    }

    /**
     * Create array of class patch names from module name
     *
     * @param string $moduleName
     * @param string $modulePath
     * @return array
     */
    private function getPatchClassesPerModule($moduleName, $modulePath)
    {
        $patchClasses = [];
        $patchesPath = $this->getPatchFolder($modulePath);
        $specificPatchPath = $patchesPath . DIRECTORY_SEPARATOR . $this->getTypeFolder();
        $patchesMask = $specificPatchPath . DIRECTORY_SEPARATOR . '*.php';

        foreach (Glob::glob($patchesMask) as $patchPath) {
            $moduleName = $this->getModuleNameForNamespace($moduleName);
            $patchClasses[] = $moduleName . '\\Setup\\' .
                self::SETUP_PATCH_FOLDER . '\\' .
                $this->getTypeFolder() . '\\' .
                basename($patchPath, '.php');
        }

        return $patchClasses;
    }

    /**
     * @param string $moduleName
     * @return array
     */
    public function read($moduleName)
    {
        $modulePath = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
        $patches = $this->getPatchClassesPerModule($moduleName, $modulePath);
        return $patches;
    }
}