2015-08-05 14:21:46 +02:00
|
|
|
<?php
|
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
|
|
|
|
|
|
|
namespace Icinga\File\Ini\Dom;
|
|
|
|
|
|
|
|
class Document
|
|
|
|
{
|
|
|
|
/**
|
2015-08-10 15:13:59 +02:00
|
|
|
* The sections of this INI file
|
|
|
|
*
|
|
|
|
* @var Section[]
|
2015-08-05 14:21:46 +02:00
|
|
|
*/
|
|
|
|
protected $sections = array();
|
|
|
|
|
|
|
|
/**
|
2015-08-10 15:13:59 +02:00
|
|
|
* The comemnts at file end that belong to no particular section
|
|
|
|
*
|
|
|
|
* @var Comment[]
|
2015-08-05 14:21:46 +02:00
|
|
|
*/
|
2015-08-10 15:13:59 +02:00
|
|
|
protected $commentsDangling;
|
2015-08-05 14:21:46 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-10 15:13:59 +02:00
|
|
|
* Append a section to the end of this INI file
|
|
|
|
*
|
2015-08-05 14:21:46 +02:00
|
|
|
* @param Section $section
|
|
|
|
*/
|
|
|
|
public function addSection(Section $section)
|
|
|
|
{
|
|
|
|
$this->sections[$section->getName()] = $section;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-10 15:13:59 +02:00
|
|
|
* Return whether this INI file has the section with the given key
|
|
|
|
*
|
2015-08-05 14:21:46 +02:00
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasSection($name)
|
|
|
|
{
|
2015-08-07 09:26:50 +02:00
|
|
|
return isset($this->sections[trim($name)]);
|
2015-08-05 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-10 15:13:59 +02:00
|
|
|
* Return the section with the given name
|
|
|
|
*
|
2015-08-05 14:21:46 +02:00
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return Section
|
|
|
|
*/
|
|
|
|
public function getSection($name)
|
|
|
|
{
|
2015-08-07 09:26:50 +02:00
|
|
|
return $this->sections[trim($name)];
|
2015-08-05 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-10 15:13:59 +02:00
|
|
|
* Set the section with the given name
|
|
|
|
*
|
2015-08-05 14:21:46 +02:00
|
|
|
* @param string $name
|
|
|
|
* @param Section $section
|
|
|
|
*
|
|
|
|
* @return Section
|
|
|
|
*/
|
|
|
|
public function setSection($name, Section $section)
|
|
|
|
{
|
2015-08-07 09:26:50 +02:00
|
|
|
return $this->sections[trim($name)] = $section;
|
2015-08-05 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-10 15:13:59 +02:00
|
|
|
* Remove the section with the given name
|
|
|
|
*
|
2015-08-05 14:21:46 +02:00
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public function removeSection($name)
|
|
|
|
{
|
2015-08-07 09:26:50 +02:00
|
|
|
unset ($this->sections[trim($name)]);
|
2015-08-05 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-10 15:13:59 +02:00
|
|
|
* Set the dangling comments at file end that belong to no particular directive
|
|
|
|
*
|
|
|
|
* @param Comment[] $comments
|
|
|
|
*/
|
|
|
|
public function setCommentsDangling(array $comments)
|
|
|
|
{
|
|
|
|
$this->commentsDangling = $comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the dangling comments at file end that belong to no particular directive
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCommentsDangling()
|
|
|
|
{
|
|
|
|
return $this->commentsDangling;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render this document into the corresponding INI markup
|
|
|
|
*
|
2015-08-05 14:21:46 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
2015-08-06 17:21:52 +02:00
|
|
|
$sections = array();
|
2015-08-05 14:21:46 +02:00
|
|
|
foreach ($this->sections as $section) {
|
|
|
|
$sections []= $section->render();
|
|
|
|
}
|
|
|
|
$str = implode(PHP_EOL, $sections);
|
|
|
|
if (! empty($this->commentsDangling)) {
|
|
|
|
foreach ($this->commentsDangling as $comment) {
|
|
|
|
$str .= PHP_EOL . $comment->render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
}
|