BasePackageInfo.php 2.18 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Setup\Model;

use Magento\Framework\FileSystem\Directory\ReadFactory;

/**
 * Information about the Magento base package.
 *
 */
class BasePackageInfo
{
    const MAGENTO_BASE_PACKAGE_COMPOSER_JSON_FILE = 'magento/magento2-base/composer.json';

    const COMPOSER_KEY_EXTRA = 'extra';

    const COMPOSER_KEY_MAP = 'map';

    /**
     * @var \Magento\Framework\Filesystem\Directory\ReadInterface $reader
     */
    private $reader;

    /**
     * Constructor
     *
     * @param ReadFactory $readFactory
     */
    public function __construct(ReadFactory $readFactory)
    {
        $this->reader = $readFactory->create(BP);
    }

    /**
     * Get the list of files and directory paths from magento-base extra/map section.
     *
     * @return string []
     * @throws \Magento\Setup\Exception
     */
    public function getPaths()
    {
        // Locate composer.json for magento2-base module
        $filesPathList = [];
        $vendorDir = require VENDOR_PATH;
        $basePackageComposerFilePath = $vendorDir . '/' . self::MAGENTO_BASE_PACKAGE_COMPOSER_JSON_FILE;
        if (!$this->reader->isExist($basePackageComposerFilePath)) {
            throw new \Magento\Setup\Exception(
                'Could not locate ' . self::MAGENTO_BASE_PACKAGE_COMPOSER_JSON_FILE . ' file.'
            );
        }
        if (!$this->reader->isReadable($basePackageComposerFilePath)) {
            throw new \Magento\Setup\Exception(
                'Could not read ' . self::MAGENTO_BASE_PACKAGE_COMPOSER_JSON_FILE . ' file.'
            );
        }

        // Fill array with list of files and directories from extra/map section
        $composerJsonFileData = json_decode($this->reader->readFile($basePackageComposerFilePath), true);
        if (!isset($composerJsonFileData[self::COMPOSER_KEY_EXTRA][self::COMPOSER_KEY_MAP])) {
            return $filesPathList;
        }
        $extraMappings = $composerJsonFileData[self::COMPOSER_KEY_EXTRA][self::COMPOSER_KEY_MAP];
        foreach ($extraMappings as $map) {
            $filesPathList[] = $map[1];
        }
        return $filesPathList;
    }
}