-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use with Laravel? #25
Comments
Hi @inmanturbo, to use it with Laravel, you can just install it like you would usually do (more instructions in the README.md file and in the Implicit Aspects Section or the Explicit Aspects Section if you want to add custom Aspects to your own classes)
<?php
namespace App\Aop;
use App\Aop\Aspects\DiscountAspect;
use Okapi\Aop\AopKernel;
class MyKernel extends AopKernel
{
// Define a list of your custom aspects
protected array $aspects = [
DiscountAspect::class,
];
}
// Discount Aspect
<?php
namespace App\Aop\Aspects;
use Okapi\Aop\Attributes\Aspect;
use Okapi\Aop\Attributes\After;
use Okapi\Aop\Invocation\AfterMethodInvocation;
// Aspects must be annotated with the "Aspect" attribute
#[Aspect]
class DiscountAspect
{
// Annotate the methods that you want to intercept with
// "Before", "Around" or "After" attributes
#[After(
// Use named arguments
// You can also use Wildcards (see Okapi/Wildcards package)
class: Product::class . '|' . Order::class,
method: 'get(Price|Total)',
)]
public function applyDiscount(AfterMethodInvocation $invocation): void
{
// Get the subject of the invocation
// The subject is the object class that contains the method
// that is being intercepted
$subject = $invocation->getSubject();
$productDiscount = 0.1;
$orderDiscount = 0.2;
if ($subject instanceof Product) {
// Get the result of the original method
$oldPrice = $invocation->proceed();
$newPrice = $oldPrice - ($oldPrice * $productDiscount);
// Set the new result
$invocation->setResult($newPrice);
}
if ($subject instanceof Order) {
$oldTotal = $invocation->proceed();
$newTotal = $oldTotal - ($oldTotal * $orderDiscount);
$invocation->setResult($newTotal);
}
}
}
<?php
use App\Aop\MyKernel;
MyKernel::init(); Let me know if this helped you or if you have any errors while using it :) |
But where to register or initialize the Kernel? If I add it to a service provider or
// Initialize the AOP Kernel
$kernel = App\Aop\Kernel::init(); Otherwise, if I do not add it, the kernel is just ignored and I get no errors and no applied advice |
I should say, I see it working now in the browser, with the kernel initialized in |
You're right, I missed that, sorry.
If you receive the error only when you run the tests, make sure your code doesn't call |
Strangely, if I put it directly in In any case, the code doesn't call Here are my changes to a fresh jetstream project with public/index.php: /*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/
require __DIR__.'/../vendor/autoload.php';
+$aopKernel = App\Aop\Kernel::init(); app/Aop/Kernel.php: <?php
namespace App\Aop;
use App\JetstreamAspect;
use Okapi\Aop\AopKernel;
// Extend from the "AopKernel" class
class Kernel extends AopKernel
{
// Define a list of aspects
protected array $aspects = [
JetstreamAspect::class,
];
} app/JetstreamAspect.php: <?php
namespace App;
use App\Actions\Fortify\CreateNewUser;
use Okapi\Aop\Attributes\Aspect;
use Okapi\Aop\Attributes\After;
use Okapi\Aop\Invocation\AfterMethodInvocation;
// Aspects must be annotated with the "Aspect" attribute
#[Aspect]
class JetstreamAspect
{
// This method will be executed after the "create" method of the "App\Actions\Fortify\CreateNewUser" class
#[After(
class: CreateNewUser::class,
method: 'create',
)]
public function afterCreate(AfterMethodInvocation $invocation)
{
// testing various intercepts here
// register as new user in the browser to see this code run
// I expect this to die and dump after this method is called during the RegistrationTest
dd('executed after the "create" method of the "App\Actions\Fortify\CreateNewUser" class');
}
} |
hi @inmanturbo have you successfully setup it on laravel application? when I add the kernel::init on the bootstrap/app.php just before return the $app, it was working but got some error thank you |
This error is often from a mismatch from file name to class name. Is your file called 'Kernel.php' or 'MyKernel.php' ? |
Hi @inmanturbo, my class name is both MyKernel.. did you put it on the boostrap/app.php like this ?
Thank you |
Hi @inmanturbo, sorry for the late response, I will test it now with a fresh Laravel Jetstream installation. |
@inmanturbo I have found the problem. if (!\App\Aop\Kernel::isInitialized()) {
\App\Aop\Kernel::init();
} I will add an option in the future to ignore an already initialized kernel and make it the default behaviour! |
Hi @akelalix, you don't have to require the class $path = realpath(DIR.'/..') . '/app/Aop/MyKernel.php';
require($path); because In case PHP still doesn't know about your |
Thank you @WalterWoshid .
Thanks again |
I have added the if (!\App\Aop\Kernel::isInitialized()) {
\App\Aop\Kernel::init();
} with this: \App\Aop\Kernel::init(); |
Hello It works fine with php 8.2, laravel 10.18 and php-aop 1.2.3 :-) With first lines of
|
Hello, I see you've updated this wonderful idea to work with php
8.2
How do I use it with laravel?
The text was updated successfully, but these errors were encountered: