Slide 1

Slide 1 text

The Wonderful World of Composer & ZF2 by your friend: Ryan Weaver @weaverryan Thursday, October 25, 12

Slide 2

Slide 2 text

Who is this dude? • The Symfony “Docs” guy • KnpLabs US - Symfony & Behat consulting, training, and Kumbaya • Writer for KnpUniversity.com screencasts • Husband of the much more talented @leannapelham knplabs.com github.com/weaverryan @weaverryan Thursday, October 25, 12

Slide 3

Slide 3 text

Intro Life before components (The Desert of the Real) Copyright by Moyan Brenn http://www.flickr.com/aigle_dore Thursday, October 25, 12

Slide 4

Slide 4 text

Zend Framework 2 consists of 48 individual components Thursday, October 25, 12

Slide 5

Slide 5 text

Components are available via Composer Thursday, October 25, 12

Slide 6

Slide 6 text

Snooze... http://www.flickr.com/photos/fiatlux/81268407/ Thursday, October 25, 12

Slide 7

Slide 7 text

Who Cares? Thursday, October 25, 12

Slide 8

Slide 8 text

We Suck at Sharing and that sucks for you @weaverryan 1 Thursday, October 25, 12

Slide 9

Slide 9 text

Including External Libraries is Depressing Thursday, October 25, 12

Slide 10

Slide 10 text

The Big Bummer :( •@weaverryan • How do I autoload their files? • Does their library depend on anything else? • How do I even store their files in my project? Thursday, October 25, 12

Slide 11

Slide 11 text

Include a Zend Framework 1 component in symfony1 Thursday, October 25, 12

Slide 12

Slide 12 text

Thursday, October 25, 12

Slide 13

Slide 13 text

manually download the library and commit it into your project Thursday, October 25, 12

Slide 14

Slide 14 text

if you don’t want the WHOLE library, carefully delete everything except the dependent components Thursday, October 25, 12

Slide 15

Slide 15 text

autoloading is completely custom ... blah gross! Thursday, October 25, 12

Slide 16

Slide 16 text

Components are the key to mastering your framework @weaverryan 2 Thursday, October 25, 12

Slide 17

Slide 17 text

If PHP is big, we’ll thrive If PHP is small, we’ll die @weaverryan 3 Thursday, October 25, 12

Slide 18

Slide 18 text

Communities @weaverryan PHP is Huge! Right? http://www.flickr.com/photos/kitty-kat/ Thursday, October 25, 12

Slide 19

Slide 19 text

@weaverryan PHP > Ruby Thursday, October 25, 12

Slide 20

Slide 20 text

Fragmentation @weaverryan But fragmentation makes us tiny, isolated, and misguided trend-setters http://www.flickr.com/photos/slpunk99/7329609744 Thursday, October 25, 12

Slide 21

Slide 21 text

PHP frameworks < Rails @weaverryan Thursday, October 25, 12

Slide 22

Slide 22 text

I don’t want a damned CakePHP Plugin! @weaverryan CakePHP CodeIgniter Thursday, October 25, 12

Slide 23

Slide 23 text

Fragmentation @weaverryan • More information we have to know • Difficult to hire • Disjointed forums, StackOverflow • Interoperability? What’s that? Thursday, October 25, 12

Slide 24

Slide 24 text

Components are shareable across all of PHP Thursday, October 25, 12

Slide 25

Slide 25 text

Act 1 Making Sharing Sexy Thursday, October 25, 12

Slide 26

Slide 26 text

PHP Framework Interoperability Group http://www.php-fig.org/ Thursday, October 25, 12

Slide 27

Slide 27 text

The United Nations of PHP Thursday, October 25, 12

Slide 28

Slide 28 text

(A) The Problem of Autoloading Thursday, October 25, 12

Slide 29

Slide 29 text

My Autoloader doesn’t like your PHPs @weaverryan • The symfony1 autoloader doesn’t know where ZF1 classes live. The Zf1 autoloader doesn’t know where symfony1 classes live • Each library has its own autoloader that you must discover, configure and use Thursday, October 25, 12

Slide 30

Slide 30 text

like a town where every store has its own currency Thursday, October 25, 12

Slide 31

Slide 31 text

From The Mountain: PSR-0 Class Naming Conventions @weaverryan “Thou shalt name your classes by following a predictable pattern” Thursday, October 25, 12

Slide 32

Slide 32 text

class: sfRequest path: lib/vendor/symfony/???idk Thursday, October 25, 12

Slide 33

Slide 33 text

class: Symfony\Component\HttpFoundation\Request path: vendor/symfony/src/Symfony/Component/ HttpFoundation/Request.php Thursday, October 25, 12

Slide 34

Slide 34 text

use Symfony\Component\ClassLoader\UniversalClassLoader; $loader = new UniversalClassLoader(); $loader->registerNamespaces(array( 'Zend' => __DIR__.'/path/to/zf' )); $loader->register(); Thursday, October 25, 12

Slide 35

Slide 35 text

use Symfony\Component\ClassLoader\UniversalClassLoader; $loader = new UniversalClassLoader(); $loader->registerNamespaces(array( 'Zend' => __DIR__.'/path/to/zf' )); $loader->register(); ** This uses the Symfony2 autoloader. But all PSR-0 autoloaders are really the same. Composer comes with its own. Thursday, October 25, 12

Slide 36

Slide 36 text

(B) Managing Vendors in your project Thursday, October 25, 12

Slide 37

Slide 37 text

Composer Thursday, October 25, 12

Slide 38

Slide 38 text

Act 2 Using Composer http://KnpUniversity.com/screencast/composer Thursday, October 25, 12

Slide 39

Slide 39 text

Composer in 5 easy steps 1. Download composer.phar 2. Find package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12

Slide 40

Slide 40 text

Composer in 5 easy steps 1. Download composer.phar 2. Find package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12

Slide 41

Slide 41 text

curl -s https://getcomposer.org/installer | php Download Composer.phar Source: http://getcomposer.org/download/ Thursday, October 25, 12

Slide 42

Slide 42 text

This downloads an executable file: composer.phar Thursday, October 25, 12

Slide 43

Slide 43 text

Execute it... And it’ll list the available commands Thursday, October 25, 12

Slide 44

Slide 44 text

Composer in 5 easy steps 1. Download composer.phar 2. Find package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12

Slide 45

Slide 45 text

Packagist.org @weaverryan • Composer installs packages • A package is a directory that contains anything (usually PHP classes) • Every package has a unique name • The mega-repository for packages is http://packagist.org Thursday, October 25, 12

Slide 46

Slide 46 text

Search for something Thursday, October 25, 12

Slide 47

Slide 47 text

Find the name of the package Thursday, October 25, 12

Slide 48

Slide 48 text

Find the right version ** Bleeding edge (unstable) ** Development branch (likely unstable) ** Latest stable version (safest bet!) Thursday, October 25, 12

Slide 49

Slide 49 text

knplabs/knp-menu: v1.1.2 Thursday, October 25, 12

Slide 50

Slide 50 text

Composer in 5 easy steps 1. Download composer.phar 2. Find package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12

Slide 51

Slide 51 text

{ "require": { "knplabs/knp-menu": "v1.1.2" } } Create composer.json Thursday, October 25, 12

Slide 52

Slide 52 text

Or call php composer.phar init to create the composer.json interactively Thursday, October 25, 12

Slide 53

Slide 53 text

Composer in 5 easy steps 1. Download composer.phar 2. Find package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12

Slide 54

Slide 54 text

So Far we have... @weaverryan • The composer.phar executable • A composer.json with the name and version of a package • ... that’s all! Thursday, October 25, 12

Slide 55

Slide 55 text

Now we tell Composer to read composer.json and download our libraries Thursday, October 25, 12

Slide 56

Slide 56 text

php composer.phar install Thursday, October 25, 12

Slide 57

Slide 57 text

Thursday, October 25, 12

Slide 58

Slide 58 text

Hallo Install! The “install” command does 2 things 1. Downloads all the required libraries into the vendor/ directory 2. Generates some files to making autoloading effortless Thursday, October 25, 12

Slide 59

Slide 59 text

Composer in 5 easy steps 1. Download composer.phar 2. Find package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12

Slide 60

Slide 60 text

use Knp\Menu\MenuFactory; use Knp\Menu\Renderer\ListRenderer; $factory = new MenuFactory(); $menu = $factory->createItem('My menu'); $menu->addChild('Home', array('uri' => '/')); $menu->addChild('Comments', array( 'uri' => '/comments' ))->setAttribute('class', 'comments'); $renderer = new ListRenderer(); echo $renderer->render($menu); require 'vendor/autoload.php'; That’s the magic!!! Thursday, October 25, 12

Slide 61

Slide 61 text

Thursday, October 25, 12

Slide 62

Slide 62 text

Thursday, October 25, 12

Slide 63

Slide 63 text

Act 3 Composer and Zend Framework 2 Thursday, October 25, 12

Slide 64

Slide 64 text

Zend Framework 2 • Zend Framework 2 is a group of components (i.e. normal PHP libraries) • If you’re using Zend Framework, you actually have a skeleton that is built to leverage these components Thursday, October 25, 12

Slide 65

Slide 65 text

https://github.com/zendframework/ZendSkeletonApplication Thursday, October 25, 12

Slide 66

Slide 66 text

php composer.phar create-project A special one-time-use way to “clone” a project skeleton Thursday, October 25, 12

Slide 67

Slide 67 text

Typical Usage php composer.phar create-project package/name local/dir Thursday, October 25, 12

Slide 68

Slide 68 text

Typical Usage php composer.phar create-project package/name local/dir Don’t forget to download or copy composer.phar to your current directory Thursday, October 25, 12

Slide 69

Slide 69 text

Typical Usage php composer.phar create-project package/name local/dir The command name Thursday, October 25, 12

Slide 70

Slide 70 text

Typical Usage php composer.phar create-project package/name local/dir use php composer.phar create-project --help to learn about any command Thursday, October 25, 12

Slide 71

Slide 71 text

Typical Usage php composer.phar create-project package/name local/dir The name of the package on Packagist.org Thursday, October 25, 12

Slide 72

Slide 72 text

Typical Usage php composer.phar create-project package/name local/dir Local path to download the project Thursday, October 25, 12

Slide 73

Slide 73 text

php composer.phar create-project \ The ZF2 Skeleton App Thursday, October 25, 12

Slide 74

Slide 74 text

php composer.phar create-project \ The ZF2 Skeleton App ... just breaking a long shell command onto multiple lines... Thursday, October 25, 12

Slide 75

Slide 75 text

php composer.phar create-project \ --repository-url="http://packages.zendframework.com" \ The ZF2 Skeleton App ZF2 Packages are stored on their own private “Packagist” Thursday, October 25, 12

Slide 76

Slide 76 text

The ZF2 Skeleton App php composer.phar create-project \ --repository-url="http://packages.zendframework.com" \ zendframework/skeleton-application \ The name of the package on packages.zendframework.com Thursday, October 25, 12

Slide 77

Slide 77 text

The ZF2 Skeleton App php composer.phar create-project \ --repository-url="http://packages.zendframework.com" \ zendframework/skeleton-application \ zf2 ... and our local directory name Thursday, October 25, 12

Slide 78

Slide 78 text

Thursday, October 25, 12

Slide 79

Slide 79 text

create-project • Clones the project • Runs php composer.phar install to download zendframework/zendframework into the vendor/ directory • You now have a project skeleton that uses Composer to bring in Zf2 #winning Thursday, October 25, 12

Slide 80

Slide 80 text

Now, how can I bring in other libraries? Thursday, October 25, 12

Slide 81

Slide 81 text

{ "name": "zendframework/skeleton-application", "description": "Skeleton Application for ZF2", "license": "BSD-3-Clause", "keywords": [ "framework", "zf2" ], "homepage": "http://framework.zend.com/", "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.*", "knplabs/knp-menu": "v1.1.2" } } Update composer.json Thursday, October 25, 12

