In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the structure of Composer". In daily operation, I believe that many people have doubts about the structure of Composer. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is the structure of Composer?" Next, please follow the editor to study!
Composer is a very popular PHP package dependency management tool, which has replaced the PEAR package manager. It is necessary for PHP developers to master Composer.
Composer is very simple for users, with a simple command to download the required code package to the vendor directory, and then developers can introduce the package and use it.
The key is that your project definition of composer.json, you can define the project needs to rely on packages (there may be multiple), while dependent packages may rely on other packages (this is the benefit of components), these do not bother you, Composer will automatically download everything you need, it all depends on the definition of composer.json.
Composer is very transparent to users, but the idea behind it still needs to be understood, and its birth is not accidental. Thanks to the rapid development of Github, the PHP language is becoming more and more modern.
To understand Composer, take a rough look at its structure:
The structure of Composer
Composer command line tool:
This understanding is relatively simple, through the user-defined Composer.json to download the code you need, if you simply use Composer, then you can master some specific commands
Autoloading code loader:
With Composer, developers can use it in a variety of ways, the key of which lies in the concept of PHP namespaces and the development of the PSR-4 standard. Composer has only developed an automatic code loader based on these two.
Github:
With Github,PHP developers can host open source code on this, and Composer's development stems from the fact that Github,Composer essentially downloads code on Github locally.
Packagist:
For users, Composer command line tools are used, so how do command line tools know how many packages can be used by users? this mainly depends on that Packagist,Packagist is a main package information repository of Composer. Package developers host specific code on Github and submit package information to Packagist, so that users can use it through Composer.
Composer queries the Packagist,Packagist according to the locally defined composer.json information and parses the Composer.json/Package.json information, and finally corresponds to the github repository. When Composer finally downloads the code, it also depends on the Composer.json on the Github repository. Here three types of composer.json are involved, and the meaning is different.
Composer.json:
This is the core of Composer and the rule of Composer. Three types of Composer.json are also mentioned above. We must pay attention to the distinction when using them. I always make a mess when I am a beginner.
Composer command line tool
Composer init
Users can create composer.json under their own projects to define dependent packages for your project, or they can create composer.json interactively through composer init.
Composer install
Should be the most commonly used command, composer will install the package according to the local composer.json, put the downloaded package into the vendor directory of the project, and put the installation package version information into composer.lock in order to lock the version.
In fact, in install, if you find that the version of composer.lock is the same as the version of the code in the current vendor directory, Composer will do nothing. The purpose of composer.lock is to make you feel at ease working in the current version without getting the * version of the package.
Composer update
So how do I update composer.lock to get the * version of the package? The * version of the package can be updated with this command.
Composer config
This command is also recommended to understand, the global configuration is saved in COMPOSER_HOME/config.json, non-global configuration information is stored in the project directory.
Composer config-list-gcomposer config-g notify-on-install falsecomposer global config bin-dir-absolute
Composer create-project
This command is not commonly used, but I think it is very important to use the ordinary install command to download all dependent packages of the project to the vendor directory of the project. Through this command, you put all the code and its dependent packages in a directory, which is equivalent to executing a git clone command, which may be used by the package developer to fix the bug.
Composer global
This is a global installation command that allows you to execute Composer commands in the COMPOSER_HOME directory, such as install,update. Of course, your COMPOSER_HOME should be in the $PATH environment.
For example, to execute composer global require fabpot/php-cs-fixer, now the php-cs-fixer command line can be run globally. If you want to update it later, just run composer global update.
Composer dump-autoload
When you modify the composer.json file under the project, it is not necessary to run the composer update command to update, sometimes you can use this command to update the loader, for example, you want to reference a local custom package (not from packagist), which will be explained in practice later.
Composer require
If you create a composer.json file manually or interactively, you can use this command directly to install the package
Composer require cerdic/css-tidy:1.5.2composer require "ywdblog/phpcomposer:dev-master"
-prefer-source and-prefer-dist parameters
-prefer-dist: for stable packages, this parameter is used by default in general Composer installation, which can also speed up installation. For example, it is possible to install the corresponding package directly from packagist without actually downloading the package on Github.
-prefer-source: if this parameter is used, it will be installed directly from Github. After installing the package, the vendor directory also contains .git information.
Composer require "ywdblog/phpcomposer:dev-master"-prefer-source # contains .git information in the vendor/ywdblog/phpcomposer directory
How to add agents to Composer
Composer download is very slow in China, and can be accelerated in two ways.
Composer config repo.packagist composer "https://packagist.phpcomposer.com"
Edit composer.json
"repositories": {"packagist": {"type": "composer", "url": "https://packagist.phpcomposer.com"}} Autoloading code loader
Composer itself integrates an autoloader and supports PSR-4,PSR-0,classmap,files autoloading.
Here is an example to illustrate how to reference classmap,files through Composer, local code that conforms to the PSR-4 standard.
Edit composer.json
"autoload": {"classmap": ["othsrc/", "classsrc.php"], "files": ["othsrc/filesrc.php"], "psr-4": {"Foo\ Bar\": "src"}}
Composer dump-autoload
By doing the above, PSR-4 is equivalent to registering a PSR-4 autoloader (from the FooBar namespace)
If you don't want to use Composer's autoloader, you can directly include the vendor/composer/autoload_*.php file and configure your own loader.
Specific examples are hosted on github for reference.
Repositories
About Repositories, it is not necessary to understand it, but if you master it, you can better understand Composer. For Repositories, its Chinese and English documents are well explained, and here are some excerpts.
Basic concept
Package:
Composer is a dependency management tool that installs resource bundles and descriptions of packages locally (such as package names and corresponding versions). The more important metadata description is that dist and source,dist point to an archive that packages data from a version of a resource package. source points to a source in development, which is usually a source code repository (such as git)
Resource pool:
A repository is the source of a package. It is a list of packages/versions.
Composer will look at all the repositories you define to find the resource bundles that the project needs.
Packagist.org has been registered with Composer by default (or understood as Packagist.org is the default repository type for Composer repositories)
Composer resource pool type
There are four types of Composer repositories. The default is the composer type, which is the resource type used by packagist.org.
It uses a single packages.json file that contains all the resource bundle metadata. When you publish the package to pckagist.org, the default system will create a packages.json, but I can't find the corresponding file for my package.
VCS resource pool type
If you want to build a private Composer private repository type, you can use this type. Here is an example, for example, if you define the composer.json in your own project as follows, you can use the code on the corresponding Github.
{"repositories": [{"type": "vcs", "url": "https://github.com/ywdblog/phpcomposer"}]," require ": {" ywdblog/phpcomposer ":" dev-master "}}
When running composer update, Comoser actually downloads packages from Github rather than from pckagist.org.
In addition, if you need to use Package repository type or PEAR repository type, you can refer to the official documentation. Generally, you can define name and version attributes in composer.json.
Composer.json
Composer.json has also been mentioned many times in this article. For example, if you want to use a third-party package, you need to define the local composer.json,Composer to install the third-party package, and you will also find composer.json in the third-party package directory. Then both of them are called composer.json. What's the difference? It is very important to understand this.
If you define a composer.json under your project, the package is called the ROOT package, and the composer.json defines the conditions your project needs (for example, your project may rely on a third-party package).
There are some attributes in composer.json that can only be used by ROOT packages, such as the config attribute that only works in ROOT packages.
Whether a resource package is a ROOT package depends on its context. For example, if you git clone ywdblog/phpcomposer, the local phpcomposer directory is the ROOT package. If you composer require ywdblog/phpcomposer in the local phpcomposer directory, then your project phpcomposer is the ROOT package.
To learn about composer-schema.json, you can refer to this website. Laravel is a mature framework, and its definition of composer.json is very classic.
About the version of the package
When users configure composer.json locally, they can specify a specific version of the package they need. Composer supports downloading packages under Tag or branches from the Github repository.
For Tag on Github, Packagist creates a version of the corresponding package, which conforms to the X.Y.ZMagneur vX.Y.Z-package type, which means that although there is only one version of the package on Github, Composer supports various forms of referencing, such as:
Composer require monolog/monolog 1.0.0-RC1 composer require monolog/monolog v1.0.0-RC1 composer require monolog/monolog 1.0.*composer require monolog/monolog ~ 1.10
For branches on Github, Packagist will create a version of the corresponding package. If the branch name looks like a version, it will create a package version number of {branch name}-dev. If the branch name does not look like a version number, it will create a version number in the form of dev- {branch name}
Composer require monolog/monolog master-devcomposer require monolog/monolog master.x-dev at this point, the study of "what is the structure of Composer" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.