Skip to content

Commit

Permalink
Refactor Blade class to enforce cache path requirement and improve ty…
Browse files Browse the repository at this point in the history
…pe hinting; update readme to reflect changes in Blade initialization.
  • Loading branch information
tahaghafuri committed Dec 8, 2024
1 parent d08d4fd commit 846fe9b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
7 changes: 2 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ composer require tahaghafuri/blade
require __DIR__.'/vendor/autoload.php';

use TAG\Blade\Blade;
use Illuminate\Events\Dispatcher;

$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';

$blade = new Blade($views, $cache);
$blade = new Blade($views, $cache, new Dispatcher);
echo $blade->view()->make('hello')->render();
```

You can use all blade features as described in the Laravel Documentation:
https://laravel.com/docs/master/blade
54 changes: 26 additions & 28 deletions src/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,41 @@ class Blade {

/**
* Array containing paths where to look for blade files
* @var array
* @var array<string>
*/
public $viewPaths;
protected array $viewPaths;

/**
* Location where to store cached views
* @var string
*/
public $cachePath;
protected string $cachePath;

/**
* @var Illuminate\Container\Container
* @var Container
*/
protected $container;
protected Container $container;

/**
* @var Illuminate\View\Factory
* @var Factory
*/
protected $instance;
protected Factory $instance;

/**
* Initialize class
* @param array $viewPaths
* @param string|array $viewPaths
* @param string $cachePath
* @param Illuminate\Events\Dispatcher $events
* @param Illuminate\Events\Dispatcher|null $events
* @throws \InvalidArgumentException
*/
function __construct($viewPaths = array(), $cachePath, Dispatcher $events = null) {
function __construct($viewPaths = [], string $cachePath = null, Dispatcher $events = null) {
if ($cachePath === null) {
throw new \InvalidArgumentException('Cache path must be provided');
}

$this->container = new Container;

$this->viewPaths = (array) $viewPaths;

$this->cachePath = $cachePath;
$this->cachePath = rtrim($cachePath, '/\\');

$this->registerFilesystem();

Expand All @@ -61,43 +63,39 @@ function __construct($viewPaths = array(), $cachePath, Dispatcher $events = null
$this->instance = $this->registerFactory();
}

public function view()
public function view(): Factory
{
return $this->instance;
}

public function registerFilesystem()
protected function registerFilesystem(): void
{
$this->container->singleton('files', function(){
$this->container->singleton('files', function() {
return new Filesystem;
});
}
public function registerEvents(Dispatcher $events)

protected function registerEvents(Dispatcher $events): void
{
$this->container->singleton('events', function() use ($events)
{
return $events;
});
}

/**
* Register the engine resolver instance.
*
* @return void
*/
public function registerEngineResolver()
public function registerEngineResolver(): void
{
$me = $this;

$this->container->singleton('view.engine.resolver', function($app) use ($me)
{
$this->container->singleton('view.engine.resolver', function($app) {
$resolver = new EngineResolver;

// Next we will register the various engines with the resolver so that the
// environment can resolve the engines it needs for various views based
// on the extension of view files. We call a method for each engines.
foreach (array('php', 'blade') as $engine)
{
$me->{'register'.ucfirst($engine).'Engine'}($resolver);
foreach (['php', 'blade'] as $engine) {
$method = 'register' . ucfirst($engine) . 'Engine';
$this->{$method}($resolver);
}

return $resolver;
Expand Down

0 comments on commit 846fe9b

Please # to comment.