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

C74bdcd3fa7c3d3f23290f46430b1463?s=128

Mehdi Lahmam B.

June 11, 2013
Tweet

Transcript

  1. 5.

    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. 7.

    function spam($emails) { $this->Email->subject = 'Welcome to our really cool

    thing'; $this->Email->replyTo = 'support@example.com'; $this->Email->from = 'Cool Web App <app@example.com>'; $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. 9.

    class User { function __construct($storage) { $this->storage = $storage; }

    // ... } $storage = new SessionStorage('SESSION_ID'); $user = new User($storage); Show that object who’s boss !
  4. 14.

    $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. 16.

    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. 17.

    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. 18.

    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. 20.

    The Dependency Injection Container in Symfony is managed by a

    class named sfServiceContainer. Fabien
  9. 21.

    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. 22.

    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. 24.

    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. 28.

    So ... we have Dependency Injection Managed by a Service

    Container Parametrized with XML data (or YAML for cool boys)
  13. 29.

    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. 30.

    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. 31.
  16. 34.

    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() { } }
  17. 36.
  18. 39.

    Connections::config([ 'legacy' => array( 'type' => 'database', 'adapter' => 'MySQL'

    ), 'new' => array( 'type' => 'database', 'adapter' => 'MongoDb' ), 'funcky' => array( 'type' => 'Http', 'adapter' => 'CouchDb' ) ]);
  19. 40.

    Cache::config(array( 'local' => array( 'adapter' => 'Apc', ), 'distributed' =>

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