How to create your own composer code package

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 Directory

We 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.

Write 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.

generateComposer.jsonconfiguration file

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 <2665274677@qq.com>, n to skip]: Card 2 <2665274677@qq.com>
#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.

Configure Automatic Loading

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, that the source directories are under src, autoload_When the psr4.php file is loaded again, it is downloaded from this directory.Here Bruce is the command space mentioned in writing the actual code.If you don't have that name, change it to your own.For example, you wrote Alibab\Composer, and the configuration in autoloading is as follows:

"autoload": {
    "psr-4": {
      "Alibab\Composer\\": "src/"
    }
}

Publish Code

Once we've done the above configuration, we can publish the code to packagist .First we need to update the code to the GitHub repository.Log in to packagist, click the submit button in the upper right corner, and enter the GitHub repository address in the input box.

Effect demonstration

Installation Code

Once the code is published, you can import the package directly into your project.

If you are using the composer Ali source, you may not be able to use it immediately because the Ali source is not fully synchronized yet.You can switch to an official source.

composer require bruce_redis/redis dev-master

After using this command, the results in the following figure prove that the installation was successful.You can then follow the sample code from the GitHub repository.

Automatic Loading

As mentioned above, composer loads automatically. From the following figure, we can see that composer loads files automatically according to configuration.

Here/bruce_The redis/redis/src value is,Composer.jsonThe name (<vendor>/<name>) value inside.

Tags: Programming Redis PHP github JSON

Posted on Wed, 17 Jun 2020 12:08:05 -0400 by mark_nsx