How to create your own Composer/packagist package (PHP)

Source: Internet
Author: User
Tags autoload autoloader composer install

How to create a composer/packagist bag of your own

First let's take a quick step to GitHub to create a new library, which is named composer-car , and it's fast to clone it locally:

git clone Http://go.rritw.com/github.com/GeHou/composer-car.gitcd Composer-car

This Composer-car folder is the root of your package, you just need to remember which Composer.json is in the package, which is usually the root of the package. What's that? To make a bun table? It is also possible to understand this, but the same school can collect your saliva first.

Now that we have no Composer.json file, you can generate and edit it by composer, and of course the composer is ready for US command line, look:

Although the above struggling put down generated the Composer.json file, we had to add something to it. Use your familiar editor to open the Composer.json file and modify it to the following:

{    "name": "Hou/composer-car",    "description": "In order to study composer",    "license": "MIT",    "authors ": [        {            " name ":" Gehou ",            " email ":" * * * @gmail. com "        }    ],    " minimum-stability ":" Dev ",    " Require ": {        " php ":" >=5.3.0 "    },    " AutoLoad ": {        " ford\\escape\\ ":" Src/ford/escape ",        " ford\\fusion\\ ":" Src/ford/fusion ",        " ford\\focus\\ ":" Src/ford/focus ",        " ford\\fiesta\\ ":" src/ford/ Fiesta "        }    }   }

The Heart's partners may have recognized Ford's logos, which says we are all fellow-players, and you must also like cars, right? :-)

We landed on Ford's web site to see what kind of hot car, um, there are escape, FUSION, FOCUS, FIESTA, Chinese name is the wing Tiger, Mondeo, Fox, Ka wah, ka Wah st my dream Ah ~ ~ ~ Well, the crossing put down your hand board brick, I admit that the car will go on and on, let's crowded the water out and continue to talk.

According to the mapping of the above named space and the records, the package should look like this:

composer-car-src--ford---escape----escape2013.php---fiesta----fiesta2013.php---focus---Focus2013. php---fusion----fusion2013.php-. gitignore-composer.json-readme.md

escape2013.php:

