Forráskód Böngészése

add support for EntityAuditBundle

Thomas Rabaix 13 éve
szülő
commit
f3eec647e9

+ 67 - 0
DependencyInjection/Compiler/AddAuditEntityCompilerPass.php

@@ -0,0 +1,67 @@
+<?php
+
+/*
+ * This file is part of the Sonata project.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Definition;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/*
+ *
+ * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ */
+class AddAuditEntityCompilerPass implements CompilerPassInterface
+{
+    /**
+      * {@inheritDoc}
+      */
+    public function process(ContainerBuilder $container)
+    {
+        if (!$container->hasDefinition('simplethings_entityaudit.config')) {
+            return;
+        }
+
+        $autitedEntities = $container->getParameter('simplethings.entityaudit.audited_entities');
+
+        foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
+
+            if ($attributes[0]['manager_type'] != 'orm') {
+                continue;
+            }
+
+            $definition = $container->getDefinition($id);
+
+            $autitedEntities[] = $this->getModelName($container, $definition->getArgument(1));
+        }
+
+        $autitedEntities = array_unique($autitedEntities);
+
+        $container->setParameter('simplethings.entityaudit.audited_entities', $autitedEntities);
+        $container->getDefinition('sonata.admin.audit.manager')
+            ->addMethodCall('setReader', array('sonata.admin.audit.orm.reader', $autitedEntities));
+    }
+
+    /**
+     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
+     * @param $name
+     */
+    private function getModelName(ContainerBuilder $container, $name)
+    {
+        if ($name[0] == '%') {
+            return $container->getParameter(substr($name, 1, -1));
+        }
+
+        return $name;
+    }
+}

+ 1 - 0
DependencyInjection/SonataDoctrineORMAdminExtension.php

@@ -42,6 +42,7 @@ class SonataDoctrineORMAdminExtension extends Extension
         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
         $loader->load('doctrine_orm.xml');
         $loader->load('doctrine_orm_filter_types.xml');
+        $loader->load('audit.xml');
 
         $configuration = new Configuration();
         $processor = new Processor();

+ 70 - 0
Model/AuditReader.php

@@ -0,0 +1,70 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\DoctrineORMAdminBundle\Model;
+
+use Sonata\AdminBundle\Model\AuditReaderInterface;
+use SimpleThings\EntityAudit\AuditReader as SimpleThingsAuditReader;
+
+class AuditReader implements AuditReaderInterface
+{
+    protected $auditReader;
+
+    /**
+     * @param \SimpleThings\EntityAudit\AuditReader $auditReader
+     */
+    public function __construct(SimpleThingsAuditReader $auditReader)
+    {
+        $this->auditReader = $auditReader;
+    }
+
+    /**
+     * @param $className
+     * @param $id
+     * @param $revision
+     * @return mixed
+     */
+    function find($className, $id, $revision)
+    {
+        return $this->auditReader->find($className, $id, $revision);
+    }
+
+    /**
+     * @param $className
+     * @param int $limit
+     * @param int $offset
+     * @return mixed
+     */
+    function findRevisionHistory($className, $limit = 20, $offset = 0)
+    {
+        return $this->auditReader->findRevisionHistory($limit, $offset);
+    }
+
+    /**
+     * @param $classname
+     * @param $revision
+     * @return mixed
+     */
+    function findRevision($classname, $revision)
+    {
+        return $this->auditReader->findRevision($revision);
+    }
+
+    /**
+     * @param $className
+     * @param $id
+     * @return mixed
+     */
+    function findRevisions($className, $id)
+    {
+        return $this->auditReader->findRevisions($className, $id);
+    }
+}

+ 14 - 0
Resources/config/audit.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<container xmlns="http://symfony.com/schema/dic/services"
+           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+    <services>
+
+        <service id="sonata.admin.audit.orm.reader" class="Sonata\DoctrineORMAdminBundle\Model\AuditReader">
+            <argument type="service" id="simplethings_entityaudit.reader" on-invalid="ignore" />
+        </service>
+
+    </services>
+</container>

+ 1 - 0
Resources/doc/index.rst

