Skip to content

Commit

Permalink
support AssetInjector in mutliple packages in the same app
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Sep 26, 2024
1 parent c3cbd48 commit adcd91e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 43 deletions.
23 changes: 5 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![codestyle](https://github.com/gwleuverink/inject-package-assets/actions/workflows/codestyle.yml/badge.svg)](https://github.com/gwleuverink/inject-package-assets/actions/workflows/codestyle.yml)
[![tests](https://github.com/gwleuverink/inject-package-assets/actions/workflows/tests.yml/badge.svg)](https://github.com/gwleuverink/inject-package-assets/actions/workflows/tests.yml)

Simplify your Laravel package development with automatic asset injection.
Simplify your Laravel package development with automatic asset injection.

This package allows you to seamlessly insert JavaScript and CSS into web responses without requiring manual inclusion by your package users 🚀

Expand Down Expand Up @@ -54,37 +54,24 @@ class InjectAssets implements AssetInjector
}
```

2. Bind the implementation in your package's Service Provider.
2. Register the implementation in your package's Service Provider.

```php
namespace YourPackage;

use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Leuverink\AssetInjector\Contracts\AssetInjector;
use Leuverink\AssetInjector\AssetManager;
use YourPackage\InjectAssets;

class ServiceProvider extends BaseServiceProvider
{
public function register()
public function boot()
{
$this->app->bind(
AssetInjector::class,
InjectAssets::class
);
AssetManager::register(new InjectAssets);
}
}
```

3. Usage with Orchestra testbench

If you like to test the integration within your own package you'll need to register AssetInjector's service provider by adding it to your `testbench.yaml`

```yaml
providers:
- YourPackage\ServiceProvider
- Leuverink\AssetInjector\ServiceProvider
```
## How It Works

- Assets are automatically included in full-page responses (not partial HTML responses).
Expand Down
7 changes: 0 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@
"orchestra/testbench": "^9",
"pestphp/pest": "^2.35"
},
"extra": {
"laravel": {
"providers": [
"Leuverink\\AssetInjector\\ServiceProvider"
]
}
},
"scripts": {
"lint": "vendor/bin/duster lint",
"fix": "vendor/bin/duster fix",
Expand Down
18 changes: 18 additions & 0 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Leuverink\AssetInjector;

use Illuminate\Support\Facades\Event;
use Leuverink\AssetInjector\Contracts\AssetInjector;
use Illuminate\Foundation\Http\Events\RequestHandled;

class AssetManager
{
public static function register(AssetInjector $injector)
{
Event::listen(function (RequestHandled $event) use ($injector) {
$listener = new InjectAssets($injector);
$listener($event);
});
}
}
18 changes: 0 additions & 18 deletions src/ServiceProvider.php

This file was deleted.

0 comments on commit adcd91e

Please sign in to comment.