<?phpnamespace ford\escape;class escape2013{Public    static function info ()    {        echo ' this is Ford Escape2013!<br/> ";    }}

fiesta2013.php:

<?phpnamespace ford\fiesta;class fiesta2013{Public    static function info ()    {        echo ' this is Ford Fiesta2013!<br/> ";    }}

focus2013.php:

<?phpnamespace ford\focus;class focus2013{Public    static function info ()    {        echo ' this is Ford Focus2013 !<br/> ";    }}

fusion2013.php:

<?phpnamespace ford\fusion;class fusion2013{Public    static function info ()    {        echo ' this is Ford Fusion2013!<br/> ";    }}

After finishing the above, we need to install the composer to test whether our package works properly, and install it easily in the root of the package, the installation can be:

Composer Install

After a few mysterious hints, you'll end up with a map of namespaces and records in the vendor/composer/autoload_psr4.php, which is wrapped in a number:

<?php//autoload_psr4.php @generated by Composer$vendordir = DirName (dirname (__file__)); $baseDir = DirName ($ VENDORDIR); return Array (    ' ford\\fusion\\ ' = = Array ($baseDir. '/src/ford/fusion '),    ' ford\\focus\\ ' = = Array ($baseDir. '/src/ford/focus '),    ' ford\\fiesta\\ ' = = Array ($baseDir. '/src/ford/fiesta '),    ' ford\\escape\\ ' = = Array ($baseDir. '/src/ford/escape '),);

If it is made into a packagist bag and then installed, then it is not $basedir but $vendordir.

Then we create a test file show.php, filling it with the following contents:

<?phprequire ' vendor/autoload.php '; use Ford\escape as Escape;use Ford\fiesta as Fiesta;use Ford\Focus as Focus;use for D\fusion as Fusion;echo escape\escape2013::info (); Echo Fiesta\fiesta2013::info (); Echo Focus\focus2013::info (); Echo Fusion\fusion2013::info ();

Open the browser http://foo.com/composer-car/show.php and hit (foo.com is my local test domain name, please switch to a small partner).

In turn, the browser outputs:

This is Ford escape2013! This is Ford fiesta2013! This is Ford focus2013! This is Ford fusion2013!

Is it a little bit of a shock? Don't worry, not a pair of birds, and the appearance of the spread of the process? If you are really in a hurry to WC or feel the tutorial too simple, Hougo will not let you pick up soap.

First, as part of the code, we don't need to push to GitHub, so we'll show.php into the Cold palace, edit the. gitignore file, and add show.php at the end. At this point some of the partners may wonder why there is a/vendor/on it, and remember when we answered a question when we init the package?

Would the vendor directory added to your. Gitignore [yes]? Yes

Yes, well, you understand?

In a few words, the code was push to GitHub after a phala hit by a professional player, and the Phala content was as follows:

$ git status# on branch master# untracked files:#   (use "git add <file> ..." to include in what'll be committed) # #   . gitignore#   composer.json#   Src/nothing added to commit but untracked files present (use "Git add" to track) $ git Add. $ git commit-m "GoGoGo" $ git push

Next, you need to sync the code on GitHub to http://go.rritw.com/packagist.org/and go to [Packagist's website] (http://go.rritw.com/packagist.org/) ( http://go.rritw.com/packagist.org/) Register an account (Login with GitHub is an option). Log in and click on the large Green background button to Submit apackage, at the Repository URL (GIT/SVN/HG) to the address on GitHub, which is:

Http://go.rritw.com/github.com/GeHou/composer-car

Now, click Check, and if all goes right, return to the project name and confirm the click Submit to complete the crawl operation.

Under the circumstances, Packagist is not going to update your commit code on GitHub automatically, click Force Update on the items that just came in, and Packagist will crawl the content updates of the repository on GitHub. But this is not self-contained, fortunately, we can configure the setup in GitHub to eliminate the trouble of manual updating.

Enter the Settings page of GitHub, find the Webhooks & services option, click the Configure Services button and find it in the list Packagist, bash it! There is a need to fill in some information, which can be found on the profile page of the Packagist Web site:

    • User name on the user:packagist
    • Token:packagist's License token
    • domain:http://packagist.org

After the patch, click Update Settings, and if the green cut in the list shows OK.

Is it really OK? Still a bit worried? Big deal, let's try it again!

Jump out of root and create a new folder under test environment:

mkdir TEST-AUTO-UPDATECD Test-auto-updatevim Composer.json

This time we do not use the init command and only fill in the Composer.json with some simple content:

{"    require": {        "php": ">=5.3.0",        "Hou/composer-car": "Dev-master"    },    "minimum-stability": " Dev "}

Then:

Composer Install

After the installation, scan the Test-auto-update/src/ford/fiesta to see if there are only 2013 fiesta, and then temporarily do not need to have the content of this record, let us go back to the composer-car of the catalogue.

Note: This time Test-auto-update/vendor the following hou/composer-car to create a project (/) [Hou/composer-car].

I heard that the 2014-year-old Ka-Wah has come out, and we have to add a new car model:

To composer-car/src/Ford/Fiesta create a new file fiesta2014.php, populate it with the following items:

<?phpnamespace ford\fiesta;class fiesta2014{Public    static function info ()    {        echo ' this is Ford Fiesta2014!<br/> ";    }}

Modify the show.php and append to the last:

Echo Fiesta\fiesta2014::info ();

Visit the quiz page to see if it appears:

This is Ford fiesta2014!

OK, re-submit the password:

git Add. Git commit-m "test auto update" Git push

Then go back to Test-auto-update, which is a minor order to play, Because a composer.lock file will be generated after the install command, the installed dependency cannot be updated through the install command, so this time you need to use the composer update command, try this command, and see what happens:

$ composer Updateloading composer repositories with package informationupdating dependencies (including Require-dev)       -Updating hou/composer-car dev-master (91bceb0 = 01550b4)    Checking out 01550b4eeaa85513573ce7406ca7d46ee30c6978writing lock filegenerating autoload files

Like this kind of mysterious information on the screen and a blink, in fact, because of the edge of the network, sometimes blink a long time ~

Anyway, after the update, you should be able to find a test-auto-update/vendor/hou/composer-car/src/Ford/Fiesta/ new file under the folder Fiesta2014.php . But it's important to note that there are times when the sync between Packagist and GitHub may be delayed, so drink a cup of coffee, talk to a sister, buckle the nostrils of the nostril Phala and try the update again.

All right, we're going to create a new index.php file in the test-auto-update to see if we can run, and the file is not much worse than the previous show.php:

<?phprequire ' vendor/autoload.php '; use Ford\fiesta as Fiesta;echo fiesta\fiesta2014::info ();

If it is not wrong, the browser will output the following index.php file:

This is Ford fiesta2014!

At this point the update operation is also proved to be OK, comrade to hurry up their own hands to try it.

Reference material

Chinese files http://go.rritw.com/composer.golaravel.com/

PSR-4 Specifications Http://go.rritw.com/github.com/php-fig/fig-standards/blob/master/proposed/psr-4-autoloader/psr-4-autoloader.md

Examples of this article

Http://go.rritw.com/github.com/GeHou/composer-car

Http://go.rritw.com/packagist.org/packages/hou/composer-car

How to create your own Composer/packagist package (PHP)

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.