reusable bootstrap resources zend con 2010

31
Reusable Bootstrap Resources Hector Virgen ZendCon 2010

Upload: hector-virgen

Post on 11-May-2015

5.227 views

Category:

Technology


1 download

DESCRIPTION

ZendCon 2010This session demonstrates how to build configurable and reusable bootstrap resources powered entirely by application.ini. Covers basic and advanced bootstrapping techniques, sharing bootstrap resources between applications, and accessing resources from within your applications.

TRANSCRIPT

Page 1: Reusable bootstrap resources   zend con 2010

Reusable Bootstrap ResourcesHector VirgenZendCon 2010

Page 2: Reusable bootstrap resources   zend con 2010

About Me

•Average, everyday PHP developer.•Zend PHP5 Certified Engineer•Used ZF since 1.0.2 (December 2007)•Developed web applications for

▫RealTown 2004-2008▫Houghton Mifflin 2008-2010▫Disney 2010-current

Page 3: Reusable bootstrap resources   zend con 2010

What is Bootstrapping?Or “What does footwear have to do with the interwebs?”

Page 4: Reusable bootstrap resources   zend con 2010

Typical Bootstrap Tasks

•Define constants•Set up include path•Set up autoloader•Initialize resources

▫Database connections▫Session▫Web service clients

Page 5: Reusable bootstrap resources   zend con 2010

Welcome Zend_ApplicationOr “Bootstrapping with Style”

Page 6: Reusable bootstrap resources   zend con 2010

Start with Zend_Tool$ zf create project quickstartCreating project at /Users/hevirgen/Web/projects/quickstart

Note: This command created a web project, for more information setting up your VHOST, please see docs/README

Page 7: Reusable bootstrap resources   zend con 2010

Your App’s Configuration[production]phpSettings.display_startup_errors = 0phpSettings.display_errors = 0includePaths.library = APPLICATION_PATH "/../library"bootstrap.path = APPLICATION_PATH "/Bootstrap.php"bootstrap.class = "Bootstrap"appnamespace = "Application"resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"resources.frontController.params.displayExceptions = 0

[staging : production]

[testing : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1

[development : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1resources.frontController.params.displayExceptions = 1

Page 8: Reusable bootstrap resources   zend con 2010

Bootstrap<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

{

}

Page 9: Reusable bootstrap resources   zend con 2010

Initializing ResourcesOr “Loading up your application’s dependencies”

Page 10: Reusable bootstrap resources   zend con 2010

Choose Your Path Wisely, Grasshopper

Bootstrap resources by:•Providing protected _init*() methods.•Using resource plugins.

Page 11: Reusable bootstrap resources   zend con 2010

_init*() Methods

•Coded directly in the application’s Bootstrap class.

•Return value is stored in bootstrap registry.

Page 12: Reusable bootstrap resources   zend con 2010

_init*() Methodsclass Bootstrap extends Zend_Application_Bootstrap_Bootstrap

{ protected function _initFoo() { $foo = new Foo(); return $foo; }}

Page 13: Reusable bootstrap resources   zend con 2010

Resource Plugin

•Each resource is initialized in its own class.

•Plugins are loaded in LIFO order.•Return value is stored in bootstrap

registry.

Page 14: Reusable bootstrap resources   zend con 2010

Resource Pluginclass MyResource_Foo extends Zend_Application_Resource_Abstract

{ public function init() { $foo = new Foo(); return $foo; }}

Page 15: Reusable bootstrap resources   zend con 2010

Ensure Resource Plugin is Loaded

[production]pluginPaths.My_Resource = APPLICATION_PATH “/resources”resources.foo[] =; orresources.foo.key = “value”

Page 16: Reusable bootstrap resources   zend con 2010

Accessing ResourcesOr “Gimme gimme gimme!”

Page 17: Reusable bootstrap resources   zend con 2010

Within an Action Controller$bootstrap = $this->getInvokeArg(‘bootstrap’);$foo = $bootstrap->getResource(‘foo’);

Page 18: Reusable bootstrap resources   zend con 2010

Within a Bootstrap Resource$bootstrap = $this->getBootstrap();$bootstrap->init(‘bar’);$bar = $bootstrap->getResource(‘bar’);

Page 19: Reusable bootstrap resources   zend con 2010

Statically AKA “Globally”$front = Zend_Controller_Front::getInstance();

$bootstrap = $front->getParam(‘bootstrap’);$foo = $bootstrap->getResource(‘foo’);

Page 20: Reusable bootstrap resources   zend con 2010

_init*() or Plugin?Which is better?

Page 21: Reusable bootstrap resources   zend con 2010

The Most Perfectest Bootstrapclass Bootstrap extends Zend_Application_Bootstrap_Bootstrap

{ // Empty!!}

Page 22: Reusable bootstrap resources   zend con 2010

Or Use ZF Bootstrap[production]bootstrap.path = APPLICATION_PATH "/../library/Zend/Application/Bootstrap/Bootstrap.php"

bootstrap.class = ”Zend_Application_Bootstrap_Bootstrap”

Page 23: Reusable bootstrap resources   zend con 2010

Configuring ResourcesOr “How to avoid hard-coding values”

Page 24: Reusable bootstrap resources   zend con 2010

_init*()[production]foo.bar = “bar”

[development : production]foo.bar = “derp”

Page 25: Reusable bootstrap resources   zend con 2010

_init*()protected function _initFoo(){ $options = $this->getOptions(); assert($options[‘foo’][‘bar’] == ‘derp’); // true}

Page 26: Reusable bootstrap resources   zend con 2010

Resource Plugin[production]resources.foo.bar = “bar”

[development : production]resources.foo.bar = “derp”

Page 27: Reusable bootstrap resources   zend con 2010

Resource PluginClass MyResource_Foo extends Zend_Application_Resource_Abstract

{ public function init() { $options = $this->getOptions(); assert($options[‘bar’] == ‘derp’); // true }}

Page 28: Reusable bootstrap resources   zend con 2010

Resource PluginClass MyResource_Foo extends Zend_Application_Resource_Abstract

{ protected $_bar;

public function setBar($bar) { $this->_bar = $bar; }

public function init() { assert($this->_bar == ‘derp’); // true }}

Page 29: Reusable bootstrap resources   zend con 2010

Why Plugins Are Awesomesauce•Reusable between projects•Easily unit-tested•Can be packaged with resource libraries•Can provide sane defaults•Loaded in LIFO order allows for

extending plugins

Page 30: Reusable bootstrap resources   zend con 2010

When Plugins Can Be Weaksauce•Performance concerns with loading a

class file for each resource plugin•Some editors (Notepad.exe) don’t support

opening multiple files

Page 31: Reusable bootstrap resources   zend con 2010

print(“all done!”);

http://[email protected]