ConfigHandlersConfigHandler::guessValidationType()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 1
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
// +---------------------------------------------------------------------------+
4
// | This file is part of the Agavi package.                                   |
5
// | Copyright (c) 2005-2011 the Agavi Project.                                |
6
// | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr.    |
7
// |                                                                           |
8
// | For the full copyright and license information, please view the LICENSE   |
9
// | file that was distributed with this source code. You can also view the    |
10
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
11
// |   vi: set noexpandtab:                                                    |
12
// |   Local Variables:                                                        |
13
// |   indent-tabs-mode: t                                                     |
14
// |   End:                                                                    |
15
// +---------------------------------------------------------------------------+
16
17
namespace Agavi\Config;
18
19
use Agavi\Config\Util\Dom\XmlConfigDomElement;
20
use Agavi\Exception\AgaviException;
21
use Agavi\Util\Toolkit;
22
use Agavi\Config\Util\Dom\XmlConfigDomDocument;
23
24
/**
25
 * ConfigHandlersConfigHandler allows you to specify configuration handlers
26
 * for the application or on a module level.
27
 *
28
 * @package    agavi
29
 * @subpackage config
30
 *
31
 * @author     Dominik del Bondio <[email protected]>
32
 * @author     Noah Fontes <[email protected]>
33
 * @author     David Zülke <[email protected]>
34
 * @copyright  Authors
35
 * @copyright  The Agavi Project
36
 *
37
 * @since      0.11.0
38
 *
39
 * @version    $Id$
40
 */
