Create your own composer package
-
Upload
lattapon-yodsuwan -
Category
Software
-
view
303 -
download
2
Transcript of Create your own composer package
![Page 1: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/1.jpg)
Create your own composer package
![Page 2: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/2.jpg)
Why• Easy to manage dependencies
• Easy to use with 3rd party library
![Page 3: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/3.jpg)
normally `require`
<?php
require_once ‘pantip/pantip.class.php’; require_once ‘twitter/twitter.class.php’; require_once ‘facebook/facebook.class.php’; require_once ‘instagram/instagram.class.php’;
![Page 4: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/4.jpg)
the composer way
<?php
require_once ‘vendor/autoload.php’;
![Page 5: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/5.jpg)
Create Package$ composer init
// composer.json
{ "name": “clozed2u/hello-composer", "authors": [ { "name": "Lattapon Yodsuwan", "email": "[email protected]" } ], "require": {} }
![Page 6: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/6.jpg)
Directory Structure
hello-composer | |__src | |__Hello.php | |__tests | |__HelloTest.php | |__composer.json | |__optional(circle.yml, .travis.yml, phpunit.xml)
![Page 7: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/7.jpg)
Hello class
<?php
namespace Hello;
class Hello { public function sayAnything($message) { return $message; } }
![Page 8: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/8.jpg)
Hello Tests<?php
use \Hello\Hello;
class HelloTest extends PHPUnit_Framework_TestCase { public function testSayAnything() { $said = new Hello(); $this->assertEquals(
"X JAPAN ROCK!!”, $said->sayAnything("X JAPAN ROCK!!”) );
} }
![Page 9: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/9.jpg)
Add autoload to composer// composer.json
{ …,
"require-dev": { "phpunit/phpunit": "*" }, "autoload": { "psr-4": {"Hello\\": "src/"} } }
![Page 10: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/10.jpg)
Push to git and release
![Page 11: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/11.jpg)
Use package
$ composer install
// composer.json
{ "repositories": [ { "type": "vcs", "url": "https://github.com/clozed2u/hello-composer.git" } ], "require": { "clozed2u/hello-composer": "1.0.0" } }
![Page 12: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/12.jpg)
Push to packagist.org
$ composer require clozed2u/hello-composer
![Page 13: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/13.jpg)
Don’t forget continuous integration
![Page 14: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/14.jpg)
/clozed2u/hello-composer
![Page 16: Create your own composer package](https://reader035.fdocuments.in/reader035/viewer/2022062904/587ed1581a28abdb198b5283/html5/thumbnails/16.jpg)
We are hiring