@@ -27,6 +27,7 @@ Reference Guide
    reference/form_field_definition
    reference/form_types_and_transformers
    reference/templates
+   reference/audit
    reference/query_proxy
    reference/troubleshootings
 

+ 42 - 0
Resources/doc/reference/audit.rst

@@ -0,0 +1,42 @@
+Audit
+=====
+
+The bundle provides a support for the ``EntityAuditBundle`` from https://github.com/simplethings/EntityAudit.
+
+How it work
+-----------
+
+**Extract from the original documentation**
+
+There are a bunch of different approaches to auditing or versioning of database tables. This extension creates a
+mirroring table for each audited entities table that is suffixed with "_audit". Besides all the columns of the
+audited entity there are two additional fields:
+
+- rev : Contains the global revision number generated from a "revisions" table.
+- revtype : Contains one of 'INS', 'UPD' or 'DEL' as an information to which type of database operation caused
+  this revision log entry.
+
+The global revision table contains an id, timestamp, username and change comment field.
+
+With this approach it is possible to version an application with its changes to associations at the particular
+points in time.
+
+This extension hooks into the SchemaTool generation process so that it will automatically create the necessary
+DDL statements for your audited entities.
+
+
+Configuration
+-------------
+
+If the ``EntityAuditBundle`` is enabled, then all entities managed by the ``DoctrineORMAdminBundle`` will be autited.
+
+Usage
+-----
+
+Once the ``EntityAuditBundle`` is set, then 2 new actions are availables :
+
+ - /admin/vendor/entity/{id}/history : display the history list
+ - /admin/vendor/entity/{id}/history/{revision} : display the object at a specific revision
+
+These actions are available in the ``view`` and ``edit`` action. Please note the current implementation uses
+the ``show`` definition to display the revision.

+ 30 - 17
Resources/doc/reference/installation.rst

@@ -1,16 +1,22 @@
 Installation
 ============
 
-First install the Sonata Admin Bundle
+First install the Sonata Admin Bundle which provides Core functionalities. The ``EntityAudit`` is an optional
+history feature (https://github.com/simplethings/EntityAudit).
 
-Download bundle
----------------
+Download bundles
+----------------
 
 Add the following lines to the file ``deps``::
 
-  [SonataDoctrineORMAdminBundle]
-      git=http://github.com/sonata-project/SonataDoctrineORMAdminBundle.git
-      target=/bundles/Sonata/DoctrineORMAdminBundle
+    [SonataDoctrineORMAdminBundle]
+        git=http://github.com/sonata-project/SonataDoctrineORMAdminBundle.git
+        target=/bundles/Sonata/DoctrineORMAdminBundle
+
+    # optional bundle
+    [EntityAudit]
+        git=git://github.com/simplethings/EntityAudit.git
+        target=/bundles/SimpleThings/EntityAudit
 
 and run::
 
@@ -24,14 +30,21 @@ files:
 
 .. code-block:: php
 
-  <?php
-  // app/AppKernel.php
-  public function registerBundles()
-  {
-      return array(
-          // ...
-          new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
-          // ...
-      );
-  }
-
+    <?php
+    // app/AppKernel.php
+    public function registerBundles()
+    {
+        return array(
+            // ...
+            new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
+            new SimpleThings\EntityAudit\SimpleThingsEntityAuditBundle(),
+            // ...
+        );
+    }
+
+    // autoload.php
+    $loader->registerNamespaces(array(
+        // ...
+        'simplethings'      => __dir__.'/../vendor/bundles',
+        // ...
+    ));

+ 2 - 0
SonataDoctrineORMAdminBundle.php

@@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
 
 use Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler\AddGuesserCompilerPass;
 use Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler\AddTemplatesCompilerPass;
+use Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler\AddAuditEntityCompilerPass;
 
 class SonataDoctrineORMAdminBundle extends Bundle
 {
@@ -22,5 +23,6 @@ class SonataDoctrineORMAdminBundle extends Bundle
     {
         $container->addCompilerPass(new AddGuesserCompilerPass());
         $container->addCompilerPass(new AddTemplatesCompilerPass());
+        $container->addCompilerPass(new AddAuditEntityCompilerPass());
     }
 }