41
class ConfigHandlersConfigHandler extends XmlConfigHandler
42
{
43
    const XML_NAMESPACE = 'http://agavi.org/agavi/config/parts/config_handlers/1.1';
44
    
45
    /**
46
     * Execute this configuration handler.
47
     *
48
     * @param      XmlConfigDomDocument $document The document to handle.
49
     *
50
     * @return     string Data to be written to a cache file.
51
     *
52
     * @throws     <b>UnreadableException</b> If a requested configuration
53
     *                                        file does not exist or is not
54
     *                                        readable.
55
     * @throws     <b>ParseException</b> If a requested configuration file is
56
     *                                   improperly formatted.
57
     *
58
     * @author     Dominik del Bondio <[email protected]>
59
     * @author     Noah Fontes <[email protected]>
60
     * @author     David Zülke <[email protected]>
61
     * @since      0.11.0
62
     */
63
    public function execute(XmlConfigDomDocument $document)
64
    {
65
        // set up our default namespace
66
        $document->setDefaultNamespace(self::XML_NAMESPACE, 'config_handlers');
67
        
68
        // init our data arrays
69
        $handlers = array();
70
        
71
        foreach ($document->getConfigurationElements() as $configuration) {
72
            if (!$configuration->has('handlers')) {
73
                continue;
74
            }
75
            
76
            // let's do our fancy work
77
            /** @var XmlConfigDomElement $handler */
78
            foreach ($configuration->get('handlers') as $handler) {
79
                $pattern = $handler->getAttribute('pattern');
80
                
81
                $category = Toolkit::normalizePath(Toolkit::expandDirectives($pattern));
82
                
83
                $class = $handler->getAttribute('class');
84
                
85
                $transformations = array(
86
                    XmlConfigParser::STAGE_SINGLE => array(),
87
                    XmlConfigParser::STAGE_COMPILATION => array(),
88
                );
89
                if ($handler->has('transformations')) {
90
                    /** @var XmlConfigDomElement $transformation */
91
                    foreach ($handler->get('transformations') as $transformation) {
92
                        $path = Toolkit::literalize($transformation->getValue());
93
                        $for = $transformation->getAttribute('for', XmlConfigParser::STAGE_SINGLE);
94
                        $transformations[$for][] = $path;
95
                    }
96
                }
97
                
98
                $validations = array(
99
                    XmlConfigParser::STAGE_SINGLE => array(
100
                        XmlConfigParser::STEP_TRANSFORMATIONS_BEFORE => array(
101
                            XmlConfigParser::VALIDATION_TYPE_RELAXNG => array(
102
                            ),
103
                            XmlConfigParser::VALIDATION_TYPE_SCHEMATRON => array(
104
                            ),
105
                            XmlConfigParser::VALIDATION_TYPE_XMLSCHEMA => array(
106
                            ),
107
                        ),
108
                        XmlConfigParser::STEP_TRANSFORMATIONS_AFTER => array(
109
                            XmlConfigParser::VALIDATION_TYPE_RELAXNG => array(
110
                            ),
111
                            XmlConfigParser::VALIDATION_TYPE_SCHEMATRON => array(
112
                            ),
113
                            XmlConfigParser::VALIDATION_TYPE_XMLSCHEMA => array(
114
                            ),
115
                        ),
116
                    ),
117
                    XmlConfigParser::STAGE_COMPILATION => array(
118
                        XmlConfigParser::STEP_TRANSFORMATIONS_BEFORE => array(
119
                            XmlConfigParser::VALIDATION_TYPE_RELAXNG => array(
120
                            ),
121
                            XmlConfigParser::VALIDATION_TYPE_SCHEMATRON => array(
122
                            ),
123
                            XmlConfigParser::VALIDATION_TYPE_XMLSCHEMA => array(
124
                            ),
125
                        ),
126
                        XmlConfigParser::STEP_TRANSFORMATIONS_AFTER => array(
127
                            XmlConfigParser::VALIDATION_TYPE_RELAXNG => array(
128
                            ),
129
                            XmlConfigParser::VALIDATION_TYPE_SCHEMATRON => array(
130
                            ),
131
                            XmlConfigParser::VALIDATION_TYPE_XMLSCHEMA => array(
132
                            ),
133
                        ),
134
                    ),
135
                );
136
                if ($handler->has('validations')) {
137
                    /** @var XmlConfigDomElement $validation */
138
                    foreach ($handler->get('validations') as $validation) {
139
                        $path = Toolkit::literalize($validation->getValue());
140
                        $type = null;
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
141
                        if (!$validation->hasAttribute('type')) {
142
                            $type = $this->guessValidationType($path);
143
                        } else {
144
                            $type = $validation->getAttribute('type');
145
                        }
146
                        $for = $validation->getAttribute('for', XmlConfigParser::STAGE_SINGLE);
147
                        $step = $validation->getAttribute('step', XmlConfigParser::STEP_TRANSFORMATIONS_AFTER);
148
                        $validations[$for][$step][$type][] = $path;
149
                    }
150
                }
151
                
152
                $handlers[$category] = isset($handlers[$category])
153
                    ? $handlers[$category]
154
                    : array(
155
                        'parameters' => array(),
156
                        );
157
                $handlers[$category] = array(
158
                    'class' => $class,
159
                    'parameters' => $handler->getAgaviParameters($handlers[$category]['parameters']),
160
                    'transformations' => $transformations,
161
                    'validations' => $validations,
162
                );
163
            }
164
        }
165
        
166
        $data = array(
167
            'return ' . var_export($handlers, true),
168
        );
169
        
170
        return $this->generate($data, $document->documentURI);
171
    }
172
    
173
    /**
174
     * Convenience method to quickly guess the type of a validation file using its
175
     * file extension.
176
     *
177
     * @param      string $path The path to the file.
178
     *
179
     * @return     string An XmlConfigParser::VALIDATION_TYPE_* const value.
180
     *
181
     * @throws     AgaviException If the type could not be determined.
182
     *
183
     * @author     David Zülke <[email protected]>
184
     * @since      1.0.0
185
     */
186
    protected function guessValidationType($path)
187
    {
188
        switch (pathinfo($path, PATHINFO_EXTENSION)) {
189
            case 'rng':
190
                return XmlConfigParser::VALIDATION_TYPE_RELAXNG;
191
            case 'rnc':
192
                return XmlConfigParser::VALIDATION_TYPE_RELAXNG;
193
            case 'sch':
194
                return XmlConfigParser::VALIDATION_TYPE_SCHEMATRON;
195
            case 'xsd':
196
                return XmlConfigParser::VALIDATION_TYPE_XMLSCHEMA;
197
            default:
198
                throw new AgaviException(sprintf('Could not determine validation type for file "%s"', $path));
199
        }
200
    }
201
}
202