Download - Decouple your framework now, thank me later
![Page 1: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/1.jpg)
DECOUPLE YOUR FRAMEWORK NOW, THANK ME LATERMICHELANGELO VAN DAM - @DRAGONBE
![Page 2: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/2.jpg)
MICHELANGELO VAN DAM
CEO at in2it Lead PHP architect Community leader Coach & mentor FOSS contributor Public speaker
![Page 3: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/3.jpg)
FRAMEWORKS ARE GREAT!
• They abstract common tasks like email, database connectivity, routing and a ton more…
• They allow us to quickly develop complex applications
• They offer good to great security and robustness
![Page 4: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/4.jpg)
BUT…
• Frameworks have a nasty aftertaste when building business logic
• You need to use their database, log or cache adapter everywhere
• View templates are requiring framework components like translations, escaping and other trivial purposes
• Best practices require to use modules, bundles or features to separate business logic components
![Page 5: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/5.jpg)
IMPROVEMENTS THROUGH STANDARDS
• The PHP-FIG standards motivates frameworks to use great components to abstract functionality and ensure interoperability with other frameworks and tools
• Major frameworks already offer this straight off the bat
• But add their own “secret sauce” to link it within their framework, even when using Dependency Injection
![Page 6: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/6.jpg)
WHAT ARE THE CHALLENGES?
![Page 7: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/7.jpg)
UPGRADE (OR CHANGE) YOUR FRAMEWORK
![Page 8: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/8.jpg)
UPGRADE TO THE LATEST PHP VERSION
![Page 9: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/9.jpg)
BUSINESS LOGIC AND FRAMEWORKS MIXED
Framework X Business Logic
BL DB
FW DB
FW Logging
FW Mail
FW Service
BL Logging
BL Mail
BL Services
![Page 10: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/10.jpg)
APPLYING INTERFACES IN BETWEEN
Framework X Business Logic
FW DB
FW Logging
FW Mail
FW Service
BL DB
BL Logging
BL Mail
BL Services
DB Interface
Logging Interface
Mail Interface
Service Interface
![Page 11: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/11.jpg)
RESTAURANT PRINCIPLE
![Page 12: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/12.jpg)
THE HOSTESWILL BRING YOU TO YOUR TABLE AND GIVES YOU THE MENU AND WINE LIST.
![Page 13: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/13.jpg)
A WAITERWILL TAKE YOUR ORDER FOR DRINKS AND FOOD
![Page 14: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/14.jpg)
BARTENDERWILL PREPARE YOUR DRINKS
![Page 15: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/15.jpg)
KITCHENWILL PREPARE YOUR MEAL
![Page 16: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/16.jpg)
YOUR WAITERBRINGS YOUR DRINKS
![Page 17: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/17.jpg)
YOUR WAITERWILL DELIVER YOUR FOOD
![Page 18: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/18.jpg)
YOUR WAITERWILL GIVE YOU THE BILL WHEN DONE
![Page 19: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/19.jpg)
![Page 20: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/20.jpg)
YOUR WAITER
• Interfaces with the hostess to get started
• Interfaces with you to take your order
• Interfaces with the bar for drinks
• Interfaces with the cash register to present you a bill
![Page 21: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/21.jpg)
YOUR WAITER
• Receives notification you have arrived at your table
• Receives your order from you
• Receives drinks from the bartender
• Receives food from the kitchen
• Receives money from you
![Page 22: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/22.jpg)
HOW DO WE DO THIS IN CODE?
![Page 23: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/23.jpg)
INTERFACESDESIGN BY CONTRACTS
![Page 24: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/24.jpg)
WHAT ARE INTERFACES
• Interfaces define a requirement without concrete implementation
• There’s no limit on interfaces implemented
• Everyone understands the “contract” immediately
• One interface per goal, feature or purpose
![Page 25: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/25.jpg)
COMMON INTERFACES IN PHP
• Countable
• Iterator (and derivates)
• See language.oop5.interfaces on php.net for more details!
![Page 26: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/26.jpg)
CUSTOM INTERFACES
interface TableGatewayInterface { public function find(int $id): array;
public function fetchRow( array $where = [], array $order = [] ): array;
public function fetchAll( array $where = [], array $order = [], int $count = 0, int $offset = 0 ): array;
public function insert(array $data): int;
public function update(array $data, array $where = []): int; }
![Page 27: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/27.jpg)
BONUS: VERY TESTABLE!!!
• No need to implement concrete code, just use interfaces to guide your development
• A class can implement multiple interfaces, testing can occur on a single interface functionality at a time.
![Page 28: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/28.jpg)
EVENTS
![Page 29: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/29.jpg)
WHAT ARE EVENTS?
• Events allow us to run tasks in the background and call back when completed.
• Implements the observer pattern (e.g. SplSubject & SplObserver)
• One observer can have many subscribers
• A subscriber can subscribe to many observer objects
![Page 30: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/30.jpg)
EXAMPLE
$memberService = new MemberService(); $memberService->attach(new DBObserver()); $memberService->attach(new LogObserver()); $memberService->attach(new EmailObserver()); $memberService->attach(new CacheObserver()); $memberService->attach(new SearchObserver()); $memberService->register(new Member('John', 'Doe', '[email protected]'));
![Page 31: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/31.jpg)
EVENT OBSERVATION
Register
DB
Log
Cache
Search
Storing in DB
Logging
Sending email
Write to cache
Update indexes
![Page 32: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/32.jpg)
EVENT OBSERVATION AFTER EVENTS
Register
DB
Log
Cache
Search
Storing in DB
Logging
Sending email
Write to cache
Update indexes
![Page 33: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/33.jpg)
BENEFITS
• Reusable logic
• Runs in the background, so no delays
• Scalable
![Page 34: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/34.jpg)
GLUING ALL TOGETHER
![Page 35: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/35.jpg)
Core Business Logic
Silex Web Frontend
Apigility API
Phalcon Web Backend
![Page 36: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/36.jpg)
Core Business Logic
Slim Web Frontend
Zend Expressive
API
Python Web Backend
![Page 37: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/37.jpg)
GLUE CLASS TO INTERACT 1/3
class SilexServiceProvider implements ServiceProviderInterface { public function register(Container $container) { $dsn = $container['cb_config_db.dsn'] ?: ''; $username = $container['cb_config_db.username'] ?: ''; $password = $container['cb_config_db.password'] ?: ''; $pdo = new \PDO($dsn, $username, $password);
$authorTable = new AuthorTable($pdo); $authorHydrator = new AuthorHydrator(); $author = new Author();
$bookTable = new BookTable($pdo); $bookHydrator = new BookHydrator(); $book = new Book(); $memberTable = new MemberTable($pdo); $memberHydrator = new MemberHydrator(); $member = new Member();
![Page 38: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/38.jpg)
GLUE CLASS TO INTERACT 2/3
$serviceLocator = new ServiceLocator(); $serviceLocator ->set('\Cloudbooks\Author\Model\AuthorTable', $authorTable) ->set('\Cloudbooks\Author\Model\AuthorHydrator', $authorHydrator) ->set('\Cloudbooks\Author\Entity\Author', $author) ->set('\Cloudbooks\Book\Model\BookTable', $bookTable) ->set('\Cloudbooks\Book\Model\BookHydrator', $bookHydrator) ->set('\Cloudbooks\Book\Entity\Book', $book) ->set('\Cloudbooks\Member\Model\MemberTable', $memberTable) ->set('\Cloudbooks\Member\Model\MemberHydrator', $memberHydrator) ->set('\Cloudbooks\Member\Entity\Member', $member); }
![Page 39: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/39.jpg)
GLUE CLASS TO INTERACT 3/3
$container['cb_author_service'] = $container->factory(function () use ($serviceLocator) { $serviceFactory = new AuthorServiceFactory(); return $serviceFactory->createService($serviceLocator); }); $container['cb_book_service'] = $container->factory(function () use ($serviceLocator) { $serviceFactory = new BookServiceFactory(); return $serviceFactory->createService($serviceLocator); }); $container['cb_member_service'] = $container->factory(function () use ($serviceLocator) { $serviceFactory = new MemberServiceFactory(); return $serviceFactory->createService($serviceLocator); }); }
![Page 40: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/40.jpg)
LET’S RECAP
![Page 41: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/41.jpg)
![Page 43: Decouple your framework now, thank me later](https://reader035.vdocument.in/reader035/viewer/2022062412/587f3c911a28ab43318b4dd5/html5/thumbnails/43.jpg)
THANK YOU!