ZF2 Modular Architecture - Taking advantage of it
-
Upload
stefano-maraspin -
Category
Technology
-
view
111 -
download
2
description
Transcript of ZF2 Modular Architecture - Taking advantage of it
ZF2 Modular Architecture Taking Advantage of It
PHPDay – Verona, Italy – 18/05/2013
2
STEVE MARASPIN
3
http://friuli.grusp.org/
MODULES, WHAT?
Picture credits: http://www.flickr.com/photos/10459273@N05/4771563267
A Module is…
5
A collection of code and other files that solves a more specific atomic problem of the larger business problem.
ZF2 RFC
A re-usable piece of functionality that can be used to construct a more complex application.
6
Evan Coury - http://evan.pro/zf2-modules-talk.html
A Module is…
A re-usable piece of functionality that can be used to construct a more complex application.
7
Evan Coury - http://evan.pro/zf2-modules-talk.html
A Module is…
8
• 15.Reuse-in-the-small is a solved problem.
• 16.Reuse-in-the-large remains a mostly unsolved problem.
9
Robert R. Glass – Facts and Fallacies of Software Engineering
About Software Reuse
What I remember about ZF1 Modules…
10
Model
Admin Panel content management, store management, contact requests, ...
Web Site contents, product catalogue, contact form, …
What could we reuse?
11
Model
Admin Panel content management, store management, contact requests, ...
Web Site contents, product catalogue, contact form, …
Picture credits: http://www.flickr.com/photos/cclark395/7671665642/
MEET THE NICE FOLKS
AT OUR COMPANY
INFORMATION DESK
TECHNICAL OFFICE
Picture credits: http://www.flickr.com/photos/ter-burg/5807937726/
WAREHOUSE
Picture credits: http://www.flickr.com/photos/forklifttrainingadelaide/6774324457/
ACCOUNT MANAGER
17
Our Company Hierarchy
TECHNICAL OFFICE
ACCOUNT MGR.
WAREHOUSE
TECHNICAL OFFICE
ACCOUNT MGR.
INFORMATION DESK
DEPARTMENT ONE
DEPARTMENT TWO
18
Customer arrives…
TECHNICAL OFFICE
ACCOUNT MGR.
WAREHOUSE
TECHNICAL OFFICE
ACCOUNT MGR.
19
Information desk greets her
TECHNICAL OFFICE
ACCOUNT MGR.
WAREHOUSE
TECHNICAL OFFICE
ACCOUNT MGR.
20
…gives her directions…
WAREHOUSE
TECHNICAL OFFICE
ACCOUNT MGR. TECHNICAL OFFICE
ACCOUNT MGR.
21
Warehouse folks do things
TECHNICAL OFFICE
ACCOUNT MGR. TECHNICAL OFFICE
WAREHOUSE
ACCOUNT MGR.
22
Stuff is ready for delivery
TECHNICAL OFFICE
ACCOUNT MGR.
WAREHOUSE
TECHNICAL OFFICE
ACCOUNT MGR.
23
Customer is happy.
TECHNICAL OFFICE
ACCOUNT MGR.
WAREHOUSE
TECHNICAL OFFICE
ACCOUNT MGR.
24
Customer is happy. Eventually.
TECHNICAL OFFICE
ACCOUNT MGR.
WAREHOUSE
TECHNICAL OFFICE
ACCOUNT MGR.
25
But Hey…
This is MVC!
26
But Hey…
27
http://framework.zend.com/
28
Our Application
CONTROLLER
VIEW
MODEL
CONTROLLER
VIEW
29
Back to our company…
TECHNICAL OFFICE
ACCOUNT MGR.
WAREHOUSE
TECHNICAL OFFICE
ACCOUNT MGR.
INFORMATION DESK
30
We need to move a dept…
TECHNICAL OFFICE
ACCOUNT MGR.
WAREHOUSE
TECHNICAL OFFICE
ACCOUNT MGR.
31
Dependency problem!
TECHNICAL OFFICE
ACCOUNT MGR.
WAREHOUSE
TECHNICAL OFFICE
ACCOUNT MGR.
32
In our application…
CONTROLLER
VIEW
MODEL
CONTROLLER
VIEW
33
Same Problem!
CONTROLLER
VIEW
MODEL
CONTROLLER
VIEW
34
Solving company problem…
TECHNICAL OFFICE
ACCOUNT MGR.
WAREHOUSE
TECHNICAL OFFICE
ACCOUNT MGR.
INFORMATION DESK
WAREHOUSE
35
Same with our application…
CONTROLLER
VIEW
MODEL
CONTROLLER
VIEW
MODEL
36
Project delivered!
37
Let’s start a new one…
38
Hey, we need this thing!
39
No problems, folks!
MODEL
CONTROLLER
VIEW
40
http://framework.zend.com/
WELCOME ZF2 MODULES
Picture Credits: http://www.flickr.com/photos/10459273@N05/4771563267
42
Namespace Module.php Module
43
Namespace MioModulo;
Class Module {}
Namespace Module.php Module
Simplest Module:
WHAT COULD WE DO WITH IT?
Picture credits: http://www.flickr.com/photos/vittoriomilanes/8286559013
45
Format a date Handle a contact form
46
Format a date Handle a contact form
Handle ACLs Integrate an ORM
47
Format a date Handle a contact form
Handle ACLs Integrate an ORM
Encapsulate a whole application
WHAAAT?
Picture credits: http://www.flickr.com/photos/elzey/3481161467/
49
Format a date Handle a contact form
Handle ACLs Integrate an ORM
Encapsulate a whole application
Let’s think about ZF1 once again…
50
Model
Admin Panel content management, store management, contact requests, ...
Web Site contents, product catalogue, contact form, …
This is how modules are now…
51
Web Site
Model
Admin Panel
Content Management
Focus on features
52
Web Site
Model
Admin Panel
Product Catalogue
Contact Form
Content Management
Focus on features
53
Website
Model
Admin Panel
Product Catalogue
Contact Form
Content Management
What’s in a Module?
54
Feature
What’s in a module?
55
Feature
Controller
Views
PHP Classes
Routing
Configuration
…
Business Logic Hints
56
Feature
Controller
Viste
PHP Classes
Routing
Configurazioni
…
Services
Helpers / Plugins
Mappers
…
LET’S GET OUR HANDS ON IT…
Picture credits: http://www.flickr.com/photos/vittoriomilanes/8286559013
THE SKELETON APP
Picture credits: http://www.flickr.com/photos/fil/144232588/
59
Cloning
git clone
https://github.com/zendframework/ZendSkeletonApplication
60
http://getcomposer.org/
61
composer.json
{
"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.*"
}
}
62
composer.json
{
"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.*"
}
}
63
Installation
cd ZendSkeletonApplication
php composer.phar self-update
php composer.phar install
64
Installation
cd ZendSkeletonApplication
php composer.phar self-update
php composer.phar install
> Installing zendframework/zendframework
66
Filesystem Layout
67
Filesystem Layout
Application Wide Configuration
68
Filesystem Layout
Misc (cached files, documents …)
69
Filesystem Layout
Public DocRoot
70
Filesystem Layout
Our Modules
Filesystem Layout
71
Third Party Modules
Focusing on a module…
72
Module specific configuration
What’s inside a module?
73
Module PHP Source Files (Controller, Form, Model, Services, …)
What’s inside a module?
74
Module test files
What’s inside a module?
75
View stuff (view files, layouts, partials)
LET’S BOOTSTRAP SH*T UP!
Picture credits: http://www.flickr.com/photos/fil/144232588/
index.php <?php
/**
* This makes our life easier when dealing with paths. Everything is
* relative to the application root now.
*/
chdir(dirname(__DIR__));
// Setup autoloading
require 'init_autoloader.php';
// Run the application!
Zend\Mvc\Application::init(require 'config/application.config.php')->run();
index.php <?php
/**
* This makes our life easier when dealing with paths. Everything is
* relative to the application root now.
*/
chdir(dirname(__DIR__));
// Setup autoloading
require 'init_autoloader.php';
// Run the application!
Zend\Mvc\Application::init(require 'config/application.config.php')->run();
config/application.config.php
79
return array(
'modules' => array(
'DoctrineModule',
'DoctrineORMModule',
'SpeckPaypal',
'Application',
),
),
...
Module.php namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{[…]}
public function getConfig() {
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig() {}
}
80
Module configuration file // config/module.config.php
return array(
'router' => array(
'routes' => array(...)
),
'controllers' => array(...),
'service_manager' => array(...),
'view_manager' => array(...),
...
)
Where’s module configuration?
82
Here!
Controllers <?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
$timestamp = time();
return new ViewModel(array(
'timestamp' => $timestamp
));
}
}
Actions <?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
$timestamp = time();
return new ViewModel(array(
'timestamp' => $timestamp
));
}
}
CONVENTION
OVER
CONFIGURATION
Picture credits: http://www.flickr.com/photos/29233640@N07/8412347937/
This is for all modules
86
return array(
'modules' => array(
'DoctrineModule',
'DoctrineORMModule',
'SpeckPaypal',
'Application',
),
),
...
config/application.config.php
87
Feature A
Controller
Views
PHP Classes
Routing
Configurations
…
88
Feature A
Controller
Views
PHP Classes
Routing
Configurations
…
Feature B
Controller
Views
PHP Classes
Routing
Configurations
…
Feature C
Controller
Views
PHP Classes
Routing
Configurations
…
89
Controller
Views
PHP Classes
Routing
Configurations
…
Controller
Views
PHP Classes
Routing
Configurations
…
Controller
Views
PHP Classes
Routing
Configurations
…
Modules are merged at runtime
CONFIGURATION MERGE
Picture credits: http://www.flickr.com/photos/michelleundihrefotos/5970015736
91
Controller
Views
Routing
…
Controller
Views
Routing
…
Controller
Views
Routing
…
Modules are merged at runtime
PHP Classes PHP Classes PHP Classes
Configurations
We can override things… Views / Layouts
92
return array(
...
'view_manager' => array(
...
),
)
We can override things… Views / Layouts Routing Rules
93
return array(
...
'router' => array(
...
),
)
We can override things… Views / Layouts Routing Rules All Other Configuration Parameters
94
We can override things… Views / Layouts Routing Rules All Other Configuration Parameters
95
Do NOT modify original 3° party
modules!
Custom Configuration
96
Do not touch anything here!
MODULE.local.php
97
Controller
Views
Routing
…
Controllers
Views
Routing
…
Controller
Views
Routing
…
Modules are merged at runtime
PHP Classes PHP Classes PHP Classes
Configurations Configurations Configurations
This happens for all modules
98
return array(
'modules' => array(
'DoctrineModule',
'DoctrineORMModule',
'SpeckPaypal',
'Application',
),
),
...
config/application.config.php
Order is Important!
LET’S LOOK AROUND FIRST! Picture credits: http://www.flickr.com/photos/billward/5626976800
http://modules.zendframework.com/
100
What about a contact form?
101
What about a contact form?
102
cd vendor
git clone
https://github.com/weierophinney/PhlyContact.git
103
104
Here we go…
application.config.php <?php
return array(
'modules' => array(
'PhlyContact',
'Application',
),
'module_listener_options' => array(
[…]
),
);
105
application.config.php <?php
return array(
'modules' => array(
'PhlyContact',
'Application',
),
'module_listener_options' => array(
[…]
),
);
106
107
Default Configuration
PhlyContact/config/module.config.php <?php
return array(
'phly_contact' => array(
[…]
'message' => array(
'to' => array('EMAIL HERE' => 'NAME HERE',),
'sender' => array(
'address' => 'EMAIL HERE',
'name' => 'NAME HERE',
),
'from' => array('EMAIL HERE' => 'NAME HERE',),
),
[…]
),
108
109
Remember: Do NOT modify files in here!
110
DO create and use files herein instead
In less than 2 minutes…
111
BUILDING OUR OWN MODULES
Picture credits: http://www.flickr.com/photos/pullip_junk/6624713631
SHARED RESOURCES
Picture credits: http://www.flickr.com/photos/nasahqphoto/5162244810
Catalogue
114
Shopping Cart
115
Product
Catalogue Shopping Cart
Where shall we put this?
- id - name - price
Little Information Needed
116
117
Shopping Cart
Product - id - name - price
We need a bit more stuff here…
118
Catalogue
119
Shopping Cart
Product - id - name - price
Product
- id - name - price - description - picturePath
Catalogue
120
Shopping Cart
- description - picturePath - …
Product - id - name - price
Product - id - name - price
121
- description - picturePath - …
Product - id - name - price
Catalogue
Shopping Cart
/**
* Catalogue\Entity\Product
* ...
*/
class Product extends \Ecommerce\Entity\Cartproduct {}
Through extension
/**
* Catalogue\Entity\Product
* ...
*/
class Product implements \Ecommerce\Entity\CartproductInterface {}
Through Interfaces
Constraints
How do I make this possible?
123
SERVICE MANAGER
Picture credits: http://www.flickr.com/photos/urbanwoodswalker/4375401057/
Service Locator
SL Product
Controller
ShoppingCart
NOT THE REGISTRY
In a Controller Factory <?php
public function createService(ServiceLocatorInterface $services){
$serviceLocator = $services->getServiceLocator();
$cartService = $serviceLocator->get('CartItem');
$controller = new CartController($cartService);
return $controller;
}
}
Original Module Configuration /* Shopping Cart Module – module.config.php */
return array(
'router' => array(…),
'controllers' => array(…),
'service_manager' => array(
'invokables' => array(
'CartItem' => 'Cart\Service\CartItem',
),
'factories' => array(…)
)
);
Module Configuration /* Shopping Cart Module – module.config.php */
return array(
'router' => array(…),
'controllers' => array(…),
'service_manager' => array(
'invokables' => array(
// 'CartItem' => 'Cart\Service\CartItem',
'CartItem' => 'Catalogue\Service\CatalogueItem',
),
'factories' => array(…)
)
);
130
LSP in Action
Catalogue
Shopping Cart
Product - id - name - price
Product - id - name - price - description - picturePath
131
Catalogue
Shopping Cart
Product - id - name - price - description - picturePath
Product - id - name - price - description - picturePath
REUSE ENABLED
Picture credits: http://www.flickr.com/photos/89544908@N00/7877685112/
REUSE ENABLED
EVEN IN THE LARGE
Picture credits: http://www.flickr.com/photos/89544908@N00/7877685112/
134
A re-usable piece of functionality that can be used to construct a more complex application.
135
Evan Coury - http://evan.pro/zf2-modules-talk.html
Back to definition
A re-usable piece of functionality that can be used to construct a more complex application.
136
Evan Coury - http://evan.pro/zf2-modules-talk.html
Now I understand it better!
I hope you too.
Stefano Maraspin @maraspin [email protected]
QUESTIONS?
Picture credits: http://www.flickr.com/photos/hefhoover/3929261255/
139
@maraspin
140
http://www.mvlabs.it/
Stefano Maraspin @maraspin [email protected]
http://joind.in/talk/view/8655