search
HomeDevelopment ToolscomposerDetailed explanation of how Composer+Git creates a 'service class library'

This article is written by the tutorial column of composer to introduce to you how Composer combines Git to create a "service class library". I hope it will be helpful to friends in need!

Introduction

I always think that now PHP has progressed into the field of engineering. In the past, PHP developers regarded speed as beauty, and speed and scale were always contradictory. Today's PHP projects, especially larger projects, have gradually evolved to a level that requires both engineering and scale. Engineering a code means evolving into an increasingly complex architecture. For complex architectures, microservices are often a good choice.

I needed this question in a recent project. I need to develop a map service. This service is of course not in the form of a simple class library, but has its own database and its own service interface. In this case, the best option is servitization. Of course, there are many ways to service, such as Thrift, HTTP, etc. But I evaluated the current department environment. PHP is the mainstream language, and the progress of my project is also relatively tight. In my eyes, Thrift, HTTP and other methods all use network protocols to achieve service decoupling. It seems to be a serious solution. I think this approach is not necessary when the project is not clearly in critical condition. The disadvantage of using network protocol servitization is that it introduces significant complexity. This complexity often means investment in manpower, material resources, and time. So I hope to be able to provide a "service class library" in the PHP language for development.

What I am thinking of is PHP’s Composer.

Modification of Composer

Create service class library

First, I need to change my "service class library" from My application (named xxx/main1) is independent. For this independence, I did not choose to create a directory in the application (in fact, I thought about creating a directory such as Services). However, if the code is coupled with the business program, I feel that due to human laziness, it is difficult to control oneself from beginning to end and insist on not using various convenient functions in the application. So my choice is to create a new project in the Git repository and name it xxx/mapService.

composer.json

Now there are two Git projects (xxx/main1 and xxx/mapService). I added the following statement to the composer.json file in main1:

Detailed explanation of how Composer+Git creates a service class library

The composer.json in mapService is as follows:

Detailed explanation of how Composer+Git creates a service class library

This configuration tells the main1 project that the Git address of mapService needs to be used version of.

Of course you need to pay attention to the following points:

  • dev-master means directly using the master branch of mapService. If mapService has other tags, you can use the tag information here
  • repositories is the address of the project
  • The service I have here is placed on the GitLab built by our company
  • mapService The namespace of the src folder below is xxxx\\xxxx\\MapService\\ and supports PSR-4
  • mapService uses illuminate/database

Last use composer update -vvv can download the mapService we need and put it in the vendor directory.

Update and modification

Our editor is now in the main1 project. If we have edited and modified the mapService project, and want to merge it into the master branch of mapService , directly enter the vender/xxx/mapService directory and perform Git corresponding operations. This allows direct code modifications.

Independent configuration

The combination of this structure is only the first step in completing the long march of thousands of miles. What is more important later is that when writing this service, I need to always remember not to use everything in main1, so as to maintain the independence of mapService (independence is one of the necessary conditions for servitization). For example, the first problem I encountered was that the configuration file needs to be independent.

My implementation method is to create a Config class directly in mapService, and the configuration is directly written in this class.

I have always felt that the implementation of this configuration file is a bit frustrating, because in this way, this configuration file enters the Git library. But I really can't think of a better solution. There is a way in Laravel to create Config in Laravel's config folder by implementing ServiceProvider, but this method only applies to Laravel. There is no universality. On the other hand, I think which database the service uses is itself part of the service, and it seems to have nothing to do with putting it in the Git library of the service.

Directory structure

Detailed explanation of how Composer+Git creates a service class library

The directory structure is as above

  • Configs provides configuration files
  • Contracts provides interface protocols
  • Exceptions provides exceptions
  • Supports provides third-party methods or class libraries
  • Models provides interaction with the database
  • Node.php implementation Specific interface

The most important thing about the service is the interface protocol. So create a Contracts folder and interface the provided services.

Detailed explanation of how Composer+Git creates a service class library

# The exception handling of the interface should try to use exceptions instead of error codes for interaction. And these exceptions should be customized as much as possible. In this way, there is the possibility of unified processing at the upper level.

Thinking

I position this architectural model as a service-oriented model at the PHP code level. Applicable scenarios should be:

  • Later planning service-oriented
  • Scenarios where both manpower and thinking in the early stage hope to maintain rapid development

and Git The difference between SubTree and SubModule

In fact, these three methods all use one project as the class library of another project. SubTree and SubModule are Git solutions. Composer is a solution for the PHP language. In addition to the function of adding a project to another project, it also provides solutions such as adding versions and dependency solutions. If your project is in PHP, then using Composer is undoubtedly a better choice.

