Local development of composer & laravel package

Time:2021-4-30

Local development of composer & laravel package

This article is written by Chao GeModular development of PHP based on composerDetailed practice version of.

Development pain points

I believe many people, who don’t know about composer, will do this when developing packages

JCC / new package > write business logic > push to code warehouse > create debug Project > component require JCC / new package - VVV > debug > Modify Project > push update code > component update - VVV in debug project - >

It can be seen that repeatedly submitting code and updating code is very troublesome, and it can’t be debugged in real time.

This paper mainly solves the following problems

When developing a local development package, you can test it locally without submitting the code to a third-party code repository, and then install composer require into the project for testing

Create a project (JCC / new package) - > write business logic > > create debug Project > > configure composer. JSON > > debug > > Modify Project > > debug directly in debug project - >

It can be seen that local development can reduce the time of GIT submitting and fetching code, and reduce the low-level errors of every submitted version.

How local package development works:

Composer connects the locally developed packages from the new project (JCC / taxis) to the debugging project (JCC / test taxis / vendor / JCC / texi) through soft connection

The path type of repositories is mainly used. Please move to update the informationfile

Local development of composer package

First of all, let’s take a look at the structure of the composer installation package. Under normal use, take one and install itlaravel/laravel)For example, open laravel in the vendor directory, and you will see the following structure:

vendor/laravel
├── framework
│   └── src
│       └── Illuminate
│           └── ...
└── tinker
    └── src
        └── Console

At the same time, take a look at the required or required dev dependency package of composer.json file under laravel project

{
      ...,
    "require": {
        "php": ">=7.0.0",
        "fideloper/proxy": "~3.3",
        "laravel/framework": "5.5.*",
        "laravel/tinker": "~1.0"
    },
      ...
}

From this we can know that the directory structure is corresponding.

Create directory and initialize

Of course, when initializing and creating a directory, you should pay attention to the consistency between the project name and the directory structure. For example, if my new project is called JCC / taxi, the directory structure I create should look like this:

jcc
└── taxi

Then we need to develop further under taxis. Of course, before development, we need to initialize the composer configuration and run it under taxis directory

composer init

Follow the instructions to fill in the information:

Local development of composer & laravel package

The composer.json file will appear in the texi directory

{
    "name": "jcc/taxi",
    "description": "This is a test.",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "jcc",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

Create a development directory and set up the necessary information

In general, we will create two directories, one is SRC to store all the logic code of the package, and the other is tests to store test cases

jcc/taxi
├── src
├── tests
├── README.md
├── composer.json
└── LICENSE

It should be noted that we need to set the composer auto loading configuration in composer.json. Add the following in composer.json:

{
      ...,
    "autoload": {
        "psr-4": {
            "Jcc\Taxi\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Jcc\Taxi\Tests\": "tests/"
        }
    },
      ...
}

You can see moreComposer automatically loads documents

Create test package project

First, create a new project in the JCC directory for testing:

jcc
├── taxi
└── test-taxi

Then initialize the composer configuration and generate the composer.json file

composer init

Then add the repositories project in two ways:

The first is to run the command directly

composer config repositories.jcc path /Users/jiajianchan/Sites/jcc/taxi

The second method is to directly add in the composer.json file:

{
      ...,
    "repositories": {
        "jcc": {
            "type": "path",
            "url": "/Users/jiajianchan/Sites/jcc/taxi"
        }
    }
}

The type is path, and the URL is the relative path of the project

The next step is to add dependencies in two ways:

Shell:

composer require jcc/taxi:dev-master -vvv

In composer.json, add:

{
    ...,
    "require": {
     "jcc/taxi": "dev-master"
    },
    ...
}

Of course, you should pay attention to the version number, which must be set in composer. JSON in JCC / taxiprojectminimum-stabilityProperty, otherwise when installing the package, it will report the error that the version number cannot be found.

Development and testing

First, create the client.php file in SRC under JCC / taxiproject

<?php

namespace Jcc\Taxi;

class Client
{
    protected $a;

    protected $b;

    public function __construct(int $a, int $b)
    {
        $this->a = $a;
        $this->b = $b;
    }

    public function addTogether()
    {
        return $this->a + $this->b;
    }
}

After installing the JCC / taxiproject in the JCC / test taxidirectory, add the test. PHP file:

//Introducing composer to load files automatically

Finally, run the test.php file in the JCC / test taxi directory to get the result of addition

php test.php

If you are more careful, you will find that thejcc/taxiA dependency project is aSoft connection. In other words, yourClient.phpAdd a new method to the file, then call it in the jcc/test-taxi project, and do not need to re composer update package. Very convenient.

Local development of laravel package

The local development of a laravel package is basically the same as the development of composer package. Let’s go over it.

First, create a new laravel project:

composer create-project laravel/laravel laravel -vvv

Create the following directory in the laravel project:

laravel
├── app
├── ...
└── packages
    └── jcc
        └── taxi
            ├── LICENSE
            ├── README.md
            ├── composer.json
            ├── src
            │   ├── Taxi.php
            │   └── TaxiServiceProvider.php
            └── tests

jcc/taxi (vendor/name)For the laravel package we’re going to release,jccCorresponding togithub usernametaxiCorresponding toProject name

First, initialize the composer configuration, which is no different from normal package development, and then use thesrcdirectories creatingTaxiServiceProvider.phpFile:

<?php

namespace Jcc\Taxi;

use Illuminate\Support\ServiceProvider;

class TaxiServiceProvider extends ServiceProvider
{
    public function boot()
    {
        //
    }

    public function register()
    {
        $this->app->singleton('taxi', function () {
            return new Taxi;
        });
    }
}

establishTaxi.phpFile:

<?php

namespace Jcc\Taxi;

class Taxi
{
    public function printRunning()
    {
        echo 'running' . "\n";
    }
}

Finally, you want to register the package in the laravel projectconfig/app.phpadd to:

return [
    ...,
    'providers' => [
        ...,
        Jcc\Taxi\TaxiServiceProvider::Class,
    ],
];

Modify thecomposer.jsonFile:

{
    ...,
    "autoload": {
        ...,
        "psr-4": {
            ...,
            "Jcc\Taxi\": "packages/jcc/taxi/src/"
        }
    },
    ...
}

And run the command:

composer dump-autoload

At the end of the dayweb.phpThe following is amended in the text:

Route::get('/', function () {
    app('taxi')->printRunning();
});

At this point, when we visit the home page of the laravel project, therunningThis word, congratulations on your success.

It’s not difficult to develop a package, and it’s not easy to say. The most important thing is that you need a steady stream of creativity!

This work adoptsCC agreementReprint must indicate the author and the link of this article


Nothing is impossible. —— @Jiajian Chan

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]