The cause of

I was given the task of refactoring code:

This is a 6 or 7 year old, multi-user system, PHP, running on 50 machines. The code uses the simplest structure, does not use any complete framework, even has three or four thousand lines of code file, when modifying the latest IDE can not help, when problems occur troubleshooting difficulties. Especially when programmers write code, want to reference the previous method can not find, their own definition of new methods and nowhere to put, have to pile up on the already very messy file.

Since this system has been in operation, even the peak QPS of each machine has nearly 1000, and as a business system, there was no CR before, which was filled with some “strange” logic and writing methods, which made people’s scalp numb, migrating this system is like changing the wheels of a running car.

Writing articles related to the project migration is not only to share experience, but also to sort out thoughts in the steps of the migration, as little as possible to step on the pit, and also incidentally precipitation of knowledge.

Articles are often people climb, but also do not indicate the original address, I’m here to update and error correction can’t sync, mention here the original address: http://www.cnblogs.com/zhenbianshu/p/7773930.html


The problem

The first is the positioning problem. The previous code had the following problems:

  • File: the code organization logic is not obvious, manual loading file is not convenient, and the file has many lines of code, there are many unnecessary references.
  • Code: function definition logic is not clear, according to the function to find the function is not convenient; And the code coupling degree is high, resulting in low reuse rate; Multiple defined global variables are likely to be referenced and modified somewhere, raising exceptions.
  • Configuration: The configuration is scattered in various files, so the reference is not easy to check. Therefore, the configuration cannot be completely changed.
  • Test: There is no test item, and the risk cannot be controlled after modification.

To address these issues, consider migrating your code to the Yaf framework and refactoring it:

  • The Yaf framework is used to manage code organization, and namespaces are used for easy loading and on-demand loading.
  • Use namespaces and classes to logically aggregate methods to avoid global variable risk; Code stratification, separation of data and logic, improve the reuse rate of data code and part of logic code;
  • Unified management of configuration data avoids multiple dependencies and reduces configuration modification risks.
  • Add PHPUnit unit tests to reduce the risk of code changes;

Talk about framework

The framework

When we collaborate on large projects, we have to think about how to maximize code reuse and how to make sure that a developer can quickly find the method he wants on a large project. Over time, people will come up with a formula to solve these problems, and develop the same way every time they face a development task. Some skilled developers abstracted this routine, organized it, and implemented it as a framework.

So the framework is designed to solve an open problem with a certain constraint support structure. Analyze from several aspects of its definition:

  • Problem solving: The problem the framework is trying to solve isDevelop specifications and efficiencyProblems, using the same rules, can greatly reduce the mental burden of deciding on many strategies.
  • Binding: no rules no fangyuan, since it is a standard, so must have its binding. Frameworks typically impose constraints on files, methods, names, classes, and so on.
  • Support structure: A framework is just a support structure that can be used widely, like a shelf where developers can put their code cargo.

To get a deeper understanding of the framework idea, I have previously written about my own PHP framework, Github-Zhenbianshu-sqire_framwork, as well as three accompanying blog posts about how to build my own PHP framework.

The framework just implements the MVC design pattern and simple routing. Those who are interested in this can fork down and have a look.

Yaf

Yaf should know a lot about PHP, but I don’t want to talk about it here. It exists as a PHP extension written in C, which is of course efficient, but Yaf was chosen more for its “freedom” as a framework.

Yaf gives developers the maximum freedom to define code structures and to define individual requirements between routing steps. Yaf, on the other hand, only provides some help when it is most appropriate, just enough to meet development requirements, without adding unnecessary rules and restrictions.

When we use navigation software, the traditional framework will map out a route, which may take a long detour in order to make sure you don’t need a restaurant or hotel, and the developer must follow that route; Yaf just gives directions, taking straight or crooked paths all by itself.

Of course, there is a “price” to be paid for this freedom. Without the guidance of a framework, the complexity of project layering and structure has to be determined by ourselves.


structure

The code structure was designed by me, referring to the structure of several existing projects, and also trying to be compatible with the writing method of the current project, so that colleagues could accept it as easily as possible. I can’t say best, but at least for now, it’s the best fit.

The overall structure

