Download the PHP package ehime/hello-world without Composer
On this page you can find all versions of the php package ehime/hello-world. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ehime/hello-world
More information about ehime/hello-world
Files in ehime/hello-world
Informations about the package hello-world
Creating your first Composer/Packagist package
Hi everybody! Today I'll write about how you can contribute with PHP community creating packages (or updating your's) using Composer and Packagist.
Using Composer
Composer is a package manager for PHP. You can use packages the community developed and you can contribute with your packages too. Here I'll show how to create a project/package, install Composer inside it and send to Packagist, where others developers can use it inside their projects.
Creating the Package
You can create a new project or update one to use Composer. I'll create a hello world class. It's a simple class but you can create complex projects and share them with the others developers. I'll use "hello-world" as project's name. Composer work in "vendor/package" name format. Here we can set as "vendor" name my name: "ehime" and as package name "hello-world", the name of the project.
Files Structure
You can put all files inside the main dir, but I strongly recommend to create another dir, as "src" to be easier to understand and maintain your code organized. The project structure will start with the follow: hello-world (root dir) src HelloWorld SayHello.php Our SayHello.php file will have:
Starting Composer
As our project is ready we can "install" Composer inside it. This is only create a "composer.json" file inside root dir, but Composer can do that for you. Inside your project root: composer init You'll have the follow Composer response:
What we did here is add information about PHP 5.3 as minimum requirements (require section) and tell Composer to "autoload" (using PSR-0) all files with "HelloWorld" namespace that are inside "src" dir.
Testing Package
Sure we want to do a simple test to verify if our class is working well. You can create a new project and "paste" your classes inside it or test inside your own project, which is better and easier. We're creating a Composer project so we must have Composer files installed inside our projects. So, install it running "composer install" inside your root dir:
composer install
As you have only "php >=5.3.0" inside "composer.json", Composer will install only it's own files. With Composer installed create a directory "tests" inside your root dir. Create the "TestCase.php" file inside it with the follow content:
Go to the terminal (or create a PHP web server inside "tests" dir) and type:
php -f tests/HelloWorld/Tests/TestCase.php
You'll get "Hello World, Composer!". It's working now.
Sending to Packagist.org
Now your project is working and you want to send it to Packagist. The easy way is push your project to Github using Git. Go to Github and create a new public repo called "composer-helloworld", start the Git project inside your root dir and push it:
Now you have your project inside a Github repo and you're ready to send it to Packagist. Go to Packagist web site, create your account, login and Submit a Package. Packagist'll ask you for Repository URL (Git/Svn/Hg). Paste there [email protected]:username/hello-world.git and click "Check!". Packagist will check your project and return the project name. If it's correct accept it.
Packagist Details
Every time you do a new commit to Github you must update the Packagist. Go to your account, your package and click "Force Update!". Packagist will go to Github and update the sources. You can turn on "auto update" going to your Github repo, clicking "Settings", after "Service Hooks" and click the "Packagist" service. There update with your information, like:
- User: your Packagist username, like ehime
- Token: your API token, that you can find inside your Packagist settings link
- Domain: packagist.org Ok! Auto update finished and your package is available to other developers.
From here on out you can now embed this project into another workflow by using the following command.
composer require ehime/hello-world
Our first Composer package is finished, but you can do much more using it. Thanks!
NOTE
If at any time you change your repository url you will sever the Packagist connection and will no longer be able to run autoupdate!