Completed
Push — deprecation_warning_configurat... ( d50133...8dcd73 )
by Maximilian
01:44
created

SonataDoctrinePHPCRAdminExtension::loadTreeTypes()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\DependencyInjection;
13
14
use Sonata\AdminBundle\DependencyInjection\AbstractSonataAdminExtension;
15
use Symfony\Component\Config\Definition\Processor;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19
20
/**
21
 * SonataAdminBundleExtension.
22
 *
23
 * @author      Thomas Rabaix <[email protected]>
24
 * @author      Michael Williams <[email protected]>
25
 * @author      Nacho Martín <[email protected]>
26
 */
27
class SonataDoctrinePHPCRAdminExtension extends AbstractSonataAdminExtension
28
{
29
    /**
30
     * @param array            $configs   An array of configuration settings
31
     * @param ContainerBuilder $container A ContainerBuilder instance
32
     */
33
    public function load(array $configs, ContainerBuilder $container)
34
    {
35
        $defaultConfig = array(
36
            'templates' => array(
37
                'types' => array(
38
                    'list' => array(
39
                        'node' => 'SonataDoctrinePHPCRAdminBundle:CRUD:list_node.html.twig',
40
                    ),
41
                    'show' => array(
42
                        'doctrine_phpcr_many_to_many' => 'SonataDoctrinePHPCRAdminBundle:CRUD:show_phpcr_many_to_many.html.twig',
43
                        'doctrine_phpcr_many_to_one' => 'SonataDoctrinePHPCRAdminBundle:CRUD:show_phpcr_many_to_one.html.twig',
44
                        'doctrine_phpcr_one_to_many' => 'SonataDoctrinePHPCRAdminBundle:CRUD:show_phpcr_one_to_many.html.twig',
45
                        'doctrine_phpcr_one_to_one' => 'SonataDoctrinePHPCRAdminBundle:CRUD:show_phpcr_one_to_one.html.twig',
46
                    ),
47
                ),
48
            ),
49
        );
50
51
        $configs = $this->fixTemplatesConfiguration($configs, $container, $defaultConfig);
52
53
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
54
        $loader->load('doctrine_phpcr.xml');
55
        $loader->load('doctrine_phpcr_filter_types.xml');
56
        $loader->load('doctrine_phpcr_form_types.xml');
57
        $loader->load('form.xml');
58
        $loader->load('route.xml');
59
        $loader->load('twig.xml');
60
        $loader->load('block.xml');
61
        $loader->load('tree.xml');
62
63
        $configuration = new Configuration();
64
        $processor = new Processor();
65
        $config = $processor->processConfiguration($configuration, $configs);
66
67
        $pool = $container->getDefinition('sonata.admin.manager.doctrine_phpcr');
68
        $pool->addMethodCall('__hack_doctrine_phpcr__', $config);
69
70
        $container->getDefinition('sonata.admin.builder.doctrine_phpcr_list')
71
            ->replaceArgument(1, $config['templates']['types']['list']);
72
73
        $container->getDefinition('sonata.admin.builder.doctrine_phpcr_show')
74
            ->replaceArgument(1, $config['templates']['types']['show']);
75
76
        $this->loadTreeTypes($config, $container);
77
    }
78
79
    public function getNamespace()
80
    {
81
        return 'http://sonata-project.org/schema/dic/doctrine_phpcr_admin';
82
    }
83
84
    /**
85
     * Set the tree type mapping configuration in the services.
86
     *
87
     * @param array            $config
88
     * @param ContainerBuilder $container
89
     */
90
    private function loadTreeTypes($config, ContainerBuilder $container)
91
    {
92
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
93
            'The "sonata_doctrine_phpcr_admin.document_tree_options" configuration is deprecated since 1.2.8, to be replaced in 2.0.',
94
            E_USER_DEPRECATED
95
        );
96
97
        if (isset($config['document_tree_defaults'])) {
98
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
99
                'The "sonata_doctrine_phpcr_admin.document_tree_defaults" configuration is deprecated
100
                since 1.2.8, to be moved in 2.0.
101
                Use "sonata_doctrine_phpcr_admin.document_tree.routing_defaults" then',
102
                E_USER_DEPRECATED
103
            );
104
        }
105
106
        $options = $config['document_tree_options'];
107
        $container->setParameter('sonata_admin_doctrine_phpcr.tree_block.defaults', $config['document_tree_defaults']);
108
        $container->setParameter('sonata_admin_doctrine_phpcr.tree_confirm_move', $options['confirm_move']);
109
        unset($options['confirm_move']);
110
        $container->getDefinition('sonata.admin.doctrine_phpcr.phpcr_odm_tree')
111
            ->replaceArgument(5, $this->processDocumentTreeConfig($config['document_tree']));
112
        $container->getDefinition('sonata.admin.doctrine_phpcr.phpcr_odm_tree')
113
            ->replaceArgument(6, $options);
114
    }
115
116
    /**
117
     * Process the document tree config
118
     * Expand references to 'all' to an array of all types
119
     * Validate document types.
120
     *
121
     * @param array $documentTree
122
     */
123
    private function processDocumentTreeConfig(array $documentTree)
124
    {
125
        $docClasses = $this->findAllDocumentClasses($documentTree);
126
        if (count($docClasses)) {
127
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
128
                'The "sonata_doctrine_phpcr_admin.document_tree.class" configuration is deprecated since 1.2.8,
129
            to be removed in 2.0.',
130
                E_USER_DEPRECATED
131
            );
132
        }
133
        // Validate all document classes
134
        $invalidClasses = array_filter(
135
            $docClasses,
136
            function ($class) {
137
                return false === class_exists($class);
138
            }
139
        );
140
        if (count($invalidClasses)) {
141
            throw new \InvalidArgumentException(sprintf(
142
                'The following document types provided in valid_children are invalid: %s '.
143
                'The class names provided could not be loaded.',
144
                implode(', ', array_unique($invalidClasses))
145
            ));
146
        }
147
148
        // Process the config
149
        $processed = array();
150
        foreach ($documentTree as $docClass => $config) {
151
            // Expand 'all'
152
            if (false !== array_search('all', $config['valid_children'])) {
153
                $config['valid_children'] = $docClasses;
154
            }
155
156
            $processed[$docClass] = $config;
157
        }
158
159
        return $processed;
160
    }
161
162
    /**
163
     * Find all document classes within a document tree.
164
     *
165
     * @param array $documentTree
166
     */
167
    private function findAllDocumentClasses(array $documentTree)
168
    {
169
        $documentClasses = array_unique(array_reduce(
170
            $documentTree,
171
            function ($result, $config) {
172
                return array_merge($result, $config['valid_children']);
173
            },
174
            array_keys($documentTree)
175
        ));
176
177
        if (false !== ($allIndex = array_search('all', $documentClasses))) {
178
            unset($documentClasses[$allIndex]);
179
        }
180
181
        return $documentClasses;
182
    }
183
}
184