nayzo/NzoUrlEncryptorBundle

PHP 8 attributes not working

Closed this issue · 7 comments

You can't add ParamDecryptor as attribute in PHP8

obrazek

to fix it you have to add the official attribute

/**
 * @Annotation
 */
#[\Attribute(\Attribute::TARGET_METHOD)] // <-- this here
class ParamDecryptor
{
    public $params;
}
nayzo commented

Not True !
It works fine, it has been tested on PHP 8.0 and PHP 8.1
Example:

//...
use Nzo\UrlEncryptorBundle\Annotations\ParamEncryptor;
use Nzo\UrlEncryptorBundle\Encryptor\Encryptor;

class TestController extends AbstractController
{
    private $encryptor;

    public function __construct(Encryptor $encryptor)
    {
        $this->encryptor = $encryptor;
    }

    #[Route('/test/{id}', name: 'app_test')]
    #[ParamEncryptor(["id"])]
    public function index(string $id): Response
    {

        $decrypted = $this->encryptor->decrypt($id);

        dd($id, $decrypted);

       //...
    }

Not True ! It works fine, it has been tested on PHP 8.0 and PHP 8.1 Example:

//...
use Nzo\UrlEncryptorBundle\Annotations\ParamEncryptor;
use Nzo\UrlEncryptorBundle\Encryptor\Encryptor;

class TestController extends AbstractController
{
    private $encryptor;

    public function __construct(Encryptor $encryptor)
    {
        $this->encryptor = $encryptor;
    }

    #[Route('/test/{id}', name: 'app_test')]
    #[ParamEncryptor(["id"])]
    public function index(string $id): Response
    {

        $decrypted = $this->encryptor->decrypt($id);

        dd($id, $decrypted);

       //...
    }

Why do you inject the encryptor since you state here: https://github.com/nayzo/NzoUrlEncryptorBundle#in-the-controller-with-annotation-service that with annotations it's not needed?

Also, I did a simple test with annotations and I can pass any string to the URL parameter... no error or anything.

My bad... now I understood your example sorry.

Not True ! It works fine, it has been tested on PHP 8.0 and PHP 8.1 Example:

//...
use Nzo\UrlEncryptorBundle\Annotations\ParamEncryptor;
use Nzo\UrlEncryptorBundle\Encryptor\Encryptor;

class TestController extends AbstractController
{
    private $encryptor;

    public function __construct(Encryptor $encryptor)
    {
        $this->encryptor = $encryptor;
    }

    #[Route('/test/{id}', name: 'app_test')]
    #[ParamEncryptor(["id"])]
    public function index(string $id): Response
    {

        $decrypted = $this->encryptor->decrypt($id);

        dd($id, $decrypted);

       //...
    }

@WebDevM4DC, can you help me also to understand this example?

To me, it seems #[ParamEncryptor(["id"])] is not doing anything at all in this example. Otherwise, there would be no need of $decrypted = $this->encryptor->decrypt($id);

It was working fine with Symfony 6.1 and PHP 8.1, but with Symfony 6.2, it throws this error 'Attempting to use non-attribute class "Nzo\UrlEncryptorBundle\Annotations\ParamDecryptor" as attribute'

#[Route('/test/{id}', name: 'app_test')]
#[ParamEncryptor(["id"])]

Because of these attributes:

#[Route('/test/{id}', name: 'app_test')]
#[ParamEncryptor(["id"])]

There is no need to call decrypt as it gets decrypted automatically. It works very well and we are already using it in production.

@WebDevM4DC, Is it working with Symfony 6.2? In Symfony 6.1, it works for me as well.

@nayzo This commit in symfony/http-kernel v6.2 has altered how attributes are parsed