Download the PHP package dmp/aop-bundle without Composer

On this page you can find all versions of the php package dmp/aop-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package aop-bundle

======== Overview

This bundle adds AOP capabilities to Symfony 5.

If you haven't heard of AOP yet, it basically allows you to separate a cross-cutting concern (for example, security checks) into a dedicated class, and not having to repeat that code in all places where it is needed.

In other words, this allows you to execute custom code before, and after the invocation of certain methods in your service layer, or your controllers. You can also choose to skip the invocation of the original method, or throw exceptions.

Installation

Checkout a copy of the code::

composer require dmp/aop-bundle

Configuration

jms_aop:
    cache_dir: %kernel.cache_dir%/jms_aop

Usage

In order to execute custom code, you need two classes. First, you need a so-called pointcut. The purpose of this class is to make a decision whether a method call should be intercepted by a certain interceptor. This decision has to be made statically only on the basis of the method signature itself.

The second class is the interceptor. This class is being called instead of the original method. It contains the custom code that you would like to execute. At this point, you have access to the object on which the method is called, and all the arguments which were passed to that method.

Examples

  1. Logging

In this example, we will be implementing logging for all methods that contain "delete".

Pointcut ^^^^^^^^

::

<?php

use DMP\AopBundle\Aop\PointcutInterface;

class LoggingPointcut implements PointcutInterface
{
    public function matchesClass(\ReflectionClass $class)
    {
        return true;
    }

    public function matchesMethod(\ReflectionMethod $method)
    {
        return false !== strpos($method->name, 'delete');
    }
}

::

# services.yml
services:
    my_logging_pointcut:
        class: LoggingPointcut
        tags:
            - { name: jms_aop.pointcut, interceptor: logging_interceptor }

LoggingInterceptor ^^^^^^^^^^^^^^^^^^

::

<?php

use CG\Proxy\MethodInterceptorInterface;
use CG\Proxy\MethodInvocation;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;

class LoggingInterceptor implements MethodInterceptorInterface
{
    private $context;
    private $logger;

    public function __construct(SecurityContextInterface $context,
                                LoggerInterface $logger)
    {
        $this->context = $context;
        $this->logger = $logger;
    }

    public function intercept(MethodInvocation $invocation)
    {
        $user = $this->context->getToken()->getUsername();
        $this->logger->info(sprintf('User "%s" invoked method "%s".', $user, $invocation->reflection->name));

        // make sure to proceed with the invocation otherwise the original
        // method will never be called
        return $invocation->proceed();
    }
}

::

# services.yml
services:
    logging_interceptor:
        class: LoggingInterceptor
        arguments: [@security.context, @logger]
  1. Transaction Management

In this example, we add a @Transactional annotation, and we automatically wrap all methods where this annotation is declared in a transaction.

Pointcut ^^^^^^^^

::

use Doctrine\Common\Annotations\Reader;
use DMP\AopBundle\Aop\PointcutInterface;
use DMP\DiExtraBundle\Annotation as DI;

/**
 * @DI\Service
 * @DI\Tag("jms_aop.pointcut", attributes = {"interceptor" = "aop.transactional_interceptor"})
 *
 * @author Johannes M. Schmitt <[email protected]>
 */
class TransactionalPointcut implements PointcutInterface
{
    private $reader;

    /**
     * @DI\InjectParams({
     *     "reader" = @DI\Inject("annotation_reader"),
     * })
     * @param Reader $reader
     */
    public function __construct(Reader $reader)
    {
        $this->reader = $reader;
    }

    public function matchesClass(\ReflectionClass $class)
    {
        return true;
    }

    public function matchesMethod(\ReflectionMethod $method)
    {
        return null !== $this->reader->getMethodAnnotation($method, 'Annotation\Transactional');
    }
}

Interceptor ^^^^^^^^^^^

::

use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use CG\Proxy\MethodInvocation;
use CG\Proxy\MethodInterceptorInterface;
use Doctrine\ORM\EntityManager;
use DMP\DiExtraBundle\Annotation as DI;

/**
 * @DI\Service("aop.transactional_interceptor")
 *
 * @author Johannes M. Schmitt <[email protected]>
 */
class TransactionalInterceptor implements MethodInterceptorInterface
{
    private $em;
    private $logger;

    /**
     * @DI\InjectParams
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em, LoggerInterface $logger)
    {
        $this->em = $em;
        $this->logger = $logger;
    }

    public function intercept(MethodInvocation $invocation)
    {
        $this->logger->info('Beginning transaction for method "'.$invocation.'")');
        $this->em->getConnection()->beginTransaction();
        try {
            $rs = $invocation->proceed();

            $this->logger->info(sprintf('Comitting transaction for method "%s" (method invocation successful)', $invocation));
            $this->em->getConnection()->commit();

            return $rs;
        } catch (\Exception $ex) {
            if ($ex instanceof NotFoundHttpException) {
                $this->logger->info(sprintf('Committing transaction for method "%s" (exception thrown, but no rollback)', $invocation));
                $this->em->getConnection()->commit();
            } else {
                $this->logger->info(sprintf('Rolling back transaction for method "%s" (exception thrown)', $invocation));
                $this->em->getConnection()->rollBack();
            }

            throw $ex;
        }
    }
}

All versions of aop-bundle with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
dmp/cg-lib Version ^0.1.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package dmp/aop-bundle contains the following files

Loading the files please wait ....