Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Create your own composer package

Create your own composer package

Gentle introduction to php package to use with composer

Avatar for Lattapon Yodsuwan

Lattapon Yodsuwan

December 16, 2015
Tweet

Other Decks in Programming

Transcript

  1. Create Package $ composer init // composer.json { "name": “clozed2u/hello-composer",

    "authors": [ { "name": "Lattapon Yodsuwan", "email": "[email protected]" } ], "require": {} }
  2. Directory Structure hello-composer | |__src | |__Hello.php | |__tests |

    |__HelloTest.php | |__composer.json | |__optional(circle.yml, .travis.yml, phpunit.xml)
  3. Hello class <?php namespace Hello; class Hello { public function

    sayAnything($message) { return $message; } }
  4. 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!!”) ); } }
  5. Add autoload to composer // composer.json { …, "require-dev": {

    "phpunit/phpunit": "*" }, "autoload": { "psr-4": {"Hello\\": "src/"} } }
  6. Use package $ composer install // composer.json { "repositories": [

    { "type": "vcs", "url": "https://github.com/clozed2u/hello-composer.git" } ], "require": { "clozed2u/hello-composer": "1.0.0" } }