Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why Lithium Sucks...

Why Lithium Sucks...

... Less than your Framework

Inspired by @nateabele and @jperras talk: http://www.slideshare.net/nateabele/lithium-the-framework-for-people-who-hate-frameworks

Mehdi Lahmam B.

June 11, 2013
Tweet

More Decks by Mehdi Lahmam B.

Other Decks in Programming

Transcript

  1. function initialize(&$controller, $settings = []) { //... $prefixes = Router::prefixes();

    if (!empty($prefixes)) { foreach ($prefixes as $prefix) { // do something with the global state ... } } if (Configure::read() > 0) { App::import('Debugger'); Debugger::checkSecurityKeys(); } } Sucks Sucks hard !
  2. function spam($emails) { $this->Email->subject = 'Welcome to our really cool

    thing'; $this->Email->replyTo = '[email protected]'; $this->Email->from = 'Cool Web App <[email protected]>'; $this->Email->template = 'simple_message'; $this->Email->sendAs = 'both'; foreach ($emails as $email) { $this->Email->to = $email['address']; $this->Email->subject = "Special offer for {$email['name']}"; $this->Email->send(); } } Sucks
  3. class User { function __construct($storage) { $this->storage = $storage; }

    // ... } $storage = new SessionStorage('SESSION_ID'); $user = new User($storage); Show that object who’s boss !
  4. $transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', array( 'auth' => 'login', 'username' =>

    'foo', 'password' => 'bar', 'ssl' => 'ssl', 'port' => 465, )); $mailer = new Zend_Mail(); $mailer->setDefaultTransport($transport);
  5. class Container { public function getMailTransport() { return new Zend_Mail_Transport_Smtp('smtp.gmail.com',

    array( 'auth' => 'login', 'username' => 'foo', 'password' => 'bar', 'ssl' => 'ssl', 'port' => 465, )); } public function getMailer() { $mailer = new Zend_Mail(); $mailer->setDefaultTransport($this->getMailTransport()); return $mailer; } }
  6. class Container { public function getMailTransport() { return new Zend_Mail_Transport_Smtp('smtp.gmail.com',

    array( 'auth' => 'login', 'username' => 'foo', 'password' => 'bar', 'ssl' => 'ssl', 'port' => 465, )); } public function getMailer() { $mailer = new Zend_Mail(); $mailer->setDefaultTransport($this->getMailTransport()); return $mailer; } } $container = new Container(); $mailer = $container->getMailer();
  7. class Container { protected $parameters = array(); public function __construct(array

    $parameters = array()) { $this->parameters = $parameters; } public function getMailTransport() { return new Zend_Mail_Transport_Smtp('smtp.gmail.com', array( 'auth' => 'login', 'username' => $this->parameters['mailer.username'], 'password' => $this->parameters['mailer.password'], 'ssl' => 'ssl', 'port' => 465, )); } public function getMailer() { $mailer = new Zend_Mail(); $mailer->setDefaultTransport($this->getMailTransport()); return $mailer; } }
  8. The Dependency Injection Container in Symfony is managed by a

    class named sfServiceContainer. Fabien
  9. class Container extends sfServiceContainer { static protected $shared = array();

    protected function getMailTransportService() { return new Zend_Mail_Transport_Smtp('smtp.gmail.com', array( 'auth' => 'login', 'username' => $this['mailer.username'], 'password' => $this['mailer.password'], 'ssl' => 'ssl', 'port' => 465, )); } protected function getMailerService() { if (isset(self::$shared['mailer'])) { return self::$shared['mailer']; } $class = $this['mailer.class']; $mailer = new $class(); $mailer->setDefaultTransport($this->getMailTransportService()); return self::$shared['mailer'] = $mailer; } }
  10. Of course now you want to abstract the crap out

    of the Service Container. http://fabien.potencier.org/article/13/introduction-to-the-symfony-service-container
  11. require_once 'PATH/TO/sf/lib/sfServiceContainerAutoloader.php'; sfServiceContainerAutoloader::register(); $sc = new sfServiceContainerBuilder(); $sc-> register('mail.transport', 'Zend_Mail_Transport_Smtp')->

    addArgument('smtp.gmail.com')-> addArgument(array( 'auth' => 'login', 'username' => '%mailer.username%', 'password' => '%mailer.password%', 'ssl' => 'ssl', 'port' => 465, ))-> setShared(false) ; $sc-> register('mailer', '%mailer.class%')-> addMethodCall('setDefaultTransport', array(new sfServiceReference('mail.transport'))) ;
  12. So ... we have Dependency Injection Managed by a Service

    Container Parametrized with XML data (or YAML for cool boys)
  13. So ... we have Dependency Injection Managed by a Service

    Container Parametrized with XML data (or YAML for cool boys) And the whole thing configured by a Builder
  14. So ... we have Dependency Injection Managed by a Service

    Container Parametrized with XML data (or YAML for cool boys) And the whole thing configured by a Builder ... to fix one problem.
  15. namespace app\foo; use lithium\util\String; use lithium\util\Collection; class Foo extends \lihtium\core\Object

    { protected $_classes = [ 'logger' => 'lithium\analysis\Logger', 'cache' => 'lithium\storage\Cache' ]; public function __construct(array $config = []) { //... } protected function _init() { } }
  16. Connections::config([ 'legacy' => array( 'type' => 'database', 'adapter' => 'MySQL'

    ), 'new' => array( 'type' => 'database', 'adapter' => 'MongoDb' ), 'funcky' => array( 'type' => 'Http', 'adapter' => 'CouchDb' ) ]);
  17. Cache::config(array( 'local' => array( 'adapter' => 'Apc', ), 'distributed' =>

    array( 'adapter' => 'Memcached', 'servers' => ['127.0.0.1'] ), 'default' => array( 'adapter' => 'File', ) ));