- 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.
- ThinkPHP 5實戰
- 摩登創客:與智能手機和平板電腦共舞
- Mastering Scientific Computing with R
- Blender 3D Incredible Machines
- Building Minecraft Server Modifications
- Learn React with TypeScript 3
- Modular Programming in Java 9
- 精通MATLAB(第3版)
- 快人一步:系統性能提高之道
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- Learning Nessus for Penetration Testing
- C陷阱與缺陷
- 并行編程方法與優化實踐
- Application Development with Parse using iOS SDK
- 深度實踐KVM:核心技術、管理運維、性能優化與項目實施