Focus on PHP, MySQL, Linux and front-end development, thank you for your attention!! The article is organized on GitHub, mainly including PHP, Redis, MySQL, JavaScript, HTML&CSS, Linux, Java, Golang, Linux and tool resources and other relevant theoretical knowledge, interview questions and practical content.

preface

For developers, code packages have become a part of the development process. For example, PHP has Composer, Java has Maven, NPM, YARN, Brew installed for Mac, yum installed for Linux. Using these packages, we can easily manage the external code components introduced by our code, which helps us to improve our development efficiency, and also helps us to manage our code more elegantly. The following is mainly to share a personal custom composer code package, the code is only used as a demonstration example, no actual effect. It will also be updated constantly, so you can pay attention to it.

The specific implementation

Create a remote Git repository.

To create the repository, I was choosing GitHub as the remote repository. Once created, we pull directly to the local. All subsequent code is performed under this repository. I won’t show you how to create the repository and pull code.

Create source directory

We create a SRC directory under the repository (instead of the word project for ease of description) to store our actual code.

Writing actual code

The following file is the actual class that needs to be called later. Specific demo code can be viewed through the demo code repository. The main function is to use the factory mode Cache to call the actual Cache class.


      
// Composer demonstrates code
declare(strict_types=1);

// Fill in the namespace according to your own situation. The generated composer. Json file will use this namespace.
namespace Bruce;

use Bruce\Client\Redis;

class Cache
{
    public $redisHandle = ' ';

    public function __construct()
    {
        $this->redisHandle = newRedis(); }}Copy the code

With all the files in the repository created, we can start generating the composer. Json file.

Generate the composer. Json configuration file

Here’s how to generate it. Remember to use the Composer init command at the root of your project.

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
#Project description
Description []: composer test
#The author informationAuthor [card 2 <[email protected]>, n to skip]: card 2 <[email protected]>#Enter the lowest stable version
Minimum Stability []: dev
#Project type
Package Type (e.g. library, project, metapackage, composer-plugin) []: library
#Authorization type
License []: 
Define your dependencies.

#Rely on the information
Would you like to define your dependencies (require) interactively [yes]? yes
#If a dependency is required, enter the dependency to installSearch 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": "type": "Library", "require" : {" PHP ":" > = 7.0} ", "the require - dev" : {" PHP ":" > = 7.0 "}
#Confirm the build project and generate composer. 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
Copy the code

Note the/configuration item here

According to theComposer Chinese websiteVendor name and project name are translated. When you install the Composer package in another project, it will be named under the vendor directory of your other project. For example, I set this configuration item for the sample code to bruce_redis/redis. I used this package for other projects and it looks like this.

Configuring automatic loading

After generating the composer. Json file, our code was installed in other projects and cannot be used directly because it cannot be automatically loaded for composer. We also need to configure the following code to be added to the composer.

"autoload": {
    "psr-4": {
      "Bruce\\": "src/"}}Copy the code

The main purpose of this configuration item is to tell the composer class whose namespace is Bruce that the source directory is under SRC. The autoload_psr4.php file will be downloaded from this directory when it is loaded. Bruce here is the command space mentioned in writing actual code. If that’s not your name, change it to your own. For example, if you write Alibab\Composer, the configuration in autoload would look like this:

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

Post code

With the above configuration done, we can publish the code topackagist. 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.

Results demonstrate

Installation code

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

If you are using Composer ali source, you may not be able to use it immediately because ali source has not been fully synced. Switch to official source.

composer require bruce_redis/redis dev-master
Copy the code

After using this command, the following results appear, indicating a successful installation. You can then follow the sample code in the GitHub repository.

Automatic loading

As mentioned above, Composer loads automatically. From the following figure, we can see that Composer loads files automatically based on the configuration.

The /bruce_redis/redis/ SRC value is the name(/) value in composer. Json.