diff --git a/composer.json b/composer.json index 1cc0dbc..c5ddc72 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,9 @@ "require": { "illuminate/support": "5.1.*|5.2.*|5.3.*", "illuminate/filesystem": "5.1.*|5.2.*|5.3.*", + "illuminate/events": "5.1.*|5.2.*|5.3.*", + "illuminate/container": "5.1.*|5.2.*|5.3.*", + "illuminate/contracts": "5.1.*|5.2.*|5.3.*", "erusev/parsedown": "^1.6", "erusev/parsedown-extra": "0.7.*" }, diff --git a/src/Laradown.php b/src/Laradown.php index bac704c..f1fec31 100644 --- a/src/Laradown.php +++ b/src/Laradown.php @@ -3,13 +3,23 @@ namespace Buzzylab\Laradown; use ParsedownExtra; +use Illuminate\Filesystem\Filesystem; +use Illuminate\Contracts\Container\Container; class Laradown extends ParsedownExtra { + + /** + * The IoC container instance. + * + * @var \Illuminate\Contracts\Container\Container + */ + protected $container; + /** - * @var + * @var Filesystem */ - protected $files; + protected $filesystem; /** * Indicator for markdown collect block. @@ -20,14 +30,47 @@ class Laradown extends ParsedownExtra /** * Laradown constructor. + * + * @param Filesystem $filesystem */ - public function __construct() + public function __construct(Filesystem $filesystem) { parent::__construct(); - $this->files = app('files'); + $this->filesystem = $filesystem; } + /** + * Set the IoC container instance. + * + * @param \Illuminate\Contracts\Container\Container $container + * + * @return $this + */ + public function setContainer(Container $container) + { + $this->container = $container; + + return $this; + } + /** + * Get the IoC container instance or any of it's services. + * + * @param string|null $service + * + * @return object + */ + public function getContainer($service = null) + { + return is_null($service) ? ($this->container ?: app()) : ($this->container[$service] ?: app($service)); + } + + /** + * Handlers for all elements + * + * @param array $Element + * @return string + */ protected function element(array $Element) { $markup = ''; @@ -51,7 +94,15 @@ protected function element(array $Element) */ public function convert($markdown) { - return $this->text($markdown); + // Fire converting event + $this->getContainer('events')->fire('laradown.entity.converting'); + + $text = $this->text($markdown); + + // Fire converted event + $this->getContainer('events')->fire('laradown.entity.converted'); + + return $text; } /** @@ -71,6 +122,9 @@ public function render($markdown) */ public function collect() { + // Fire collecting event + $this->getContainer('events')->fire('laradown.entity.collecting'); + // Make indicator true $this->collect_indicator = true; @@ -89,6 +143,9 @@ public function endCollect() // Get the markdown content from block $markdown = ob_get_clean(); + // Fire collected event + $this->getContainer('events')->fire('laradown.entity.collected'); + // Convert the markdown content to html return $this->convert($markdown); } @@ -123,11 +180,13 @@ public function loadStyle($file = null) } // check if style file exists - if ($this->files->exists($file)) { - $content = $this->files->get($file); + if ($this->filesystem->exists($file)) { + $content = $this->filesystem->get($file); } // Finally return style return ""; } + + } diff --git a/src/MarkdownServiceProvider.php b/src/MarkdownServiceProvider.php index 0c7d9af..ac51e49 100644 --- a/src/MarkdownServiceProvider.php +++ b/src/MarkdownServiceProvider.php @@ -2,9 +2,9 @@ namespace Buzzylab\Laradown; +use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; -use ParsedownExtra; class MarkdownServiceProvider extends ServiceProvider { @@ -22,9 +22,10 @@ public function boot() public function register() { $this->app->singleton('markdown', function () { - $parsedown = new ParsedownExtra(); - return new Laradown($parsedown); + $filesystem = new Filesystem(); + + return new Laradown($filesystem); }); }