Matthew Weier O'Phinney
AFUP PHP Tour
Nantes, France
November 2012
$events = $someObject->getEventManager();
$events->attach('do', function ($e) {
$event = $e->getName(); // "do"
$target = $e->getTarget(); // $someObject!
$params = $e->getParams();
printf('Target: %s; event: %s; parameters: %s',
$target, get_class($event), json_encode($params)
);
});
// in class "SomeObject"
public function do($some, $thing) {
$params = compact($some, $thing);
$this->getEventManager()->trigger(
__FUNCTION__, $this, $params
);
}
$someObject->do('foo', 'bar');
// Target: SomeObject; event: do; parameters: {
// "some": "foo",
// "thing": "bar"
// }
services
)invokables
)factories
)abstract_factories
)aliases
)initializers
)// Programmatically
$services->setService('foo', $fooInstance);
// Configuration
array('services' => array(
'foo' => new Foo(),
));
// Programmatically
$services->setInvokableClass('foo', 'Foo');
// Configuration
array('invokables' => array(
'foo' => 'Foo',
));
// Programmatically
$services->setFactory('foo', $callableOrFactory);
// Configuration
array('factories' => array(
'foo' => function ($services) {
$dep = $services->get('Dependency');
return new Foo($dep);
},
'bar' => 'Some\Static::method',
'baz' => 'Class\Implementing\FactoryInterface',
'bat' => 'Class\Implementing\Invoke',
));
// Programmatically
$services->addAbstractFactory(
$abstractFactoryOrClassName
);
// Configuration
array('abstract_factories' => array(
'Class\Implementing\AbstractFactoryInterface',
));
// Interface methods:
public function canCreateServiceWithName(
$services, $name, $rName);
public function createServiceWithName(
$services, $name, $rName);
// Programmatically
$services->setAlias('my_foo', 'foo');
// Configuration
array('aliases' => array(
'my_foo' => 'foo', // alias services
'foo_master' => 'my_foo', // alias aliases!
));
// Programmatically
$services->addInitializer($callback);
// Configuration
array('initializers' => array(
$instance,
$callback,
'Class\Implementing\InitializerInterface',
'Class\Implementing\Invoke',
));
function ($instance, $services) {
if (!$instance instanceof SomeInterface) {
return;
}
$instance->setSomeDep($services->get('Dep'));
}
Everything is an event
/foo
/literal/:id[/:optional]
/literal(?P<id>[a-f0-9]{8})
Controllers are Services
They look like ZF1 controllers
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class HelloController extends AbstractActionController
{
public function worldAction()
{
return new ViewModel(array(
'vars' => 'to inject',
));
}
}
Inject dependencies
class HelloController extends AbstractActionController
{
protected $dependency;
public function setDependency(Dependency $dep) {
$this->dependency = $dep
}
public function worldAction() {
$result = $this->dependency->doSomething();
return array('work' => $result);
}
}
Define service factories to inject dependencies
function ($controllers) {
$services = $controllers->getServiceLocator();
$dependency = $services->get('Dependency');
$controller = new MyController();
$controller->setDependency($dependency);
return $controller;
}
Anything
ServiceManager
(and plugin manager)
configurations from Module classes and/or module configurationnamespace My;
class Module {
public function getAutoloaderConfig() {
// return config for autoloader factory
}
public function getConfig() {
return include
__DIR__ . '/config/module.config.php';
}
public function onBootstrap($e) {
// do some initialization
}
}
curl -s https://getcomposer.org/installer | php --
php composer.phar create-project \
> --repository-url="http://packages.zendframework.com" \
> zendframework/skeleton-application \
> path/to/install
{
"minimum-stability": "dev",
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.*",
"zendframework/zendservice-recaptcha": "2.0.*",
"phly/phly-mongo": "dev-master",
"phly/phly-paste": "1.*",
"socalnick/scn-social-auth": "dev-master"
}
}
config/
application.config.php
autoload/
global.php
local.php
...
// application.config.php
return array(
'modules' => array(
'Application',
'EdpMarkdown',
'PhlyMongo',
'PhlyPaste',
'ScnSocialAuth',
'ZfcBase',
'ZfcUser',
),
// ...
);
module/
My/
Module.php
src/
Module/
Controller/
HelloController.php
config/
module.config.php
test/
view/
my/
hello/
world.phtml
http://packages.zendframework.com/
http://framework.zend.com/downloads
http://framework.zend.com/participate
https://github.com/zendframework/zf2
Table of contents | t |
---|---|
Exposé | ESC |
Autoscale | e |
Full screen slides | f |
Presenter view | p |
Source files | s |
Slide numbers | n |
Blank screen | b |
Notes | 2 |
Help | h |