Preface
For small development partners, code packages are a common part of the development process.For example, PHP has composer, Java has maven, front-end has npm, yarn, Mac has brew, Linux has yum.Using these packages, we can easily manage the external code components introduced by the code, help us to improve development efficiency, and help us to manage the code more elegantly.The main point below is to share a composer code package that you customize personally. The code is just a demonstration example and has no practical effect.At the same time, it will be updated constantly, so you can pay attention to it. Original Address
Specific implementation
Create a remote git code repository.To create a code repository, I chose GitHub as the remote repository.Once created, we pull it locally.All subsequent code is performed under this repository.Create warehouses and pull code without demonstrating.
Create Source DirectoryWe create a directory of SRCs under the warehouse (which is replaced by the word item later for the sake of description) to store our actual code.
The file below is the class that you actually need to call later.Specific demo code can be passed through Demo Code Repository View.The main purpose is to use factory mode, where Cache calls the actual cache class.
<?php // composer demo code declare(strict_types=1); // The namespace here is filled in as you like, and is generated laterComposer.jsonThis namespace is required for files. namespace Bruce; use Bruce\Client\Redis; class Cache { public $redisHandle = ''; public function __construct() { $this->redisHandle = new Redis(); } }
Create all the files in the code repository, and then we can start buildingComposer.jsonFile.
Here's the build process, remember to use the composer init command under the project's root directory.
composer init Welcome to the Composer config generator This command will guide you through creating your composer.json config. #Project Namespace Package name (<vendor>/<name>) [bruce_redis/redis]: bruce_redis/redis #Item Description Description []: composer test #Author Information Author [Card 2 <[email protected]>, n to skip]: Card 2 <[email protected]> #Enter the minimum stable version Minimum Stability []: dev #Project type Package Type (e.g. library, project, metapackage, composer-plugin) []: library #Authorization Type License []: Define your dependencies. #Dependent Information Would you like to define your dependencies (require) interactively [yes]? yes #If a dependency is required, enter the dependency to install Search for a package: php Enter the version constraint to require (or leave blank to use the latest version): >=7.0 Search for a package: Would you like to define your dev dependencies (require-dev) interactively [yes]? yes Search for a package: php Enter the version constraint to require (or leave blank to use the latest version): >=7.0 Search for a package: { "name": "bruce_redis/redis", "description": "composer test", "type": "library", "require": { "php": ">=7.0" }, "require-dev": { "php": ">=7.0" } #Confirm the build project, generateComposer.json Do you confirm generation [yes]? yes Would you like the vendor directory added to your .gitignore [yes]? yes Would you like to install dependencies now [yes]? yes Loading composer repositories with package information Updating dependencies (including require-dev) Nothing to install or update Writing lock file Generating autoload files
Note the <vendor>/<name> configuration item here
according to composer Chinese Web The name of the vendor and the name of the project are translated.Actually, when you install the composer package in another project, it is named under the vendor directory of your other project.For example, I set the configuration item for the sample code to bruce_redis/redis.I use this package for other projects as shown in the following image.
In GenerationComposer.jsonAfter the file, our code is installed in other projects and cannot be used directly because it cannot be automatically loaded for composer. We need to configure it as follows:Composer.jsonAdd the following code to the file.
"autoload": { "psr-4": { "Bruce\\: ""src/"" } }
The main purpose of this configuration item is to tell the composer that the namespace is a Bruce class