Last active
October 12, 2016 13:47
-
-
Save mpratt/5399005 to your computer and use it in GitHub Desktop.
Revisions
-
Michael Pratt revised this gist
Apr 16, 2013 . 2 changed files with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ public function __construct($file) { $this->file = $file; } public function log($message) { $message = date('H:i:s ') . $message . PHP_EOL; return file_put_contents($this->file, $message, FILE_APPEND); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,10 +15,10 @@ class Usuario } $user = new Usuario(); if ($user->isLogged()) { $user->setLogger(new DatabaseLogger(DSN, USER, PASSWORD)); $user->logAction('Se enlogó correctamente'); } ?> -
Michael Pratt revised this gist
Apr 16, 2013 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,4 +15,14 @@ public function __construct() $container = new MyContainer(); $a = $container['a']; echo $a->calculate(12345678); /** * Vale la pena aclarar que Pimple, a diferencia * de la mayoria de IoCs, por defecto, siempre crea * una instancia nueva del objeto que se pida. * * Para modificar ese comportamiento, se debe usar * el metodo share(); y especificar ahí la construccion * de los objetos. */ ?> -
Michael Pratt revised this gist
Apr 16, 2013 . 5 changed files with 30 additions and 195 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,81 +1,41 @@ <?php class Logger { protected $file; public function __construct($file) { $this->file = $file; } public function log($message) { $message = date('H:i:s - ') . $message . PHP_EOL; return file_put_contents($this->file, $message, FILE_APPEND); } } class Usuario { protected $logger; public function __construct() { // Estamos instanciando el Logger dentro de la clase $this->logger = new Logger('/usuario.log'); } public function logAction($message) { $name = $this->getUserName(); return $this->logger->log($name . ': ' . $message); } // Haz de cuenta que aquí van un par de métodos más } $user = new Usuario(); if ($user->isLogged()) { $user->logAction('Se enlogó correctamente'); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,102 +1,37 @@ <?php // Definimos una interface, con el metodo que // es necesario para nuestra clase dependiente. interface ILogger { public function log($message); } // Implementamos la interface en la dependencia class Logger implements ILogger { // Basicamente el mismo contenido que en // el ejemplo pasado } class Usuario { protected $logger; // Usamos el typehint ILogger para forzar que // la dependencia debe implementar esa interface. public function __construct(ILogger $logger) { $this->logger = $logger; } // Haz de cuenta que aquí van un par de métodos más } // Fijate que la dependencia se esta inyectando desde afuera. $user = new Usuario(new Logger('/archivo.log')); if ($user->isLogged()) { $user->logAction('Se enlogó correctamente'); } ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,74 +1,29 @@ <?php // Esta nueva clase implementa la interface ILogger class DatabaseLogger implements ILogger { public function __construct($dsn, $user, $password) { $this->pdo = new PDO($dsn, $user, $password); } public function log($message) { $stmt = $this->pdo->prepare('INSERT INTO table (date, message) VALUES (?,?)'); return $stmt->execute(array(date('H:i:s'), $message)); } } /** * mira con que facilidad se puede intercambiar la dependencia * siempre y cuando el nuevo objeto siga la interface pactada. */ $databaseLogger = new DatabaseLogger(DSN, USER, PASSWORD); $user = new Usuario($databaseLogger); if ($user->isLogged()) { $user->logAction('Se enlogó correctamente'); } ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,36 +3,22 @@ class Usuario { protected $logger; // Ahora no usamos el constructor, sino que definimos // un metodo para la inyección. public setLogger(ILogger $logger) { $this->logger = $logger; } // Haz de cuenta que aquí van un par de métodos más } $user = new Usuario(); $user->setLogger(new DatabaseLogger(DSN, USER, PASSWORD)); if ($user->isLogged()) { $user->logAction('Se enlogó correctamente'); } ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,15 +5,14 @@ class MyContainer extends Pimple { public function __construct() { $this['c'] = function () { return new C(); }; $this['d'] = function () { return new D(); }; $this['b'] = function ($c) { return new B($c['c'], $c['d']); }; $this['a'] = function ($c) { return new A($c['b']); }; } } $container = new MyContainer(); $a = $container['a']; echo $a->calculate(12345678); ?> -
Michael Pratt revised this gist
Apr 16, 2013 . No changes.There are no files selected for viewing
-
Michael Pratt revised this gist
Apr 16, 2013 . No changes.There are no files selected for viewing
-
Michael Pratt revised this gist
Apr 16, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,7 +40,7 @@ public function __construct($dsn, $user, $password) */ public function log($message) { $stmt = $this->pdo->prepare('INSERT INTO table (date, message) VALUES (?,?)'); return $stmt->execute(array(date('H:i:s'), $message)); } -
Michael Pratt created this gist
Apr 16, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,82 @@ <?php /** * Clase Logger */ class Logger { protected $file; /** * Constructor de la clase * * @param string $file El archivo en donde se va a escribir * @return void */ public function __construct($file) { $this->file = $file; } /** * Metodo para escribir en un archivo * * @param string $message El mensaje a escribir * @return bool */ public function log($message) { return file_put_contents($this->file, date('H:i:s - ') . $message . PHP_EOL, FILE_APPEND); } } /** * Clase Usuario */ class Usuario { protected $logger; /** * Constructor de la clase * * @return void */ public function __construct() { $this->logger = new Logger('/usuario.log'); } /** * Metodo para guardar un mensaje del usuario * * @param string $message El mensaje a guardar * @return bool */ public function logAction($message) { $name = $this->getUserName(); return $this->logger->log($name . ': ' . $message); } // Aquí van un par de métodos más ... ... ... ... } /** * Así Funcionaría: */ $user = new Usuario(); if ($user->isLogged()) { ... ... ... ... $user->logAction('Se enlogó correctamente'); } ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,102 @@ <?php /** * Interface ILogger */ interface ILogger { /** * Metodo para escribir en un archivo * * @param string $message El mensaje a escribir * @return bool */ public function log($message); } /** * Clase Logger */ class Logger implements ILogger { protected $file; /** * Constructor de la clase * * @param string $file El archivo en donde se va a escribir * @return void */ public function __construct($file) { $this->file = $file; } /** * Metodo para escribir en un archivo * * @param string $message El mensaje a escribir * @return bool */ public function log($message) { return file_put_contents($this->file, date('H:i:s - ') . $message . PHP_EOL, FILE_APPEND); } } /** * Clase Usuario */ class Usuario { protected $logger; /** * Constructor de la clase * * @param object ILogger Instancia de objeto que sigue la * interface ILogger * @return void */ public function __construct(ILogger $logger) { $this->logger = $logger; } /** * Metodo para guardar un mensaje del usuario * * @param string $message El mensaje a guardar * @return bool */ public function logAction($message) { $name = $this->getUserName(); return $this->logger->log($name . ': ' . $message); } // Aquí van un par de métodos más ... ... ... ... } /** * Así Funcionaría: * Notese que la dependencia se esta inyectando * desde afuera. */ $user = new Usuario(new Logger('/archivo.log')); if ($user->isLogged()) { ... ... ... ... $user->logAction('Se enlogó correctamente'); } ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ <?php /** * Interface ILogger */ interface ILogger { /** * Metodo para escribir en un archivo * * @param string $message El mensaje a escribir * @return bool */ public function log($message); } /** * Clase Logger */ class DatabaseLogger implements ILogger { /** * Constructor de la clase * * @param string $dsn * @param string $user * @param string $password * @return void */ public function __construct($dsn, $user, $password) { $this->pdo = new PDO($dsn, $user, $password) } /** * Metodo para escribir en un archivo * * @param string $message El mensaje a escribir * @return bool */ public function log($message) { $stmt = $this->pdo->prepare('INSERT INTO table (message, date) VALUES (?,?)'); return $stmt->execute(array(date('H:i:s'), $message)); } } class Usuario { ... ... ... } /** * Así Funcionaría: * Notese que con facilidad se puede intercambiar la dependencia * siempre y cuando el nuevo objeto siga la interface pactada. */ $databaseLogger = new DatabaseLogger(DSN, USER, PASSWORD); $user = new Usuario($databaseLogger); if ($user->isLogged()) { ... ... ... ... $user->logAction('Se enlogó correctamente'); } ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ <?php class Usuario { protected $logger; /** * Usado para inyectar el objeto Logger * * @param object ILogger Instancia de objeto que sigue la * interface ILogger * @return void */ public setLogger(ILogger $logger) { $this->logger = $logger; } // Aquí van un par de métodos más ... ... ... ... } $user = new Usuario(); $user->setLogger(new DatabaseLogger(DSN, USER, PASSWORD)); if ($user->isLogged()) { ... ... ... ... $user->logAction('Se enlogó correctamente'); } ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ <?php interface IObjectB { public function calculate($value); } interface IObjectC { public function convert($value); } interface IObjectD { public function hash($value); } class A { protected $b; public function __construct(IObjectB $b) { $this->b = $b; } public function calculate($value) { return $this->b->calculate($value); } } class B implements IObjectB { protected $c, $d; public function __construct(IObjectC $c, IObjectD $d) { $this->c = $c; $this->d = $d; } public function calculate($value) { $value = $this->c->convert($value); return $this->d->hash($value); } } class C implements IObjectC { public function convert($value) { return intval($value); } } class D implements IObjectD { public function hash($value) { return md5($value); } } $b = new B(new C(), new D()); $a = new A($b); echo $a->calculate(12345678); ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ <?php require('Pimple.php'); class MyContainer extends Pimple { public function __construct() { $this['c'] = new C(); $this['d'] = new D(); $this['b'] = new B($this['c'], $this['d']); $this['a'] = new A($this['b']); } } $container = new MyContainer(); $a = $container['a']; echo $a->calculate(12345678); ?>