Create composer.json
The *composer.json* file must live in the root directory of your package – Packagist will look for it there and nowhere else. The file we’ll use in this package looks like this:
{ "name": "digitalformula/hello-world", "description": "Package for testing blog article about Packagist", "license": "MIT", "keywords": [ "test" ], "authors": [ { "name": "Chris Rasmussen", "email": "packages@digitalformula.net", "homepage": "http://www.digitalformula.net", "role": "Developer" } ], "minimum-stability": "dev", "require": { "php": ">=5.4.0" }, "autoload": { "psr-0": { "DigitalFormula\\HelloWorld": "src/" } } }
The name, description, license, keywords and authors parts are self-explanatory. However, the package name should follow certain conventions. These are outlined in the Packagist “About” page.
The ‘require’ section in this example specifies that we need to be running PHP 5.4.0 or later before this package will be installed. For complete information on how Composer versioning works and how you can specify versions correctly, please read the Composer Basic usage page.
Please consider how powerful and stable you can make your packages at this point. For example, if you have an application that makes use of the excellent new features in PHP 5.5, you can require that PHP 5.5 be available before your package will be installed. Personally I’d like to see more web hosts support PHP 5.5 so I can use try-catch-finally blocks, but I’m out of luck so far! 🙂
Create supporting files
The README.md and LICENSE are standard files for all Github repositories – remember that Packagist packages are based on Github repositories. The following external articles already cover how these files work so I won’t reproduce their work here.
Create A Repo
Open source licensing
Create HelloWorld.php
The main file for this tiny package is HelloWorld.php. It specifies the namespace, the PrintHelloWorld class and the class method that will print a test message for us.
HelloWorld.php lives in the directory described above and looks like this. You’ll notice that I’m in the habit of including phpDocumentor-style comments in my files. If you get into that habit, it’ll make generating documentation much easier later.
<?php /** * * Contains the PrintHelloWorld class * * @author Chris Rasmussen, digitalformula.net * @category Tests and POC * @license MIT * @package DigitalFormula\HelloWorld * */ namespace DigitalFormula\HelloWorld; /** * * The PrintHelloWorld class * * @package DigitalFormula\HelloWorld * */ class PrintHelloWorld { /** * Print a simple 'Hello, World!' message * @returns string The 'Hello, World!' message */ public static function SayIt() { return 'Hello, World!'; } /* SayIt */ } /* PrintHelloWorld */ ?>
You can omit the closing PHP tag, if you like; it’s not required if a file contains PHP only. Some IDEs like PHPStorm will show a warning if you include it, although there’s technically nothing wrong with leaving it there, either.
Setup local repository
In a terminal session, make sure you’re in the root directory of the package. In the example above, this means being in the ‘digitalformula’ directory – note that it’s the top-level directory, not the ‘DigitalFormula’ directory lower down.
git init
Add all the files to the new repo:
git add .
Commit the changes. All files will be added as everything is considered a change right now. Change the text inside the single-quotes if you like, although the text I’ve used is quite common when doing the first commit.
git commit -m 'Project creation & initial commit'
Setup Github repository
Follow the instructions on Github to create the online version of your local repository. Make sure you choose a meaningful name as it’s the name everyone will use if they decide to use your package.
The best instructions are the ones published by Github themselves in their <a href="https://help.github.com/articles/create-a-repo" title="Create a repo">Create a repo</a> document. It's the same document we looked before when creating the README.md file. Make sure you complete all the steps, including the part that adds the remote repository to your local repository. It should be something like this (obviously the username and repository names will be different). git remote add origin https://github.com/username/repository.git
At this point I'll assume you've successfully followed the instructions and have a copy of your local repository on Github.