ConfigHandler::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
rs 9.4285
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\Util\Toolkit;
20
21
/**
22
 * ConfigHandler allows a developer to create a custom formatted
23
 * configuration file pertaining to any information they like and still
24
 * have it auto-generate PHP code.
25
 *
26
 * @package    agavi
27
 * @subpackage config
28
 *
29
 * @author     Sean Kerr <[email protected]>
30
 * @author     Dominik del Bondio <[email protected]>
31
 * @author     David Zülke <[email protected]>
32
 * @copyright  Authors
33
 * @copyright  The Agavi Project
34
 *
35
 * @since      0.9.0
36
 *
37
 * @deprecated Superseded by AgaviXmlConfigHandler, will be removed in Agavi 1.1
38
 *
39
 * @version    $Id$
40
 */
41
abstract class ConfigHandler extends BaseConfigHandler implements LegacyConfigHandlerInterface
42
{
43
    /**
44
     * @var        string An absolute filesystem path to a validation filename.
45
     */
46
    protected $validationFile = null;
47
48
    /**
49
     * @var        string A class name of the class which should be used to parse
50
     *                    Input files of this config handler.
51
     */
52
    protected $parser = null;
53
    
54
    /**
55
     * Retrieve the parameter node values of the given item's parameters element.
56
     *
57
     * @param      ConfigValueHolder $itemNode   The node that contains a parameters child.
58
     * @param      array             $oldValues  As associative array of parameters that will
59
     *                                           be overwritten if appropriate.
60
     * @param      boolean           $literalize Whether or not values should be literalized.
61
     *
62
     * @return     array An associative array of parameters
63
     *
64
     * @author     Dominik del Bondio <[email protected]>
65
     * @since      0.11.0
66
     */
67
    protected function getItemParameters($itemNode, $oldValues = array(), $literalize = true)
68
    {
69
        $data = array();
70
        if ($itemNode->hasChildren('parameters')) {
71
            foreach ($itemNode->parameters as $node) {
0 ignored issues
show
Documentation introduced by
The property parameters does not exist on object<Agavi\Config\ConfigValueHolder>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
72
                if (!$node->hasAttribute('name')) {
73
                    // create a new entry in in the array and get they key of the new
74
                    // created entry (the last in the array). The value doesn't matter
75
                    // since it will be overwritten anyways
76
                    $data[] = 0;
77
                    end($data);
78
                    $name = key($data);
79
                } else {
80
                    $name = $node->getAttribute('name');
81
                }
82
                if ($node->hasChildren('parameters')) {
83
                    $data[$name] = (isset($oldValues[$name]) && is_array($oldValues[$name])) ? $oldValues[$name] : array();
84
                    $data[$name] = $this->getItemParameters($node, $data[$name], $literalize);
85
                } else {
86
                    $data[$name] = $literalize ? Toolkit::literalize($node->getValue()) : $node->getValue();
87
                }
88
            }
89
        }
90
        // we can NOT use array_merge here, since it would break numeric keys
91
        foreach ($data as $key => $value) {
92
            $oldValues[$key] = $value;
93
        }
94
        return $oldValues;
95
    }
96
97
    /**
98
     * Initialize this ConfigHandler.
99
     *
100
     * @param      string $validationFile The path to a validation file for this config handler.
101
     * @param      string $parser         The parser class to use.
102
     * @param      array  $parameters     An associative array of initialization parameters.
103
     *
104
     * @throws     <b>InitializationException</b> If an error occurs while
105
     *                                                 initializing the
106
     *                                                 ConfigHandler
107
     *
108
     * @author     Dominik del Bondio <[email protected]>
109
     * @since      0.9.0
110
     */
111
    public function initialize($validationFile = null, $parser = null, $parameters = array())
112
    {
113
        $this->validationFile = $validationFile;
114
        $this->parser = $parser;
115
        $this->setParameters($parameters);
116
    }
117
    
118
    /**
119
     * Retrieves the stored validation filename.
120
     *
121
     * @return     string An absolute filesystem path to a validation filename.
122
     *
123
     * @author     Dominik del Bondio <[email protected]>
124
     * @since      0.11.0
125
     */
126
    public function getValidationFile()
127
    {
128
        return $this->validationFile;
129
    }
130
    
131
    /**
132
     * Builds a proper regular expression from the input pattern to test against
133
     * the given subject. This is for "environment" and "context" attributes of
134
     * configuration blocks in the files.
135
     *
136
     * @param      string $pattern A regular expression chunk without delimiters/anchors.
137
     * @param      string $subject The subject to test against
138
     *
139
     * @return     bool Whether or not the subject matched the pattern.
140
     *
141
     * @see        XmlConfigParser::testPattern()
142
     *
143
     * @author     David Zülke <[email protected]>
144
     * @since      0.11.0
145
     */
146
    public static function testPattern($pattern, $subject)
147
    {
148
        return XmlConfigParser::testPattern($pattern, $subject);
149
    }
150
151
    /**
152
     * Returns a properly ordered array of ConfigValueHolder configuration
153
     * elements for given env and context.
154
     *
155
     * @param      ConfigValueHolder $configurations The root config element
156
     * @param      string            $environment    An environment name.
157
     * @param      string            $context        A context name.
158
     * @param      bool              $autoloadParser Whether the parser class should be
159
     *                                               autoloaded or not.
160
     *
161
     * @return     ConfigValueHolder[] An array of ConfigValueHolder configuration elements.
162
     *
163
     * @author     David Zülke <[email protected]>
164
     * @since      0.11.0
165
     */
166
    public function orderConfigurations(ConfigValueHolder $configurations, $environment = null, $context = null, $autoloadParser = true)
167
    {
168
        $configs = array();
169
170
        if ($configurations->hasAttribute('parent')) {
171
            $parent = Toolkit::literalize($configurations->getAttribute('parent'));
172
            $parentConfigs = $this->orderConfigurations(ConfigCache::parseConfig($parent, $autoloadParser, $this->getValidationFile(), $this->parser)->configurations, $environment, $context, $autoloadParser);
0 ignored issues
show
Documentation introduced by
The property configurations does not exist on object<Agavi\Config\ConfigValueHolder>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Deprecated Code introduced by
The method Agavi\Config\ConfigCache::parseConfig() has been deprecated with message: New-style config handlers don't call this method anymore. To be removed in Agavi 1.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
173
            $configs = array_merge($configs, $parentConfigs);
174
        }
175
176
        foreach ($configurations as $cfg) {
177
            if (!$cfg->hasAttribute('environment') && !$cfg->hasAttribute('context')) {
178
                $configs[] = $cfg;
179
            }
180
        }
181 View Code Duplication
        foreach ($configurations as $cfg) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
            if ($environment !== null && $cfg->hasAttribute('environment') && self::testPattern($cfg->getAttribute('environment'), $environment) && !$cfg->hasAttribute('context')) {
183
                $configs[] = $cfg;
184
            }
185
        }
186 View Code Duplication
        foreach ($configurations as $cfg) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
            if (!$cfg->hasAttribute('environment') && $context !== null && $cfg->hasAttribute('context') && self::testPattern($cfg->getAttribute('context'), $context)) {
188
                $configs[] = $cfg;
189
            }
190
        }
191
        foreach ($configurations as $cfg) {
192
            if ($environment !== null && $cfg->hasAttribute('environment') && self::testPattern($cfg->getAttribute('environment'), $environment) && $context !== null && $cfg->hasAttribute('context') && self::testPattern($cfg->getAttribute('context'), $context)) {
193
                $configs[] = $cfg;
194
            }
195
        }
196
197
        return $configs;
198
    }
199
}
200