As a front-line developer, I’ve done a lot of work to sort out the best code structure for developers. Finally, combining MVC and three-tier architecture (three-tier architecture: UI presentation layer, BLL business logic layer, DAL data access layer), the current four-tier code structure is sorted out:

Considering that M layer in MVC will become logic complex due to business expansion, and finally become bloated and difficult to maintain; In the three-tier architecture, the presentation layer is too thin and the View is not easy to control. BLL/DAL/V/C;

From top to bottom:

  • V: interface data output, log, file, view page;
  • C: Controllers
  • BLL: business logic Service;
  • DAL: Data access layer, including internal data access :Db, and external interface data access: Api.

Support layer

In addition to the four-tier code structure, two structures are reserved for global support:

Tools: Some functions can be cumbersome to call due to limitations that forbid cross-layer calls. This provides globally available tools that developers can load on demand at each layer.

Config: Provides centralized file configurations. You can quickly locate and modify configurations in a clear folder.

In addition, some very common methods and constants are registered as global, eliminating unnecessary frequent loading; At the same time, it also borrows Yaf built-in global variables to provide public data pass-through function.

File structure

Below is the structure of the file that I have reduced to the point of no further reduction, for your reference and for your suggestions for improvement.

├ ─ ─ app │ ├ ─ ─ the Bootstrap. PHP │ ├ ─ ─ controllers │ │ ├ ─ ─ the Error. The PHP │ │ └ ─ ─ Mobile │ │ └ ─ ─ Search. PHP │ └ ─ ─ library │ ├ ─ ─ Api │ │ └ ─ ─ Util. PHP │ ├ ─ ─ the Db │ │ └ ─ ─ the User │ │ └ ─ ─ Addr. PHP │ ├ ─ ─ Service │ │ └ ─ ─ the Order │ │ └ ─ ─ Count. PHP │ └ ─ ─ the Tools │ └ ─ ─ Http. PHP ├ ─ ─ the config │ ├ ─ ─ application. Ini │ ├ ─ ─ the error │ │ └ ─ ─ API. PHP │ └ ─ ─ rewrite. PHP ├ ─ ─ public │ └ ─ ─ index. The PHP ├─ ├─ exercises, exercises, exercises, exercises, exercises, exercises, exercises, exercises, exercisesCopy the code


thinking

In addition, there are some tricky questions:

The Cache and the Db

In the current code structure, I integrate Redis Cache and Mysql Db into the same Db layer.

The main considerations are:

  • The project is stable, the database type is controllable and will not be extended;
  • Most of the data directly use Redis as Db, that is, a single database;
  • Add a data control layer in the middle and developers will write a lot of meaningless code in order to comply with the non-cross-layer specification;

I don’t know if it’s going to be a pit, but even if it’s going to be complicated, it’s not going to be a big risk to split this layer up again.

Static method or class method

Most of the logic in the project is to add, delete, change, check or data processing, so I generally use static method in the bottom method. Because static method does not need to instantiate objects, it is more efficient than using class method both in development and operation.

Of course, class methods are fully supported for complex, long-flow data processing.

Business logic partitioning, entity or logic

In the business logic layer, file classification is the most difficult thing. For example, the logic related to user operation order:

  • Neither the user class nor the order class can be represented completely accurately if broken down by entity.
  • When broken down by logic, the variety of operating logic is equally maddening.

At present, it mainly uses entity splitting and only considers the operated objects. For example, the logic of querying user orders is placed in the user class, while the logic of deleting user orders is placed in the order class.

In addition, some proprietary and complex logic will be extracted to achieve some logic classes, such as the order has a variety of types of sorting, are put in the order class will not be reused, then abstract out an order sorting class.


summary

Now that the project has just finalized the code structure and refactured the basic methods, the business code is still in the process of migration. Next time I will talk about grayscale testing through frame routing and Nginx configuration.

Since it is a business department, the development of the business is the top priority, and the code migration can only be ranked behind the business requirements. Moreover, there are not enough developers, and it takes too much time to sort out the weird logic before, so the migration is very slow. This series of articles will be updated occasionally, welcome students who have the same experience to pay attention or find opinions.

If you have any questions about this article, please leave a comment below. If you think this article is helpful to you, you can click the recommendation below to support me. The blog is always updated, welcome to follow.