|
| 1 | +# Functional Example of Integrating your own renderer |
| 2 | +In this project I've used a simple renderer (which just does an echo of an iframe) but you can use your own implementation. |
| 3 | +First, It must follow the RicardoFiorani\Renderer\EmbedRendererInterface interface. |
| 4 | + |
| 5 | +Basically you need two classes: |
| 6 | + |
| 7 | +* The Renderer Implementation |
| 8 | +* The Renderer's Factory |
| 9 | + |
| 10 | +The examples can be seem below: |
| 11 | + |
| 12 | +### My Renderer Implementation Class |
| 13 | +This is the concrete implementation on how your renderer is going to handle the embed URL to give you an embed code. |
| 14 | +In here you can inject any dependency you might need by the constructor and add any logic you need. |
| 15 | +Please note that it should implement the interface "\RicardoFiorani\Renderer\EmbedRendererInterface". |
| 16 | +```php |
| 17 | +<?php |
| 18 | +namespace MyVendor\MyRenderer; |
| 19 | +use \RicardoFiorani\Renderer\EmbedRendererInterface; |
| 20 | + |
| 21 | +class MyOwnRenderer implements EmbedRendererInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @param string $embedUrl |
| 25 | + * @param integer $height |
| 26 | + * @param integer $width |
| 27 | + * @return string |
| 28 | + */ |
| 29 | + public function renderVideoEmbedCode($embedUrl, $height, $width) |
| 30 | + { |
| 31 | + //Just for example porpoises |
| 32 | + return sprintf("Hello, I'm embedding %s", addslashes($embedUrl)); |
| 33 | + |
| 34 | + //A functional example would be like |
| 35 | + //return '<iframe width="' . $width . '" height="' . $height . '" src="' . addslashes($embedUrl) . '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'; |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | +### My Renderer Implementation Factory Class |
| 40 | +This is the Factory of your renderer, basically all it must do is to implement the interface RicardoFiorani\Renderer\Factory\RendererFactoryInterface |
| 41 | +```php |
| 42 | +<?php |
| 43 | +namespace MyVendor\MyRenderer\Factory; |
| 44 | +use RicardoFiorani\Renderer\EmbedRendererInterface; |
| 45 | +use RicardoFiorani\Renderer\Factory\RendererFactoryInterface; |
| 46 | + |
| 47 | +class MyOwnRendererFactory implements RendererFactoryInterface |
| 48 | +{ |
| 49 | + /** |
| 50 | + * @return EmbedRendererInterface |
| 51 | + */ |
| 52 | + public function __invoke() |
| 53 | + { |
| 54 | + return new MyOwnRenderer(); |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | +### Registering my renderer |
| 59 | + |
| 60 | +The last part is attaching your own renderer service to the VideoServiceMatcher, which can be done as the example that follows: |
| 61 | + |
| 62 | +```php |
| 63 | +<?php |
| 64 | +use RicardoFiorani\Matcher\VideoServiceMatcher; |
| 65 | + |
| 66 | +require __DIR__ . '/vendor/autoload.php'; |
| 67 | + |
| 68 | +$vsm = new VideoServiceMatcher(); |
| 69 | + |
| 70 | +//This is where you attach your own renderer to be used instead of the default one |
| 71 | +$vsm->getServiceContainer()->setRenderer('MyOwnRenderer', MyVendor\MyRenderer\Factory\MyOwnRendererFactory::class); |
| 72 | + |
| 73 | +$video = $vsm->parse('https://www.youtube.com/watch?v=PkOcm_XaWrw'); |
| 74 | + |
| 75 | +//This will output "Hello, I'm embedding http://www.youtube.com/embed/PkOcm_XaWrw" |
| 76 | +echo $video->getEmbedCode(500,500); |
| 77 | +``` |
0 commit comments