Modules in Zend Framework 2.0
Evan Coury


ZendCon Uncon, October 2011

Presenter Notes

Evan Coury (EvanDotPro)

Gravatar



Who am I?

  • Zend Framework contributor
    • I wrote the ZF2 module system
  • Zend Certified Engineer (ZCE)
  • Software Engineer / Consultant / Entrepreneur
    • Lead Developer @ SouthwestMedical.com
    • Owner / Developer @ SMSCloud.com
    • Previously: CBS Radio, Odysseyware

Blog: http://blog.evan.pro/

GitHub: http://github.com/EvanDotPro

Twitter: Sorry, I don't tweet. ;)

Presenter Notes

Everything is subject to change!

Presenter Notes

  • Google-style beta: not 5 years, but stuff will change
  • ZF2 Beta 1 released here at ZendCon
  • 6-week (max) beta release cycles
  • Plans for Beta 2, 3, and 4 already set

What is a module?

Presenter Notes

A re-usable piece of functionality that can be used to construct a more complex application.

Presenter Notes

WRITE CODE ONCE!

Presenter Notes

"User" Module

  • Uses Zend\Authentication AND Zend\Acl
  • Provides db schema
  • Provides forms, models, views, etc
  • Registration, forgot password functionality
  • Could provide social adapters (Google, Facebook, Twitter, OpenID)

Presenter Notes

Examples:

  • Full applications: blog, e-commerce platform, CMS
  • Plugins: payment module for e-commerce, markdown for blog
  • Themes: CSS files, images, replacement view scripts
  • Libraries: Doctrine2 support, Twig support

Presenter Notes

Vocabulary: Modules, Library, Component, Package, whatever...

Presenter Notes

Modules in ZF1 SUCKED

  • Limited support for re-usability
  • No packaging mechanism
  • No distribution mechanism
  • Tightly coupled with MVC
  • Expensive bootstrapping
  • No support for handling static assets
  • Not self-contained
  • No dependency management
  • etc, etc

Presenter Notes

Modules in ZF2 are AWESOME

  • Self-contained, portable, re-usable
  • Phar packaging
  • Pyrus distribution
  • Dependency management
  • Lightweight, very fast (if used properly)
  • Multiple module paths (local and system-wide modules)

...

  • Basically the opposite of everything on the previous slide

Presenter Notes

Module Naming

ZF2 modules are PHP namespaces; same rules apply

  • Foo [valid]
  • FooBar [valid]
  • foobar [valid]
  • f00bar [valid]
  • 1foobar [invalid]
  • Foo-Bar [invalid]
  • Foo.Bar [invalid]

Use a vendor prefix: ZendDeveloperTools, not DeveloperTools

Presenter Notes

Basic ZF2 Module

|~modules/
| |~FooModule/
| | `-Module.php

Presenter Notes

Baisc ZF2 Module

./modules/FooModule/Module.php:

1 <?php
2 
3 namespace FooModule;
4 
5 class Module {}

That's it!

Presenter Notes

Recommended Structure

|~module_root/
| |~configs/
| | `-module.config.php
| |~public/
| | |+css/
| | |+images/
| | `+js/
| |~src/
| | `~<module_namespace>/
| |   `-<code files>
| |+tests/
| |~views/
| | `~<controller-name>/
| |   `-<.phtml files>
| |-autoload_classmap.php
| |-autoload_function.php
| |-autoload_register.php
| `-Module.php

Presenter Notes

The Module Class

  • Single entry point into modules
  • Only requirement for a module

Presenter Notes

Module::init($moduleManager)

  • IT IS OPTIONAL!
  • Most likely to be abused
  • For lightweight tasks ONLY
    • Register autoloader(s)
    • Register event listeners
  • Module Manager passes itself as only argument

Presenter Notes

Module::getConfig($env = null)

  • IT IS OPTIONAL!
  • Expected to return Zend\Config\Config
  • Application environment is passed as string

Presenter Notes

Module::init() Event Example:

 1 <?php
 2 class Module
 3 {
 4   public function init($moduleManger)
 5   {
 6     $events = $moduleManager->events();
 7     $events->attach('init.post', function($e) {
 8       // All modules loaded now
 9     });
10   }
11 }

Presenter Notes

Dependency Resolving

Module::getProvides() and Module::getDependencies()

  • Returns arrays
  • Can turn on dep-solving
  • Default is off
  • This will change and improve

Presenter Notes

Distribution Sources

  • Pyrus
  • Git
  • HTTP
  • Manual (drag and drop, FTP, etc)
  • Web GUI??

Presenter Notes

Installation (not real yet, sorry)

Pyrus

$ zf install MyModule

Git

$ zf install git://github.com/EvanDotPro/MyModule.git

HTTP

$ zf install http://evan.pro/MyModule.phar

Presenter Notes

Only slide with incomplete, everything else is real and works today

Best-in-class Phar support

Presenter Notes

Packaging a module is easy:

  • $ tar -cf MyModule.tar MyModule/
  • On Windows: Right click module folder -> Send to -> Compressed Zip Folder

Presenter Notes

Better than any other framework/project support I've seen.

Phar Packaging is (mostly) Transparent

  • Two Exceptions:
    • Modules should remain in-tact from their upstream distribution (do not perform writes within your modules!)
    • No realpath() or glob() within Phar (PHP Bug #52769)

Presenter Notes

Supprts All Phar Formats

phar, phar.gz, phar.bz2, phar.tar, phar.tar.gz, phar.tar.bz2, phar.zip, tar, tar.gz, tar.bz2, zip

Presenter Notes

Module Paths

Module paths work like PHP the include_path.

Presenter Notes

Application configuration

./configs/application.config.php

1 <?php
2 return new Zend\Config\Config(array(
3   'module_paths' => array(
4     realpath(__DIR__ . '/../modules'),
5   ),
6   'modules' => array(
7     'Application'
8   ),
9 ));

Presenter Notes

Application configuration

./configs/application.config.php

 1 <?php
 2 return new Zend\Config\Config(array(
 3   'module_paths' => array(
 4     realpath(__DIR__ . '/../modules'),
 5     // Override module location for 
 6     // 'non-standard' modules:
 7     'My\Module' => '/some/other/path/MyModule',
 8   ),
 9   'modules' => array(
10     'Application',
11   ),
12 ));

Presenter Notes

The Module Autoloader

1 <?php
2 $appConfig = include __DIR__ . '/../configs/application.config.php';
3 $moduleLoader = new Zend\Loader\ModuleAutoloader(
4     $appConfig->module_paths
5 );
6 $moduleLoader->register();

Presenter Notes

The Module Manager

1 <?php
2 $moduleManager = new Zend\Module\Manager(
3     $appConfig->modules
4 );
5 $config = $moduleManager->getMergedConfig();

Presenter Notes

Performance

Presenter Notes

It's really, really fast.

Presenter Notes

Live Demo!

Presenter Notes

Questions?

Presenter Notes

JoindIn
http://joind.in/4013

Presenter Notes