Later protocol serviceization

If my mapService wants to be protocol service-oriented later, then the mapService project can be simplified into an SDK. For the upper-layer business logic, Just use composer update to update.

Service registration and discovery

The so-called "service class library" I call here does not solve the problem of service registration. I have no way of knowing how many projects use me. services. This may require additional process work.

The above is the detailed content of Detailed explanation of how Composer+Git creates a 'service class library'. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
Composer: Streamlining PHP Project DevelopmentComposer: Streamlining PHP Project DevelopmentApr 15, 2025 am 12:08 AM

Composer simplifies PHP project development steps include: 1) adding dependencies, such as adding "laravel/framework":"^8.0" in composer.json; 2) automatically loading, generating an autoload.php file by defining the namespace and classpath; 3) optimizing performance, using composerdump-autoload-o and composerinstall--no-dev-optimize-autoloader commands. Through these steps, developers can be more efficient and avoid common mistakes.

Composer Credentials: What Makes Someone a Composer?Composer Credentials: What Makes Someone a Composer?Apr 14, 2025 am 12:13 AM

The key elements of becoming a composer include: 1. Mastering music theory, 2. Being creative and original, 3. Having technical skills, 4. Being able to express emotions and tell stories, 5. Understand cultural and historical background, 6. Accumulating practice and experience, these elements together constitute the composer's identity and ability.

The Path to Becoming a Composer: A Practical GuideThe Path to Becoming a Composer: A Practical GuideApr 13, 2025 am 12:11 AM

The steps to becoming a composer include: 1. Master the basic elements of music, such as notes, rhythm, harmony, and melody; 2. Select appropriate technical tools, such as AbletonLive; 3. Understand the process of composing, including inspiration acquisition, conception, writing, modification and improvement; 4. Start with simple melody creation and gradually try complex techniques such as harmony; 5. Solve common problems through debugging techniques, such as note selection and rhythm arrangement; 6. Apply performance optimization and best practices, such as using templates, version control, and collaboration.

Composer: The Key to Building Robust PHP ApplicationsComposer: The Key to Building Robust PHP ApplicationsApr 12, 2025 am 12:05 AM

Composer is a key tool for building robust PHP applications because it simplifies dependency management, improves development efficiency and code quality. 1) Composer defines project dependencies through composer.json file and automatically downloads and manages these dependencies. 2) It generates a composer.lock file to ensure that the dependency version is consistent and automatically loaded through vendor/autoload.php. 3) Examples of usage include basic usage such as adding log libraries, as well as advanced usage such as version constraints and environment variable management. 4) Common error debugging techniques include handling dependency conflicts and network problems. 5) Performance optimization suggestions include using composer.lock file and optimizing automatic loading.

Composer Expertise: What Makes Someone SkilledComposer Expertise: What Makes Someone SkilledApr 11, 2025 pm 12:41 PM

To become proficient when using Composer, you need to master the following skills: 1. Proficient in using composer.json and composer.lock files, 2. Understand how Composer works, 3. Master Composer's command line tools, 4. Understand basic and advanced usage, 5. Familiar with common errors and debugging techniques, 6. Optimize usage and follow best practices.

What is a composer doing?What is a composer doing?Apr 08, 2025 am 12:19 AM

Composer is a dependency management tool for PHP, used to declare, download and manage project dependencies. 1) Declare dependencies through composer.json file, 2) Install dependencies using composerinstall command, 3) parse the dependency tree and download it from Packagist, 4) generate the autoload.php file to simplify automatic loading, 5) optimize use includes using composerupdate--prefer-dist and adjusting the autoload configuration.

What is App composer?What is App composer?Apr 07, 2025 am 12:07 AM

AppComposer is a tool for building and managing applications. 1) It simplifies application development and improves efficiency by dragging and configuring predefined components. 2) Developers can define components, combine interfaces, define business logic, and ultimately render the application. 3) Support basic and advanced usage, such as task management and conditional rendering, helping to build flexible applications.

What is a composer used for?What is a composer used for?Apr 06, 2025 am 12:02 AM

Composer is a dependency management tool for PHP. The core steps of using Composer include: 1) Declare dependencies in composer.json, such as "stripe/stripe-php":"^7.0"; 2) Run composerinstall to download and configure dependencies; 3) Manage versions and autoloads through composer.lock and autoload.php. Composer simplifies dependency management and improves project efficiency and maintainability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.