官术网_书友最值得收藏!

Composer

Composer is a tool that handles dependency management in PHP. It is not a package manager like Yum and Apt on Linux systems are. Though it deals with libraries (packages), it does so on a per-project level. It does not install anything globally. Composer is a multiplatform tool. Therefore, it runs equally well on Windows, Linux, and OS X.

Installing Composer on a machine is as simple as running the installer in the project directory by using the following command:

curl -sS https://getcomposer.org/installer | php

More information about the installation of Composer can be found on its official website, which can be viewed by visiting https://getcomposer.org.

Composer is used to fetch Magento and the third-party components that it uses. As seen in the previous chapter, the following composer command is what pulls everything into the specified directory:

composer create-project --repository-url=https://repo.magento.com/ magento/project-enterprise-edition <installation directory name>

Once Magento is downloaded and installed, there are numerous composer.json files that can be found in its directory. Assuming <installation directory name> is magento2, if we were to do a quick search executing command such as find magento2/ -name 'composer.json', that would yield over 100 composer.json files. Some of these files are (partially) listed here:

/vendor/magento/module-catalog/composer.json
/vendor/magento/module-cms/composer.json
/vendor/magento/module-contact/composer.json
/vendor/magento/module-customer/composer.json
/vendor/magento/module-sales/composer.json
/...
/vendor/magento/theme-adminhtml-backend/composer.json
/vendor/magento/theme-frontend-blank/composer.json
/vendor/magento/theme-frontend-luma/composer.json
/vendor/magento/language-de_de/composer.json
/vendor/magento/language-en_us/composer.json
/...
/composer.json
/dev/tests/...
/vendor/magento/framework/composer.json

The most relevant file is probably the composer.json file in the root of the magento directory. Its content appears like this:

{
    "name": "magento/project-community-edition",
    "description": "eCommerce Platform for Growth (Community Edition)",
    "type": "project",
    "version": "2.0.0",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "repositories": [
        {
            "type": "composer",
            "url": "https://repo.magento.com/"
        }
    ],
    "require": {
        "magento/product-community-edition": "2.0.0",
        "composer/composer": "@alpha",
        "magento/module-bundle-sample-data": "100.0.*",
        "magento/module-widget-sample-data": "100.0.*",
        "magento/module-theme-sample-data": "100.0.*",
        "magento/module-catalog-sample-data": "100.0.*",
        "magento/module-customer-sample-data": "100.0.*",
        "magento/module-cms-sample-data": "100.0.*",
        "magento/module-catalog-rule-sample-data": "100.0.*",
        "magento/module-sales-rule-sample-data": "100.0.*",
        "magento/module-review-sample-data": "100.0.*",
        "magento/module-tax-sample-data": "100.0.*",
        "magento/module-sales-sample-data": "100.0.*",
        "magento/module-grouped-product-sample-data": "100.0.*",
        "magento/module-downloadable-sample-data": "100.0.*",
        "magento/module-msrp-sample-data": "100.0.*",
        "magento/module-configurable-sample-data": "100.0.*",
        "magento/module-product-links-sample-data": "100.0.*",
        "magento/module-wishlist-sample-data": "100.0.*",
        "magento/module-swatches-sample-data": "100.0.*",
        "magento/sample-data-media": "100.0.*",
        "magento/module-offline-shipping-sample-data": "100.0.*"
    },
    "require-dev": {
        "phpunit/phpunit": "4.1.0",
        "squizlabs/php_codesniffer": "1.5.3",
        "phpmd/phpmd": "@stable",
        "pdepend/pdepend": "2.0.6",
        "sjparkinson/static-review": "~4.1",
        "fabpot/php-cs-fixer": "~1.2",
        "lusitanian/oauth": "~0.3 <=0.7.0"
    },
    "config": {
        "use-include-path": true
    },
    "autoload": {
        "psr-4": {
            "Magento\\Framework\\": "lib/internal/Magento/Framework/",
            "Magento\\Setup\\": "setup/src/Magento/Setup/",
            "Magento\\": "app/code/Magento/"
        },
        "psr-0": {
            "": "app/code/"
        },
        "files": [
            "app/etc/NonComposerComponentRegistration.php"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Magento\\Sniffs\\": "dev/tests/static/framework/Magento/Sniffs/",
            "Magento\\Tools\\": "dev/tools/Magento/Tools/",
            "Magento\\Tools\\Sanity\\": "dev/build/publication/sanity/ Magento/Tools/Sanity/",
            "Magento\\TestFramework\\Inspection\\": "dev/tests/static/framework/Magento/ TestFramework/Inspection/",
            "Magento\\TestFramework\\Utility\\": "dev/tests/static/framework/Magento/ TestFramework/Utility/"
        }
    },
    "minimum-stability": "alpha",
    "prefer-stable": true,
    "extra": {
        "magento-force": "override"
    }
}

Composer's JSON file follows a certain schema. You will find a detailed documentation of this schema at https://getcomposer.org/doc/04-schema.md. Applying to the schema ensures validity of the composer file. We can see that all the listed keys such as name, description, require, config, and so on, are defined by the schema.

Let's take a look at the individual module's composer.json file. One of the simpler modules with the least amount of dependencies is the Contact module with its vendor/magento/module-contact/composer.json content, which looks like this:

{
    "name": "magento/module-contact",
    "description": "N/A",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/module-config": "100.0.*",
        "magento/module-store": "100.0.*",
        "magento/module-backend": "100.0.*",
        "magento/module-customer": "100.0.*",
        "magento/module-cms": "100.0.*",
        "magento/framework": "100.0.*"
    },
    "type": "magento2-module",
    "version": "100.0.2",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Magento\\Contact\\": ""
        }
    }
}

You will see that the modules define dependencies on the PHP version and other modules. Furthermore, you will see the use of PSR-4 for autoloading and the direct loading of the registration.php file.

Next, let's take a look at the contents of vendor/magento/language-en_us/composer.json from the en_us language module:

{
    "name": "magento/language-en_us",
    "description": "English (United States) language",
    "version": "100.0.2",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "require": {
        "magento/framework": "100.0.*"
    },
    "type": "magento2-language",
    "autoload": {
        "files": [
            "registration.php"
        ]
    }
}

Finally, let's take a look at the contents of vendor/magento/theme-frontend-luma/composer.json from the luma theme:

{
    "name": "magento/theme-frontend-luma",
    "description": "N/A",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/theme-frontend-blank": "100.0.*",
        "magento/framework": "100.0.*"
    },
    "type": "magento2-theme",
    "version": "100.0.2",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [
            "registration.php"
        ]
    }
}

As mentioned previously, there are a lot more composer files scattered around Magento.

主站蜘蛛池模板: 东城区| 阳泉市| 泗洪县| 永吉县| 来安县| 濮阳县| 平凉市| 化隆| 刚察县| 措勤县| 错那县| 壤塘县| 乐至县| 霍邱县| 黑河市| 呼图壁县| 武安市| 本溪| 合阳县| 苍溪县| 农安县| 泸州市| 简阳市| 塔河县| 佛坪县| 乌拉特前旗| 丰镇市| 孝昌县| 涞水县| 弋阳县| 株洲市| 蒲城县| 广宗县| 象山县| 合川市| 平利县| 大厂| 汽车| 林西县| 全南县| 灌阳县|