Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
How to create a custom composer package from scratch
1. How to create a
Custom Composer
Package from
scratch
Clickittech.com
2. WHAT IS COMPOSER?
Composer is a dependency manager for PHP. It will manage the
dependencies you require on a project-by-project basis, this means
that Composer will pull in all the required libraries and dependencies to
manage all of them in one place.
WW.CLICKITTECH.COM
3. WW.CLICKITTECH.COMWW.CLICKITTECH.COM
Creating a composer package means that only the code in a repository and projects can be
updated with a few simple steps, and even more, the entire community could have access
to it and use it in thousands of projects.
We'll learn how to create a custom package using composer to consume the GitHub API.
First of all, create the folder structure, for example:
|- vendor/name
|- src
|- Contracts
|- Models
|- Providers
|- tests
|- vendor
After creating the base structure, in the terminal type:
git init #initialize git tracking
composer init #this command will guide you through creating your composer.json file
4. WW.CLICKITTECH.COM
Set the package name vendor/name, in this case: agusrdz/github-api.
Add a package description: This is a demo package to use the API of
Github.
Add an author: Your name your@email.com.
Add minimum stability for the package: dev.
composer.json file configuration
5. WW.CLICKITTECH.COM
Define your dependencies (require) for production manually instead
interactively.
Define your dependencies (require-dev) for development manually instead
interactively.
Confirm the generation of composer.json file.
Confirm if you want the vendor directory added to your .gitignore file.
Now you only need to add every required dependency to use the package.
After editing the composer.json file run composer install on your terminal.
6. WW.CLICKITTECH.COM
{
"name": "agusrdz/github-api",
"description": "This is a demo package to use the API of Github.",
"type": "package",
"license": "MIT",
"authors": [
{
"name": "Agustin Espinoza",
"email": "agustinurdz_@hotmail.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.6",
"guzzlehttp/guzzle": "^6.1",
"jenssegers/model": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "~5.0",
"phpunit/phpunit-mock-objects": "~3.0",
"illuminate/support": "^5.3"
}
}
7. WW.CLICKITTECH.COM
Well, it is almost all what we need... basically, this is the composer.json
required to create a composer package but we need to add some more
lines to define the parameters as autoload, unit test and preferred
stability; so click here to see what is next.
That’s it!!