From d0cabb0cc1eb7129491e44b6b45c5973368fd71d Mon Sep 17 00:00:00 2001 From: mychidarko Date: Sat, 17 Aug 2024 04:36:26 +0000 Subject: [PATCH] feat: switch to jenssegers/blade --- CHANGELOG.md | 29 --- composer.json | 62 ++--- src/Blade.php | 132 ++--------- src/BladeConfig.php | 154 ------------- src/BladeContainer.php | 372 ------------------------------ tests/BladeTest.php | 82 ------- tests/Pest.php | 45 ---- tests/cache/.gitignore | 2 - tests/expected/extends.html | 10 - tests/expected/other.html | 43 ---- tests/views/basic.blade.php | 1 - tests/views/directive.blade.php | 1 - tests/views/extends.blade.php | 9 - tests/views/main/layout.blade.php | 9 - tests/views/other.blade.php | 43 ---- tests/views/plain.php | 1 - tests/views/variables.blade.php | 1 - 17 files changed, 49 insertions(+), 947 deletions(-) delete mode 100644 CHANGELOG.md delete mode 100644 src/BladeConfig.php delete mode 100755 src/BladeContainer.php delete mode 100644 tests/BladeTest.php delete mode 100644 tests/Pest.php delete mode 100644 tests/cache/.gitignore delete mode 100644 tests/expected/extends.html delete mode 100644 tests/expected/other.html delete mode 100644 tests/views/basic.blade.php delete mode 100644 tests/views/directive.blade.php delete mode 100644 tests/views/extends.blade.php delete mode 100644 tests/views/main/layout.blade.php delete mode 100644 tests/views/other.blade.php delete mode 100644 tests/views/plain.php delete mode 100644 tests/views/variables.blade.php diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 5540c39..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,29 +0,0 @@ -# Changelog - -## Leaf Blade v1.2.4 - 20th January, 2021 - -### Added - -- Added support for illuminate 8 packages - -### Changed - -- Made directory selection optional on Blade init - -### Removed - -- Nothing was removed - -## Leaf Blade v1.2.3 - 7th March, 2020 - -### Added - -- Added Configure Method - -### Changed - -- Made directory selection optional on Blade init - -### Removed - -- Nothing was removed diff --git a/composer.json b/composer.json index 413244a..03c538e 100644 --- a/composer.json +++ b/composer.json @@ -1,34 +1,34 @@ { - "name": "leafs/blade", - "description": "Leaf PHP Framework adaptation of jenssegers/blade package", - "keywords": ["leaf", "leafMVC", "laravel", "blade", "template", "view", "render"], - "license" : "MIT", - "type": "library", - "authors": [ - { - "name": "Michael Darko", - "email": "mickdd22@gmail.com", - "homepage": "https://mychi.netlify.app", - "role": "Developer" - } - ], - "require": { - "php": ">=7.3|^8.0", - "illuminate/view": "^8.0|^9.0|^10.0" - }, - "require-dev": { - "pestphp/pest": "^1.0|^2.25" - }, - "autoload": { - "psr-4": { - "Leaf\\": "src/" - } - }, - "minimum-stability": "dev", - "prefer-stable": true, - "config": { - "allow-plugins": { - "pestphp/pest-plugin": true - } + "name": "leafs/blade", + "description": "Leaf PHP Framework adaptation of jenssegers/blade package", + "keywords": [ + "leaf", + "leafMVC", + "laravel", + "blade", + "template", + "view", + "render" + ], + "license": "MIT", + "type": "library", + "authors": [ + { + "name": "Michael Darko", + "email": "mickdd22@gmail.com", + "homepage": "https://mychi.netlify.app", + "role": "Developer" } + ], + "require": { + "php": ">=7.4|^8.0", + "jenssegers/blade": "^1.4" + }, + "autoload": { + "psr-4": { + "Leaf\\": "src/" + } + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/src/Blade.php b/src/Blade.php index 5545796..9c6dd3d 100644 --- a/src/Blade.php +++ b/src/Blade.php @@ -2,52 +2,21 @@ namespace Leaf; -use Illuminate\Contracts\Container\Container as ContainerInterface; -use Illuminate\Contracts\View\Factory as FactoryContract; -use Illuminate\Contracts\View\View; -use Illuminate\Events\Dispatcher; -use Illuminate\Filesystem\Filesystem; -use Illuminate\Support\Facades\Facade; -use Illuminate\View\Compilers\BladeCompiler; -use Illuminate\View\Factory; -use Illuminate\View\ViewServiceProvider; - -class Blade implements FactoryContract +class Blade { - /** - * @var BladeContainer - */ - protected $container; - - /** - * @var Factory - */ - private $factory; + protected $blade; - /** - * @var BladeCompiler - */ - private $compiler; - - public function __construct($viewPaths = null, string $cachePath = null, ContainerInterface $container = null) + public function __construct(string $viewPaths = null, string $cachePath = null) { - $this->container = $container ?: new \Leaf\BladeContainer(); - - if ($viewPaths != null && $cachePath != null) { - $this->configure($viewPaths, $cachePath); - } + // Just to maintain compatibility with the original Leaf Blade } /** * Configure your view and cache directories */ - public function configure($viewPaths, $cachePath) + public function configure(string $viewPaths, string $cachePath) { - $this->setupContainer((array) $viewPaths, $cachePath); - (new ViewServiceProvider($this->container))->register(); - - $this->factory = $this->container->get('view'); - $this->compiler = $this->container->get('blade.compiler'); + $this->blade = new \Jenssegers\Blade\Blade($viewPaths, $cachePath); } /** @@ -56,9 +25,9 @@ public function configure($viewPaths, $cachePath) * A shorter version of the original `make` command. * You can optionally pass data into the view as a second parameter */ - public function render(string $view, array $data = [], array $mergeData = []): string + public function render(string $view, $data = [], $mergeData = []) { - return $this->make($view, $data, $mergeData)->render(); + return $this->make($view, $data, $mergeData); } /** @@ -67,90 +36,25 @@ public function render(string $view, array $data = [], array $mergeData = []): s * You can optionally pass data into the view as a second parameter. * Don't forget to chain the `render` method */ - public function make($view, $data = [], $mergeData = []): View - { - return $this->factory->make($view, $data, $mergeData); - } - - public function compiler(): BladeCompiler + public function make(string $view, $data = [], $mergeData = []): string { - return $this->compiler; + return $this->blade->make($view, $data, $mergeData)->render(); } /** - * Create your own custom directive + * Add a new namespace to the loader */ public function directive(string $name, callable $handler) { - $this->compiler->directive($name, $handler); - } - - public function if($name, callable $callback) - { - $this->compiler->if($name, $callback); - } - - public function exists($view): bool - { - return $this->factory->exists($view); - } - - public function file($path, $data = [], $mergeData = []): View - { - return $this->factory->file($path, $data, $mergeData); - } - - public function share($key, $value = null) - { - return $this->factory->share($key, $value); - } - - public function composer($views, $callback): array - { - return $this->factory->composer($views, $callback); - } - - public function creator($views, $callback): array - { - return $this->factory->creator($views, $callback); - } - - public function addNamespace($namespace, $hints): self - { - $this->factory->addNamespace($namespace, $hints); - - return $this; - } - - public function replaceNamespace($namespace, $hints): self - { - $this->factory->replaceNamespace($namespace, $hints); - - return $this; - } - - public function __call(string $method, array $params) - { - return call_user_func_array([$this->factory, $method], $params); + $this->blade->directive($name, $handler); } - protected function setupContainer(array $viewPaths, string $cachePath) + /** + * Summary of blade + * @return \Jenssegers\Blade\Blade + */ + public function blade() { - $this->container->bindIf('files', function () { - return new Filesystem; - }, true); - - $this->container->bindIf('events', function () { - return new Dispatcher; - }, true); - - $this->container->bindIf('config', function () use ($viewPaths, $cachePath) { - return new BladeConfig([ - 'view.paths' => $viewPaths, - 'view.compiled' => $cachePath, - ]); - }, true); - - Facade::setFacadeApplication($this->container); + return $this->blade; } } diff --git a/src/BladeConfig.php b/src/BladeConfig.php deleted file mode 100644 index bedcc65..0000000 --- a/src/BladeConfig.php +++ /dev/null @@ -1,154 +0,0 @@ -items = $items; - } - - /** - * @param string $key - * - * @return bool - */ - public function has($key) - { - return Arr::has($this->items, $key); - } - - /** - * @param array|string $key - * @param mixed $default - * - * @return mixed - */ - public function get($key, $default = null) - { - if (is_array($key)) { - return $this->getMany($key); - } - - return Arr::get($this->items, $key, $default); - } - - /** - * @param array $keys - * - * @return array - */ - public function getMany($keys) - { - $config = []; - - foreach ($keys as $key => $default) { - if (is_numeric($key)) { - [$key, $default] = [$default, null]; - } - - $config[$key] = Arr::get($this->items, $key, $default); - } - - return $config; - } - - /** - * @param array|string $key - * @param mixed $value - * - * @return void - */ - public function set($key, $value = null): void - { - $keys = is_array($key) ? $key : [$key => $value]; - - foreach ($keys as $key => $value) { - Arr::set($this->items, $key, $value); - } - } - - /** - * @param string $key - * @param mixed $value - * - * @return void - */ - public function push($key, $value) - { - $array = $this->get($key, []); - - $array[] = $value; - - $this->set($key, $array); - } - - /** - * @return array - */ - public function all(): array - { - return $this->items; - } - - /** - * @param string $key - * - * @return bool - */ - public function offsetExists($key): bool - { - return $this->has($key); - } - - /** - * @param string $key - * - * @return mixed - */ - #[\ReturnTypeWillChange] - public function offsetGet($key) - { - return $this->get($key); - } - - /** - * @param string $key - * @param mixed $value - * - * @return void - */ - public function offsetSet($key, $value): void - { - $this->set($key, $value); - } - - /** - * @param string $key - * - * @return void - */ - public function offsetUnset($key): void - { - $this->set($key, null); - } -} diff --git a/src/BladeContainer.php b/src/BladeContainer.php deleted file mode 100755 index b34a905..0000000 --- a/src/BladeContainer.php +++ /dev/null @@ -1,372 +0,0 @@ -terminatingCallbacks[] = $callback; - - return $this; - } - - /** - * @return string - * - * @throws \Exception - */ - public function version() - { - throw new \Exception("Not implemented"); - } - - /** - * @param $path - * - * @return string - * - * @throws \Exception - */ - public function basePath($path = '') - { - throw new \Exception("Not implemented"); - } - - /** - * @param $path - * - * @return string - * - * @throws \Exception - */ - public function bootstrapPath($path = '') - { - throw new \Exception("Not implemented"); - } - - /** - * @param $path - * - * @return string - * - * @throws \Exception - */ - public function configPath($path = '') - { - throw new \Exception("Not implemented"); - } - - /** - * @param $path - * - * @return string - * - * @throws \Exception - */ - public function databasePath($path = '') - { - throw new \Exception("Not implemented"); - } - - /** - * @param $path - * - * @return string - * - * @throws \Exception - */ - public function langPath($path = '') - { - throw new \Exception("Not implemented"); - } - - /** - * @param $path - * - * @return string - * - * @throws \Exception - */ - public function publicPath($path = '') - { - throw new \Exception("Not implemented"); - } - - /** - * @param $path - * - * @return string - * - * @throws \Exception - */ - public function resourcePath($path = '') - { - throw new \Exception("Not implemented"); - } - - /** - * @param $path - * - * @return string - * - * @throws \Exception - */ - public function storagePath($path = '') - { - throw new \Exception("Not implemented"); - } - - /** - * @param ...$environments - * - * @return bool|string - * - * @throws \Exception - */ - public function environment(...$environments) - { - throw new \Exception("Not implemented"); - } - - /** - * @return bool - * - * @throws \Exception - */ - public function runningInConsole() - { - throw new \Exception("Not implemented"); - } - - /** - * @return bool - * - * @throws \Exception - */ - public function runningUnitTests() - { - throw new \Exception("Not implemented"); - } - - /** - * @return bool - * - * @throws \Exception - */ - public function hasDebugModeEnabled() - { - throw new \Exception("Not implemented"); - } - - /** - * @return \Illuminate\Contracts\Foundation\MaintenanceMode - * - * @throws \Exception - */ - public function maintenanceMode() - { - throw new \Exception("Not implemented"); - } - - /** - * @return bool - * - * @throws \Exception - */ - public function isDownForMaintenance() - { - throw new \Exception("Not implemented"); - } - - /** - * @return void - * - * @throws \Exception - */ - public function registerConfiguredProviders() - { - throw new \Exception("Not implemented"); - } - - /** - * @param $provider - * @param $force - * - * @return \Illuminate\Support\ServiceProvider - * - * @throws \Exception - */ - public function register($provider, $force = false) - { - throw new \Exception("Not implemented"); - } - - /** - * @param $provider - * @param $service - * - * @return void - * - * @throws \Exception - */ - public function registerDeferredProvider($provider, $service = null) - { - throw new \Exception("Not implemented"); - } - - /** - * @param $provider - * - * @return \Illuminate\Support\ServiceProvider - * - * @throws \Exception - */ - public function resolveProvider($provider) - { - throw new \Exception("Not implemented"); - } - - /** - * @return void - * - * @throws \Exception - */ - public function boot() - { - throw new \Exception("Not implemented"); - } - - /** - * @param $callback - * - * @return void - * - * @throws \Exception - */ - public function booting($callback) - { - throw new \Exception("Not implemented"); - } - - /** - * @param $callback - * - * @return void - * - * @throws \Exception - */ - public function booted($callback) - { - throw new \Exception("Not implemented"); - } - - /** - * @param array $bootstrappers - * - * @return void - * - * @throws \Exception - */ - public function bootstrapWith(array $bootstrappers) - { - throw new \Exception("Not implemented"); - } - - /** - * @return string - * - * @throws \Exception - */ - public function getLocale() - { - throw new \Exception("Not implemented"); - } - - /** - * @return string - * - * @throws \Exception - */ - public function getNamespace() - { - throw new \Exception("Not implemented"); - } - - /** - * @param $provider - * - * @return array - * - * @throws \Exception - */ - public function getProviders($provider) - { - throw new \Exception("Not implemented"); - } - - /** - * @return bool - * - * @throws \Exception - */ - public function hasBeenBootstrapped() - { - throw new \Exception("Not implemented"); - } - - /** - * @return void - * - * @throws \Exception - */ - public function loadDeferredProviders() - { - throw new \Exception("Not implemented"); - } - - /** - * @param $locale - * - * @return void - * - * @throws \Exception - */ - public function setLocale($locale) - { - throw new \Exception("Not implemented"); - } - - /** - * @return bool - * - * @throws \Exception - */ - public function shouldSkipMiddleware() - { - throw new \Exception("Not implemented"); - } - - /** - * @return void - * - * @throws \Exception - */ - public function terminate() - { - throw new \Exception("Not implemented"); - } -} diff --git a/tests/BladeTest.php b/tests/BladeTest.php deleted file mode 100644 index de9a688..0000000 --- a/tests/BladeTest.php +++ /dev/null @@ -1,82 +0,0 @@ -make('basic'); - expect(trim($output))->toBe('hello world'); -}); - -test('variables', function () use (&$blade) { - $output = $blade->make('variables', ['name' => 'John Doe']); - expect(trim($output))->toBe('hello John Doe'); -}); - -test('non-blade', function () use (&$blade) { - $output = $blade->make('plain'); - expect(trim($output))->toBe('this is plain php'); -}); - -test('render alias', function () use (&$blade) { - $output = $blade->make('basic'); - expect(trim($output))->toBe('hello world'); -}); - -test('directive', function () use (&$blade) { - $blade->directive('datetime', function ($expression) { - return "format('F d, Y g:i a'); ?>"; - }); - - $output = $blade->make('directive', ['birthday' => new DateTime('1989/08/19')]); - expect(trim($output))->toBe('Your birthday is August 19, 1989 12:00 am'); -}); - -test('other', function () use (&$blade) { - $users = [ - [ - 'id' => 1, - 'name' => 'John Doe', - 'email' => 'john.doe@doe.com', - ], - [ - 'id' => 2, - 'name' => 'Jen Doe', - 'email' => 'jen.doe@example.com', - ], - [ - 'id' => 3, - 'name' => 'Jerry Doe', - 'email' => 'jerry.doe@doe.com', - ], - ]; - - $output = $blade->make('other', [ - 'users' => $users, - 'name' => 'John', - 'authenticated' => false, - ]); - - write($output, 'other'); - - expect((string)$output)->toBe(expected('other')); -}); - -function write(string $output, string $file) -{ - $file_path = __DIR__ . '/expected/' . $file . '.html'; - - file_put_contents($file_path, $output); -} - -function expected(string $file): string -{ - $file_path = __DIR__ . '/expected/' . $file . '.html'; - - return file_get_contents($file_path); -} diff --git a/tests/Pest.php b/tests/Pest.php deleted file mode 100644 index 5949c61..0000000 --- a/tests/Pest.php +++ /dev/null @@ -1,45 +0,0 @@ -in('Feature'); - -/* -|-------------------------------------------------------------------------- -| Expectations -|-------------------------------------------------------------------------- -| -| When you're writing tests, you often need to check that values meet certain conditions. The -| "expect()" function gives you access to a set of "expectations" methods that you can use -| to assert different things. Of course, you may extend the Expectation API at any time. -| -*/ - -expect()->extend('toBeOne', function () { - return $this->toBe(1); -}); - -/* -|-------------------------------------------------------------------------- -| Functions -|-------------------------------------------------------------------------- -| -| While Pest is very powerful out-of-the-box, you may have some testing code specific to your -| project that you don't want to repeat in every file. Here you can also expose helpers as -| global functions to help you to reduce the number of lines of code in your test files. -| -*/ - -function something() -{ - // .. -} diff --git a/tests/cache/.gitignore b/tests/cache/.gitignore deleted file mode 100644 index d6b7ef3..0000000 --- a/tests/cache/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore diff --git a/tests/expected/extends.html b/tests/expected/extends.html deleted file mode 100644 index 59c69f8..0000000 --- a/tests/expected/extends.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - This is the extended title - - - - This is the new context - - diff --git a/tests/expected/other.html b/tests/expected/other.html deleted file mode 100644 index 1bd4012..0000000 --- a/tests/expected/other.html +++ /dev/null @@ -1,43 +0,0 @@ -Default
- - -<script type="text/javascript">alert("Hacked!");</script>
- - -Hello, John.
- - -hello <strong>John</strong> -
- - - You are not signed in. - -
-
-
- - - - - - - - - - - - - - - - - - - - - -
IDNameEmail
1John Doejohn.doe@doe.com
2Jen Doejen.doe@example.com
3Jerry Doejerry.doe@doe.com
-
-
-
diff --git a/tests/views/basic.blade.php b/tests/views/basic.blade.php deleted file mode 100644 index 3b18e51..0000000 --- a/tests/views/basic.blade.php +++ /dev/null @@ -1 +0,0 @@ -hello world diff --git a/tests/views/directive.blade.php b/tests/views/directive.blade.php deleted file mode 100644 index 31eb417..0000000 --- a/tests/views/directive.blade.php +++ /dev/null @@ -1 +0,0 @@ -Your birthday is @datetime($birthday) diff --git a/tests/views/extends.blade.php b/tests/views/extends.blade.php deleted file mode 100644 index 5bdfc2b..0000000 --- a/tests/views/extends.blade.php +++ /dev/null @@ -1,9 +0,0 @@ -@extends('main.layout') - -@section('title') - This is the extended title -@endsection - -@section('content') - This is the new context -@endsection diff --git a/tests/views/main/layout.blade.php b/tests/views/main/layout.blade.php deleted file mode 100644 index 282acea..0000000 --- a/tests/views/main/layout.blade.php +++ /dev/null @@ -1,9 +0,0 @@ - - - - @yield('title') - - - @yield('content') - - diff --git a/tests/views/other.blade.php b/tests/views/other.blade.php deleted file mode 100644 index c457a0e..0000000 --- a/tests/views/other.blade.php +++ /dev/null @@ -1,43 +0,0 @@ -{{-- you must get 'Default' --}} -{{ $undefined ?? 'Default' }}
- -{{-- it must strip out this XSS sample --}} -{{ '' }}
- -{{-- this must be bold --}} -Hello, {!! $name !!}.
- -{{-- using include directive --}} -@include('variables', ['name' => $name])
- -{{-- using unless, reversing 'true' --}} -@unless ($authenticated) - You are not signed in. -@endunless - -
-
-
- - @if (count($users) > 0) - - - - - - @foreach ($users as $user) - - - - - - @endforeach - @else - - - - @endif -
IDNameEmail
{{ $user['id'] }}{{ $user['name'] }}{{ $user['email'] }}
No users found!
-
-
-
diff --git a/tests/views/plain.php b/tests/views/plain.php deleted file mode 100644 index ff3b69f..0000000 --- a/tests/views/plain.php +++ /dev/null @@ -1 +0,0 @@ -this is plain php diff --git a/tests/views/variables.blade.php b/tests/views/variables.blade.php deleted file mode 100644 index 71f40d3..0000000 --- a/tests/views/variables.blade.php +++ /dev/null @@ -1 +0,0 @@ -hello {{ $name }}