The Okapi framework was initially developed by the Liip AG together for use at local.ch as thin layer mainly handling XSLT transformation and request mapping between the Java backend and the PHP based frontend. It has evolved since then but has stayed true its heritage of being the thinnest possible layer that combines various open source libraries. In this spirit the sfYaml component was integrated in version 1.1 of the framework. Okapi 2 has integrated various additional symfony components to further reduce the amount of code that has to be maintained in house, while at the same time giving Okapi a very modern architecture having adopted the event dispatcher, request handler, service container components. This sessions explains why we choose to integrate these components and how we went about implementing them and more importantly what lessons we learned along the way.
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
OkAPI meet symfony, symfony meet OkAPI
1. OkAPI meets Symfony
OkAPI meet symfony, symfony meet OkAPI
Lukas (lukas@liip.ch) | Jordi (jordi@liip.ch)
Symfony Live 2010 - Paris Feburary 15-17
Mittwoch, 17. Februar 2010
2. OkAPI meets Symfony
Who are we?
We have been doing PHP quite some time
Involved in PEAR, Doctrine, Dwoo, Phergie,
Arbit, core PHP development and a bit of
symfony .. and a few other things
Liip founded 3 years ago, winning awards
doing SOTA PHP/Flash apps ever since
Don’t worry we are happy staying in
Switzerland :-)
Mittwoch, 17. Februar 2010
3. OkAPI meets Symfony
Why OkAPI?
Developed internally at Liip, released as
OSS, but never tried to grow a community
Original code is quite old, written before
current crop of frameworks were mature
Designed for fat Java backend, lightweight
PHP/XSLT frontend architectures
Slowly turned into a lightweight PHP
framework glueing together all sorts of libs
Mittwoch, 17. Februar 2010
4. OkAPI meets Symfony
What is this about?
We are not here to sell OkAPI. It works for
us, but its not ready for the world in general
OkAPI2 uses most symfony components (*)
and we want to share our experience
Symfony benefits by someone testing the
concepts behind all the new shiny code
(*) OkAPI even uses a “non” component “ripped” from symfony,
but currently we are not using the Templating component
Mittwoch, 17. Februar 2010
5. OkAPI meets Symfony
Yaml
First component we added as a replacement
for spyc into OkAPI 1.1
Faster, much better error reporting, but
missing a key feature: merge key
So we submitted patches, Fabien rewrote
them, but we now have merge key :-)
Mittwoch, 17. Februar 2010
6. OkAPI meets Symfony
Merge-key rocks!
parameters:
# declare &arguments as a source
arguments: &arguments
routing: @routing
request: @request
response: @response
params: ¶ms
debug: true
services:
api_command_hello:
class: api_command_hello
# merge default parameters and change on the fly
arguments:
<<: *arguments
routing: @overrides_work
extra: "extra values are also allowed"
shared: true
Mittwoch, 17. Februar 2010
7. OkAPI meets Symfony
Step out, get burned
Someone did a OkAPI branch with
dependency injection support
We decided it would be better to reduce the
LOC to maintain/document ourselves
After a quick prototype integrating the
Service Container we were happy
Fabien announced the jump to 5.3, great
idea, but we still wanted pre 5.3 support
So we are back to maintaining the code :-/
Mittwoch, 17. Februar 2010
8. OkAPI meets Symfony
Service Container
A “service” layer to assist in implementing
dependency injection
Services and their dependencies can be
configured via Yaml (or XML) config files
Service Container generates a PHP class that
can make instances of any of the services
Big question:
How does the controller get
access to the services?
Mittwoch, 17. Februar 2010
9. OkAPI meets Symfony
General comments
Object creation needs to be cheap, lazy load
resources as much as possible
Get rid of the idea of a “flat config file”,
instead pass everything to the constructor
Use parameters to reuse settings across
multiple services
Use import to structure configs
Do not pass around the SC,
if necessary use multiple SC’s
Mittwoch, 17. Februar 2010
10. OkAPI meets Symfony
DI Frontcontroller
<?php
// load the init routine
require_once dirname(__FILE__).'/../inc/api/init.php';
$sc = api_init::createServiceContainer();
// load the controller
$ctrl = $sc->controller;
$ctrl->setServiceContainer($sc);
// let the controller load the modules
// which in turn load the model and view
$ctrl->run()->send();
Mittwoch, 17. Februar 2010
11. OkAPI meets Symfony
Command (“action”)
abstract class api_command {
public function __construct($routing, $request, $response, $params) {
$this->routing = $routing;
$this->route = $routing->getRoute();
$this->request = $request;
$this->response = $response;
$this->params = $params;
$this->response->command = $this;
$this->response->getDataCallback = array($this, 'getData');
$this->command = api_helpers_class::getBaseName($this);
}
public function process() {
$route = $this->route->getParams();
$method = isset($route['method'])
? $method = 'execute'.ucfirst($route['method']) : null;
if (empty($method)
|| !is_callable(array($this, $method))
) {
throw new api_exception('Incorrect method name’);
}
$this->$method();
return $this->response;
Mittwoch, 17. Februar 2010
13. OkAPI meets Symfony
Request Handler
100 LOC class using the Event component
Fires request, controller, view events to
structure your request handling
Featured in some of Fabien’s slide sets, it
has not yet been officially released
Obviously Event component can
also be used inside filters and
commands to handle custom events
Mittwoch, 17. Februar 2010
15. OkAPI meets Symfony
Example filter
class api_filter_language {
public function __construct($request, $response) {
$this->request = $request;
$this->response = $response;
}
public function request(sfEvent $event) {
if ($this->request->getLangDefaultUsed()
|| $this->request->getParam('lang')
) {
$url = $this->request->getUrl();
$host = $this->request->getHost();
$languages = $this->request->getLanguages();
// [...]
$this->response->setCookie('lang', $lang, $_SERVER
['REQUEST_TIME'] + 86400*365*5, '/');
// [...]
$this->response->redirect($url);
}
return true;
}
}
Mittwoch, 17. Februar 2010
16. OkAPI meets Symfony
Routing
Our own routing component didn't have
route generation
Integrating sfRouting took a couple hours
More features, less maintenance, some docs
Wrapping it means our route definition
didn't change much
Mittwoch, 17. Februar 2010
19. OkAPI meets Symfony
Merci! Questions?
Thank you for listening.
Lukas (lukas@liip.ch) | Jordi (jordi@liip.ch)
http://joind.in/talk/view/1410
Mittwoch, 17. Februar 2010
20. OkAPI meets Symfony
While you are listening
Structured database of UN resolutions:
http://code.google.com/p/uninformed/
Google map app for DJ’s around the world:
http://code.google.com/p/djtt/wiki/worldmap
Dwoo (at least as good as Twig):
http://dwoo.org/
PHP Issue tracker with Continuous Integration:
http://arbitracker.org
Mittwoch, 17. Februar 2010