Use Phar to launch your code package

Source: Internet
Author: User
Tags php framework

In an article I wrote a while ago, "the system architecture of the new version of Segmentfault refactoring", many people are interested in the use of Phar on-line code, and I'm here to share with you my current practice.

Which projects are suitable for Phar packaging on-line?

In fact, there is no special limit to this method, only one, your program is a single portal to the Web project that is, all HTTP requests have only one php file as the processor (most programs are index.php).

If your program structure is like this, then basically you can switch to this on-line mode without obstacles. In fact, the vast majority of modern PHP framework-built projects are a single-entry structure.

What do I need to do with the existing code?

If your code structure is more scientific (for example, using most of the framework's recommended structure), then one line doesn't need to change. Only a few need to be aware

    1. include require When you use or refer to other project files in a project, it is best not to use relative addresses directly, but to precede them with __DIR__ constants
    2. Because Phar Packages can only parse code files, it is recommended that static files be deployed separately

OK, when the preparation is done, we can start to see how it was deployed. To make it easy for everyone to understand, I put a very simple small example on GitHub, and its address is

Https://github.com/SegmentFault/phar-sample

From a simple example,

The code structure for this example is similar to most of our projects, and I'm actually trying to emulate the code structure of these projects.
Note that it is best to run this project in PHP 5.4 or more environments

    • appStore the main logic code, such as controller, model, service, etc.
    • libStore some library files, including third-party
    • portalis actually the main entrance of the project, open you back to find that there is only one file index.php, this is the single entry structure I mentioned earlier
    • staticStorage is static files, such as JS, CSS, pictures and so on, this directory requires you to deploy separately, our Phar package will not be packaged inside the content, here just to show a complete project structure
    • build.phpThe file is our packing script.

If your project requires a template file, you may also need a template directory, where I omitted the

Access in the browser 你的项目地址/portal/index.php , you can see the familiar Hello World! words

The most important thing in this project is build.php this file, which shows a simplified packaging process, in fact, a little modification of it can be used in your project, the file's code comment is very detailed, I do not repeat, now we run it under the terminal

Ah oh, seems to be a bit of a problem, but it does not matter, this is an option in PHP is not set to cause, open the php.ini file, find ;phar.readonly = On this line, remove the previous semicolon, ; and then change the back to On Off , and then save and then execute this command

It seems to be successful, and there is already a Sample.phar packaged file in the current directory.

How to deploy the Phar file

Now you must be curious about this Phar file, but don't know how to use it, wtf! But wait, go back to the terminal and execute the command.

php Sample.phar

Well, now that your entire project has been included in this Sample.phar file, and it can be executed directly, it's amazing, so how do we deploy it?

Here are my suggestions.

First generate a version number for each packaged file, such as you can add a build.php line

rename(‘Sample.phar‘, ‘Sample.‘ . date(‘Ymd.His‘) . ‘.phar‘);

So every time after the packaging of the file becomes similar Sample.20141111.123456.phar , and will not be duplicated, there are many ways to generate a non-duplicate ID, I recommend this is because it can be convenient for you to see the packaging time, later you can roll back to know the time to roll back to the version, Of course, you can also associate this version with your SCM version.

Then, use another portal file on the line to refer to this package, such as the entry directory on the online /wwwroot index.php file has the following code

require __DIR__ . ‘/../packages/Sample.20141111.123456.phar‘;

Yes, only one line is so simple! packagesIt is the directory where you store these packaged files and you can place them at will.

I said these steps can be easily written into your current automated deployment process.

The above text is just a start, welcome to the proposal to comment on it and do some performance testing, if you run in more than 5.5 version (default open Opcache), almost no performance impact.

1190000002166235

Use Phar to launch your code package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.