- Magento 2 Developer's Guide
- Branko Ajzele
- 641字
- 2021-07-30 09:53:23
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.
- Vue.js 2 and Bootstrap 4 Web Development
- 你不知道的JavaScript(中卷)
- SAP BusinessObjects Dashboards 4.1 Cookbook
- SQL Server 2016數(shù)據(jù)庫(kù)應(yīng)用與開(kāi)發(fā)
- Learning OpenStack Networking(Neutron)
- 深入理解C指針
- Python 3 數(shù)據(jù)分析與機(jī)器學(xué)習(xí)實(shí)戰(zhàn)
- Python青少年趣味編程
- Three.js權(quán)威指南:在網(wǎng)頁(yè)上創(chuàng)建3D圖形和動(dòng)畫(huà)的方法與實(shí)踐(原書(shū)第4版)
- 虛擬現(xiàn)實(shí)建模與編程(SketchUp+OSG開(kāi)發(fā)技術(shù))
- HTML5程序設(shè)計(jì)基礎(chǔ)教程
- Getting Started with Windows Server Security
- 大話(huà)程序員:從入門(mén)到優(yōu)秀全攻略
- Game Programming using Qt 5 Beginner's Guide
- 算法學(xué)習(xí)與應(yīng)用從入門(mén)到精通