-
-
Save oliuz/bff7992cd903ec0a2f6ab441ab5b33de to your computer and use it in GitHub Desktop.
Ejemplo de implementación del Patrón Factory
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 characters
| <?php | |
| //Esto lo agregas en el AppServiceProvider, de esta forma puedes inyectar la clase donde lo necesites | |
| $this->app->bind(ReportFactory::class, function($app) { | |
| $config = $app['config']->get('report-factory'); | |
| return new ReportFactory($app, $config['report_type']); | |
| }); |
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 characters
| <?php | |
| return [ | |
| 'report_type' => [ | |
| 'dompdf' => 'dompdf.wrapper', | |
| 'snappy' => 'snappy.pdf.wrapper', | |
| ] | |
| ]; |
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 characters
| <?php | |
| namespace App\Services; | |
| use Illuminate\Foundation\Application; | |
| class ReportFactory | |
| { | |
| private $app; | |
| private $config; | |
| public function __construct(Application $app, array $config) | |
| { | |
| $this->app = $app; | |
| $this->config = $config; | |
| } | |
| public function create(String $reportType) | |
| { | |
| $drive = $this->resolveDrive($reportType); | |
| if (!$this->app->bound($drive)) { | |
| throw new \InvalidArgumentException("The pdf report type: {$drive} is not installed"); | |
| }; | |
| return $this->app->make($drive); | |
| } | |
| public static function new(Application $app, array $config) | |
| { | |
| return new static($app, $config); | |
| } | |
| public function resolveDrive($drive) | |
| { | |
| if (!array_key_exists($drive, $this->config)) { | |
| throw new \InvalidArgumentException("The pdf report type: {$drive} is not valid"); | |
| } | |
| return $this->config[$drive]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment