ModuleResource.php 4.61 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Framework\Module;

use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;

/**
 * Resource Model
 *
 * @deprecated 102.0.0 Declarative schema and data patches replace old functionality and setup_module table
 * So all resources related to this table, will be deprecated since 2.3.0
 */
class ModuleResource extends AbstractDb implements ResourceInterface
{
    /**
     * Database versions
     *
     * @var array
     */
    protected static $schemaVersions = null;

    /**
     * Resource data versions cache array
     *
     * @var array
     */
    protected static $dataVersions = null;

    /**
     * Define main table
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('setup_module', 'module');
    }

    /**
     * Fill static versions arrays.
     * This routine fetches Db and Data versions of at once to optimize sql requests. However, when upgrading, it's
     * possible that 'data' column will be created only after all Db installs are passed. So $neededType contains
     * information on main purpose of calling this routine, and even when 'data' column is absent - it won't require
     * reissuing new sql just to get 'db' version of module.
     *
     * @param string $needType Can be 'db' or 'data'
     * @return $this
     */
    protected function _loadVersion($needType)
    {
        if ($needType == 'db' && self::$schemaVersions === null ||
            $needType == 'data' && self::$dataVersions === null
        ) {
            self::$schemaVersions = [];
            // Db version column always exists
            self::$dataVersions = null;
            // Data version array will be filled only if Data column exist

            if ($this->getConnection()->isTableExists($this->getMainTable())) {
                $select = $this->getConnection()->select()->from($this->getMainTable(), '*');
                $rowset = $this->getConnection()->fetchAll($select);
                foreach ($rowset as $row) {
                    self::$schemaVersions[$row['module']] = $row['schema_version'];
                    if (array_key_exists('data_version', $row)) {
                        if (self::$dataVersions === null) {
                            self::$dataVersions = [];
                        }
                        self::$dataVersions[$row['module']] = $row['data_version'];
                    }
                }
            }
        }

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getDbVersion($moduleName)
    {
        if (!$this->getConnection()) {
            return false;
        }
        $this->_loadVersion('db');
        return self::$schemaVersions[$moduleName] ?? false;
    }

    /**
     * {@inheritdoc}
     */
    public function setDbVersion($moduleName, $version)
    {
        $dbModuleInfo = ['module' => $moduleName, 'schema_version' => $version];

        if ($this->getDbVersion($moduleName)) {
            self::$schemaVersions[$moduleName] = $version;
            return $this->getConnection()->update(
                $this->getMainTable(),
                $dbModuleInfo,
                ['module = ?' => $moduleName]
            );
        } else {
            self::$schemaVersions[$moduleName] = $version;
            return $this->getConnection()->insert($this->getMainTable(), $dbModuleInfo);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getDataVersion($moduleName)
    {
        if (!$this->getConnection()) {
            return false;
        }
        $this->_loadVersion('data');
        return self::$dataVersions[$moduleName] ?? false;
    }

    /**
     * {@inheritdoc}
     */
    public function setDataVersion($moduleName, $version)
    {
        $data = ['module' => $moduleName, 'data_version' => $version];

        if ($this->getDbVersion($moduleName) || $this->getDataVersion($moduleName)) {
            self::$dataVersions[$moduleName] = $version;
            $this->getConnection()->update($this->getMainTable(), $data, ['module = ?' => $moduleName]);
        } else {
            self::$dataVersions[$moduleName] = $version;
            $this->getConnection()->insert($this->getMainTable(), $data);
        }
    }

    /**
     * Flush all class cache
     *
     * @deprecated 102.0.0 This method was added as temporary solution, to increase modularity:
     * Because before new modules appears in resource only on next bootstrap
     * @return void
     */
    public static function flush()
    {
        self::$dataVersions = null;
        self::$schemaVersions = [];
    }
}