Prechádzať zdrojové kódy

Create admin exporter class

Grégoire Paris 8 rokov pred
rodič
commit
f4388b6d12

+ 72 - 0
Bridge/Exporter/AdminExporter.php

@@ -0,0 +1,72 @@
+<?php
+
+/*
+ * This file is part of the Sonata Project 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\AdminBundle\Bridge\Exporter;
+
+use Exporter\Exporter;
+use Sonata\AdminBundle\Admin\AdminInterface;
+
+/**
+ * @author Grégoire Paris <postmaster@greg0ire.fr>
+ */
+final class AdminExporter
+{
+    /**
+     * @var Exporter service from the exporter bundle
+     */
+    private $exporter;
+
+    /**
+     * @param Exporter will be used to get global settings
+     */
+    public function __construct(Exporter $exporter)
+    {
+        $this->exporter = $exporter;
+    }
+
+    /**
+     * Queries an admin for its default export formats, and falls back on global settings.
+     *
+     * @param AdminInterface $admin the current admin object
+     *
+     * @return string[] an array of formats
+     */
+    public function getAvailableFormats(AdminInterface $admin)
+    {
+        $adminExportFormats = $admin->getExportFormats();
+
+        // NEXT_MAJOR : compare with null
+        if ($adminExportFormats != array('json', 'xml', 'csv', 'xls')) {
+            return $adminExportFormats;
+        }
+
+        return $this->exporter->getAvailableFormats();
+    }
+
+    /**
+     * Builds an export filename from the class associated with the provided admin,
+     * the current date, and the provided format.
+     *
+     * @param AdminInterface $admin  the current admin object
+     * @param string         $format the format of the export file
+     */
+    public function getExportFilename(AdminInterface $admin, $format)
+    {
+        $class = $admin->getClass();
+
+        return sprintf(
+            'export_%s_%s.%s',
+            strtolower(substr($class, strripos($class, '\\') + 1)),
+            date('Y_m_d_H_i_s', strtotime('now')),
+            $format
+        );
+    }
+}

+ 62 - 0
Tests/Bridge/Exporter/AdminExporterTest.php

@@ -0,0 +1,62 @@
+<?php
+
+/*
+ * This file is part of the Sonata Project 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\AdminBundle\Tests\Bridge\Exporter;
+
+use Exporter\Exporter;
+use Sonata\AdminBundle\Bridge\Exporter\AdminExporter;
+
+class AdminExporterTest extends \PHPUnit_Framework_TestCase
+{
+    public function provideExportFormats()
+    {
+        return array(
+            'no override' => array(array('xls'), array('json', 'xml', 'csv', 'xls'), array('xls')),
+            'override in admin' => array(array('csv'), array('csv'), array('xls')),
+        );
+    }
+
+    /**
+     * @dataProvider provideExportFormats
+     */
+    public function testAdminHasPriorityOverGlobalSettings(array $expectedFormats, array $adminFormats, array $globalFormats)
+    {
+        $writers = array();
+        foreach ($globalFormats as $exportFormat) {
+            $writer = $this->getMock('Exporter\Writer\TypedWriterInterface');
+            $writer->expects($this->once())
+                ->method('getFormat')
+                ->will($this->returnValue($exportFormat));
+            $writers[] = $writer;
+        }
+
+        $exporter = new Exporter($writers);
+        $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+        $admin->expects($this->once())
+            ->method('getExportFormats')
+            ->will($this->returnValue($adminFormats));
+        $adminExporter = new AdminExporter($exporter);
+        $this->assertSame($expectedFormats, $adminExporter->getAvailableFormats($admin));
+    }
+
+    public function testGetExportFilename()
+    {
+        $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+        $admin->expects($this->once())
+            ->method('getClass')
+            ->will($this->returnValue('MyProject\AppBundle\Model\MyClass'));
+        $adminExporter = new AdminExporter(new Exporter());
+        $this->assertRegexp(
+            '#export_myclass_\d{4}_\d{2}_\d{2}_\d{2}_\d{2}_\d{2}.csv#',
+            $adminExporter->getExportFilename($admin, 'csv')
+        );
+    }
+}

+ 1 - 0
composer.json

@@ -47,6 +47,7 @@
         "jms/translation-bundle": "^1.2",
         "sensio/generator-bundle": "^2.3 || ^3.0",
         "sllh/php-cs-fixer-styleci-bridge": "^2.0",
+        "sonata-project/exporter": "^1.7",
         "sonata-project/intl-bundle": "^2.2.4",
         "symfony/phpunit-bridge": "^2.7 || ^3.0",
         "symfony/yaml": "^2.3 || ^3.0"