Slide 82

Slide 82 text

php composer.phar install Thursday, October 25, 12

Slide 83

Slide 83 text

php composer.phar update ** Remember to download or copy composer.phar into your Zf2 skeleton project Thursday, October 25, 12

Slide 84

Slide 84 text

composer.lock When you install vendors, Composer creates a composer.lock file, with the exact details and version of all libraries Thursday, October 25, 12

Slide 85

Slide 85 text

create-project • install: ignores composer.json and reads frozen vendors from composer.lock • update: ignores composer.lock and re- parses composer.json, updating packages to the latest version specified there and then updates composer.lock Thursday, October 25, 12

Slide 86

Slide 86 text

create-project • install: use day-to-day to make sure your vendor libraries are right where they should be • update: use only when you’re specifically adding or upgrading a library Thursday, October 25, 12

Slide 87

Slide 87 text

Thursday, October 25, 12

Slide 88

Slide 88 text

Start using the library immediately Thursday, October 25, 12

Slide 89

Slide 89 text

use Knp\Menu\MenuFactory; use Knp\Menu\Renderer\ListRenderer; class IndexController extends AbstractActionController { public function indexAction() { // would be even better as a service... $factory = new MenuFactory(); $menu = $factory->createItem('My menu'); $menu->addChild('Home', array('uri' => '/')); // ... $renderer = new ListRenderer(); return new ViewModel(array( 'renderedMenu' => $renderer->render($menu) )); } } Thursday, October 25, 12

Slide 90

Slide 90 text

Act 4 Using Zend Framework 2 Components from Anywhere Thursday, October 25, 12

Slide 91

Slide 91 text

Super Easy... Thursday, October 25, 12

Slide 92

Slide 92 text

One Trick... Thursday, October 25, 12

Slide 93

Slide 93 text

ZF2 hosts its packages outside of Packagist.org... Thursday, October 25, 12

Slide 94

Slide 94 text

composer.json { "repositories": [ { "type": "composer", "url": "http://packages.zendframework.com/" } ] } Source: http://framework.zend.com/downloads/composer Now your composer looks for packages at packagist.org *and* packages.zf.com Thursday, October 25, 12

Slide 95

Slide 95 text

Add your packages... Thursday, October 25, 12

Slide 96

Slide 96 text

composer.json { "repositories": [ { "type": "composer", "url": "http://packages.zendframework.com/" } ], "require": { "zendframework/zend-config": "2.0.*", "zendframework/zend-http": "2.0.*" } } Thursday, October 25, 12

Slide 97

Slide 97 text

Download composer.phar php composer.phar install Thursday, October 25, 12

Slide 98

Slide 98 text

Download composer.phar php composer.phar update ** if there is no composer.lock yet, update == install Thursday, October 25, 12

Slide 99

Slide 99 text

Download composer.phar php composer.phar update require 'vendor/autoload.php'; This belongs in some bootstrap file in your app Thursday, October 25, 12

Slide 100

Slide 100 text

And that’s it! Thursday, October 25, 12

Slide 101

Slide 101 text

Act 5 Kicking ass in the new PHP eco-system Thursday, October 25, 12

Slide 102

Slide 102 text

The PHP Ecosphere @weaverryan Zend Framework is only one part of the picture What other stuff exists? • Zend Framework • Symfony • eZ Components • Drupal • ... • Individuals Thursday, October 25, 12

Slide 103

Slide 103 text

How do we find good libraries? Thursday, October 25, 12

Slide 104

Slide 104 text

Thursday, October 25, 12

Slide 105

Slide 105 text

Thursday, October 25, 12

Slide 106

Slide 106 text

Some Favorites Thursday, October 25, 12

Slide 107

Slide 107 text

Mink behat/mink http://mink.behat.org/ By @everzet (this is a current photo) Thursday, October 25, 12

Slide 108

Slide 108 text

Command a browser, find elements, click them, and fill out forms $driver = new \Behat\Mink\Driver\SahiDriver('firefox'); $session->visit('http://my_project.dev/some_page.php'); $page = $session->getPage(); $anchor = $page->find('css', '.something'); $anchor->click(); // get the content of the new page echo $page->getContent(); Thursday, October 25, 12

Slide 109

Slide 109 text

Monolog monolog/monolog https://github.com/Seldaek/monolog Your Friend Jordi Thursday, October 25, 12

Slide 110

Slide 110 text

A Logger, where bells and whistles come standard use Monolog\Logger; use Monolog\Handler\StreamHandler; use Monolog\Handler\FirePHPHandler; // Create the logger $logger = new Logger('my_logger'); $logger->pushHandler(new StreamHandler( __DIR__.'/my_app.log' )); $logger->pushHandler(new FirePHPHandler()); $logger->addInfo('My logger is now ready'); Thursday, October 25, 12

Slide 111

Slide 111 text

Yay Find More! Thursday, October 25, 12

Slide 112

Slide 112 text

Epilogue 4 Reasons to get silly-excited about the Future Thursday, October 25, 12

Slide 113

Slide 113 text

1) Look for Participation & Consolidation Thursday, October 25, 12

Slide 114

Slide 114 text

Shared Building-blocks @weaverryan • Drupal • phpBB • Midgard • Zikula • eZ Publish • ... Thursday, October 25, 12

Slide 115

Slide 115 text

Less Library Duplication? @weaverryan • Zend/Form • Zend/Serializer • Zend/Http • Zend/EventManager • Zend/Log • Zend/Navigation • ... • Symfony/Form • Symfony/Serializer • Symfony/HttpFoundation • Symfony/EventDispatcher • Monolog • KnpMenu • ... Thursday, October 25, 12

Slide 116

Slide 116 text

2) More high quality, community-grown libraries Thursday, October 25, 12

