generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Kanuni\LaravelBladeAnchor\Blade; | ||
|
||
use Illuminate\Support\Facades\Blade; | ||
use Illuminate\Support\Facades\View; | ||
|
||
class ExtendBlade | ||
{ | ||
public function boot() | ||
{ | ||
View::creator('*', function (\Illuminate\View\View $view) { | ||
$view->with('__current_view_name', $view->getName()); | ||
}); | ||
|
||
Blade::directive('anchor', function (string $anchorName) { | ||
return <<<DIRECTIVE | ||
<?php | ||
foreach ( | ||
\Kanuni\LaravelBladeAnchor\Facades\LaravelBladeAnchor::getExtenders(\$__current_view_name, $anchorName) | ||
as \$extender | ||
) { | ||
\$extender(get_defined_vars()); | ||
} | ||
?> | ||
DIRECTIVE; | ||
}); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Kanuni\LaravelBladeAnchor; | ||
|
||
use Illuminate\Support\Facades\View; | ||
use Kanuni\LaravelBladeAnchor\Contracts\Extender; | ||
|
||
class BladeAnchor | ||
{ | ||
private static array $extenders = []; | ||
|
||
public function registerExtender(string $view, string $anchor, string $extender): bool | ||
{ | ||
if (! View::exists($view)) return false; | ||
if (! class_exists($extender)) return false; | ||
|
||
$anchorPath = "$view#$anchor"; | ||
$resolvedExtender = app($extender); | ||
|
||
$this->addAnchorExtender($anchorPath, $resolvedExtender); | ||
|
||
return true; | ||
} | ||
|
||
protected function addAnchorExtender(string $anchorPath, Extender $extender) | ||
{ | ||
if (array_key_exists($anchorPath, static::$extenders)) { | ||
// Push extender to extenders array | ||
static::$extenders[$anchorPath][] = $extender; | ||
} else { | ||
// Create new array for the path and add extender to it | ||
static::$extenders[$anchorPath] = [$extender]; | ||
} | ||
} | ||
|
||
public function getExtenders(string $view, string $anchor): array | ||
{ | ||
$path = "$view#$anchor"; | ||
|
||
return array_key_exists($path, static::$extenders) | ||
? static::$extenders[$path] | ||
: []; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Kanuni\LaravelBladeAnchor; | ||
|
||
use Kanuni\LaravelBladeAnchor\Blade\ExtendBlade; | ||
use Spatie\LaravelPackageTools\Package; | ||
use Spatie\LaravelPackageTools\PackageServiceProvider; | ||
use Kanuni\LaravelBladeAnchor\Commands\MakeBladeExtenderCommand; | ||
|
||
class BladeAnchorServiceProvider extends PackageServiceProvider | ||
{ | ||
public function configurePackage(Package $package): void | ||
{ | ||
/* | ||
* This class is a Package Service Provider | ||
* | ||
* More info: https://github.com/spatie/laravel-package-tools | ||
*/ | ||
$package | ||
->name('laravel-blade-anchor') | ||
->hasConfigFile() | ||
->hasViews() | ||
->hasMigration('create_laravel-blade-anchor_table') | ||
->hasCommand(MakeBladeExtenderCommand::class); | ||
} | ||
|
||
public function bootingPackage() | ||
{ | ||
// Register blade directive | ||
app(ExtendBlade::class)->boot(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Kanuni\LaravelBladeAnchor\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class MakeBladeExtenderCommand extends Command | ||
{ | ||
public $signature = 'make:blade-extender'; | ||
|
||
public $description = 'Create new blade extender class that can be attached to anchor defined in blade template.'; | ||
|
||
public function handle(): int | ||
{ | ||
$this->comment('All done'); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Kanuni\LaravelBladeAnchor\Contracts; | ||
|
||
use Illuminate\Contracts\Support\Htmlable; | ||
|
||
interface Extender | ||
{ | ||
public function __invoke(?array $variables): string|Htmlable; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Kanuni\LaravelBladeAnchor\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
/** | ||
* @see \Kanuni\LaravelBladeAnchor\LaravelBladeAnchor | ||
*/ | ||
class LaravelBladeAnchor extends Facade | ||
{ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return \Kanuni\LaravelBladeAnchor\BladeAnchor::class; | ||
} | ||
} |