PropertyStreamFilter::filter()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.0032

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 16
cts 17
cp 0.9412
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 4
crap 4.0032
1
<?php
2
3
/**
4
 * AppserverIo\Properties\Filter\PropertyStreamFilter
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @author    Bernhard Wick <[email protected]>
16
 * @copyright 2015 TechDivision GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      http://github.com/appserver-io/properties
19
 * @link      http://www.appserver.io
20
 */
21
22
namespace AppserverIo\Properties\Filter;
23
24
/**
25
 * A stream filter implementation that replaces the variables defined in a stream with
26
 * the content of the properties file passed to the constructor.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @author    Bernhard Wick <[email protected]>
30
 * @copyright 2015 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      http://github.com/appserver-io/properties
33
 * @link      http://www.appserver.io
34
 */
35
class PropertyStreamFilter extends \php_user_filter
36
{
37
38
    /**
39
     * The unique filter name.
40
     *
41
     * @var string
42
     */
43
    const NAME = 'appserver-io.properties.filter.property-stream-filter';
44
45
    /**
46
     * The filter parameters.
47
     *
48
     * @var \AppserverIo\Properties\StreamFilterParams
49
     */
50
    public $params;
51
52
    /**
53
     * Returns the params.
54
     *
55
     * @return \AppserverIo\Properties\StreamFilterParams The params
56
     */
57 2
    public function getParams()
58
    {
59 2
        return $this->params;
60
    }
61
62
    /**
63
     * This method is called whenever data is read from or written to the attached
64
     * stream (such as with fread() or fwrite()).
65
     *
66
     * @param resource $in       A resource pointing to a bucket brigade which contains one or more bucket objects containing data to be filtered
67
     * @param resource $out      A resource pointing to a second bucket brigade into which your modified buckets should be placed
68
     * @param integer  $consumed Should be incremented by the length of the data which your filter reads in and alters
69
     * @param boolean  $closing  If the stream is in the process of closing, the closing parameter will be set to TRUE
70
     *
71
     * @return integer Whether the filter succeeded or not
72
     * @see php_user_filter::filter()
73
     */
74 2
    public function filter($in, $out, &$consumed, $closing)
75
    {
76
77
        // load the properties from the params
78 2
        $pattern = $this->getParams()->getPattern();
79 2
        $properties = $this->getParams()->getProperties();
80
81
        // stop processing if we can't find the properties
82 2
        if ($properties == null) {
83
            return PSFS_ERR_FATAL;
84
        }
85
86
        // while we can read from the stream
87 2
        while ($bucket = stream_bucket_make_writeable($in)) {
88
            // try to find and replace the variables
89 2
            foreach ($properties->getKeys() as $propertyName) {
90 2
                $bucket->data = str_replace(
91 2
                    sprintf($pattern, $propertyName),
92 2
                    $properties->getProperty($propertyName),
93 2
                    $bucket->data
94 2
                );
95 2
            }
96
97
            // update the stream
98 2
            $consumed += $bucket->datalen;
99 2
            stream_bucket_append($out, $bucket);
100 2
        }
101
102
        // continue applying filters
103 2
        return PSFS_PASS_ON;
104
    }
105
}
106