Slide 117

Slide 117 text

Solving 1 specific problem is a low barrier to entry Thursday, October 25, 12

Slide 118

Slide 118 text

Find them, fork them. Thursday, October 25, 12

Slide 119

Slide 119 text

Write their docs :) Thursday, October 25, 12

Slide 120

Slide 120 text

Programmers that write docs get hugs Thursday, October 25, 12

Slide 121

Slide 121 text

3) Easier upgrades Thursday, October 25, 12

Slide 122

Slide 122 text

4) Grow your Community Thursday, October 25, 12

Slide 123

Slide 123 text

Solutions exist outside of your framework Thursday, October 25, 12

Slide 124

Slide 124 text

Weier O'Phinney will not come to your house and tell you about them Thursday, October 25, 12

Slide 125

Slide 125 text

Image: http://framework.zend.com/404 Thursday, October 25, 12

Slide 126

Slide 126 text

Cast your vote by using the best libraries and improving them Thursday, October 25, 12

Slide 127

Slide 127 text

and realize the power of the entire PHP community Thursday, October 25, 12

Slide 128

Slide 128 text

Thanks... Ryan Weaver @weaverryan KnpUniversity.com PHP Tutorial Screencasts Thursday, October 25, 12

Slide 129

Slide 129 text

... and we love you! Ryan Weaver @weaverryan joind.in/7038 Thursday, October 25, 12