/usr/share/php/Icinga/File/Ini/Dom/Section.php is in php-icinga 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\File\Ini\Dom;
use Icinga\Exception\ConfigurationError;
/**
* A section in an INI file
*/
class Section
{
/**
* The immutable name of this section
*
* @var string
*/
protected $name;
/**
* All configuration directives of this section
*
* @var Directive[]
*/
protected $directives = array();
/**
* Comments added one line before this section
*
* @var Comment[]
*/
protected $commentsPre;
/**
* Comment added at the end of the same line
*
* @var string
*/
protected $commentPost;
/**
* @param string $name The immutable name of this section
*
* @throws ConfigurationError When the section name is empty
*/
public function __construct($name)
{
$this->name = trim($name);
if (strlen($this->name) < 1) {
throw new ConfigurationError(sprintf('Ini file error: empty section identifier'));
}
}
/**
* Append a directive to the end of this section
*
* @param Directive $directive The directive to append
*/
public function addDirective(Directive $directive)
{
$this->directives[$directive->getKey()] = $directive;
}
/**
* Remove the directive with the given name
*
* @param string $key They name of the directive to remove
*/
public function removeDirective($key)
{
unset ($this->directives[$key]);
}
/**
* Return whether this section has a directive with the given key
*
* @param string $key The name of the directive
*
* @return bool
*/
public function hasDirective($key)
{
return isset($this->directives[$key]);
}
/**
* Get the directive with the given key
*
* @param $key string
*
* @return Directive
*/
public function getDirective($key)
{
return $this->directives[$key];
}
/**
* Return the name of this section
*
* @return string The name
*/
public function getName()
{
return $this->name;
}
/**
* Set the comments to be rendered on the line before this section
*
* @param Comment[] $comments
*/
public function setCommentsPre(array $comments)
{
$this->commentsPre = $comments;
}
/**
* Set the comment rendered on the same line of this section
*
* @param Comment $comment
*/
public function setCommentPost(Comment $comment)
{
$this->commentPost = $comment;
}
/**
* Render this section into INI markup
*
* @return string
*/
public function render()
{
$dirs = '';
$i = 0;
foreach ($this->directives as $directive) {
$comments = $directive->getCommentsPre();
$dirs .= (($i++ > 0 && ! empty($comments)) ? PHP_EOL : '')
. $directive->render() . PHP_EOL;
}
$cms = '';
if (! empty($this->commentsPre)) {
foreach ($this->commentsPre as $comment) {
$comments[] = $comment->render();
}
$cms = implode(PHP_EOL, $comments) . PHP_EOL;
}
$post = '';
if (isset($this->commentPost)) {
$post = ' ' . $this->commentPost->render();
}
return $cms . sprintf('[%s]', $this->sanitize($this->name)) . $post . PHP_EOL . $dirs;
}
/**
* Escape the significant characters in sections and normalize line breaks
*
* @param $str The string to sanitize
*
* @return mixed
*/
protected function sanitize($str)
{
$str = trim($str);
$str = str_replace('\\', '\\\\', $str);
$str = str_replace('"', '\\"', $str);
$str = str_replace(']', '\\]', $str);
$str = str_replace(';', '\\;', $str);
return str_replace(PHP_EOL, ' ', $str);
}
/**
* Convert $this to an array
*
* @return array
*/
public function toArray()
{
$a = array();
foreach ($this->directives as $directive) {
$a[$directive->getKey()] = $directive->getValue();
}
return $a;
}
}
|