Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

OkAPI meet symfony, symfony meet OkAPI


Published on

The Okapi framework was initially developed by the Liip AG together for use at 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.

  • Be the first to comment

OkAPI meet symfony, symfony meet OkAPI

  1. OkAPI meets Symfony OkAPI meet symfony, symfony meet OkAPI Lukas ( | Jordi ( 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: &params 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
  12. OkAPI meets Symfony Development Config imports: - { resource: default.yml, class: sfServiceContainerLoaderFileYaml } parameters: outputCaching: false debug: true services: writer: class: Zend_Log_Writer_apiFirebug arguments: [] shared: true logger: class: Zend_Log arguments: [@writer] shared: true log: class: api_log arguments: [@logger, DEBUG, true] shared: true solrClientSearch: class: api_solrClient arguments: options: hostname: localhost port: 8983 path: %solrPathSearch% # optional dependencies not yet supported in pre 5.3 log: @log shared: true 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
  14. OkAPI meets Symfony Filters using Event services: filter_language: class: api_filter_language arguments: [@request, @response] shared: true controller: class: api_controller arguments: # [...] events: application.request: controller: service: controller method: request language: service: @filter_language method: request 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
  17. OkAPI meets Symfony Routing // before $m->route('/api/chk/*url')->config( array( 'command'=>'liipto', 'method' => 'checkCode', 'view' => array('class' => 'json') ) ); // after $routing->route('chk', '/api/chk/:url', array( 'command'=>'liipto', 'method' => 'checkCode', 'view' => array('class' => 'json') ) ); Mittwoch, 17. Februar 2010
  18. OkAPI meets Symfony Classes loaded Example command: Symfony classes: api_command_hello sfEventDispatcher sfPatternRouting OkAPI classes: sfRequestHandler api_command sfRoute api_controller sfRouting api_helpers_class sfServiceContainer api_helpers_string sfServiceContainerBuilder api_init sfServiceContainerDumper api_request sfServiceContainerDumperInterface api_response sfServiceContainerDumperPhp api_routing sfServiceContainerInterface api_routing_route sfServiceContainerLoader api_routingcontainer sfServiceContainerLoaderFile api_views_common sfServiceContainerLoaderFileYaml api_views_php sfServiceContainerLoaderInterface sfServiceDefinition sfServiceReference sfYaml Mittwoch, 17. Februar 2010
  19. OkAPI meets Symfony Merci! Questions? Thank you for listening. Lukas ( | Jordi ( Mittwoch, 17. Februar 2010
  20. OkAPI meets Symfony While you are listening Structured database of UN resolutions: Google map app for DJ’s around the world: Dwoo (at least as good as Twig): PHP Issue tracker with Continuous Integration: Mittwoch, 17. Februar 2010
  21. OkAPI meets Symfony Resources Mittwoch, 17. Februar 2010