Added some tests and factory impl

This commit is contained in:
Michał Brzuchalski
2016-06-14 12:09:59 +02:00
parent 6e22a75a2e
commit e4982b5209
5 changed files with 228 additions and 21 deletions

View File

@@ -7,19 +7,22 @@
*/
namespace Madkom\NginxConfigurator;
use Countable;
use Madkom\Collection\CustomTypedCollection;
use Madkom\NginxConfigurator\Config\Server;
use Madkom\NginxConfigurator\Config\Upstream;
use Madkom\NginxConfigurator\Node\Directive;
use Madkom\NginxConfigurator\Node\Node;
use Madkom\NginxConfigurator\Node\Param;
use Madkom\NginxConfigurator\Node\RootNode;
use Traversable;
/**
* Class Builder
* @package Madkom\NginxConfigurator
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
*/
class Builder
class Builder implements Countable
{
/**
* @var RootNode Holds configuration root node
@@ -30,30 +33,12 @@ class Builder
* Builder constructor.
*/
public function __construct()
{
$this->clear();
}
public function clear()
{
$this->rootNode = new RootNode();
}
/**
* @param int $port
* @return Server
*/
public function addServerNode(int $port) : Server
{
$listenIPv4 = new Directive('listen', [new Param($port)]);
$listenIPv6 = new Directive('listen', [new Param("[::]:{$port}"), new Param('default'), new Param('ipv6only=on')]);
$httpNode = new Server([$listenIPv4, $listenIPv6]);
$this->rootNode->append($httpNode);
return $httpNode;
}
/**
* Append child node
* @param Node $node
* @return Node
*/
@@ -64,6 +49,46 @@ class Builder
return $node;
}
/**
* Remove child node
* @param Node $node
* @return bool
*/
public function remove(Node $node) : bool
{
return $this->rootNode->remove($node);
}
/**
* Search for specified nodes
* @param callable $checker
* @return CustomTypedCollection
*/
public function search(callable $checker) : CustomTypedCollection
{
return $this->rootNode->filter($checker);
}
/**
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
* @return int The custom count as an integer.
*/
public function count()
{
return count($this->rootNode);
}
/**
* Retrieve an external iterator
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
* @return Traversable An instance of an object implementing <b>Iterator</b> or
*/
public function getIterator()
{
return $this->rootNode->getIterator();
}
/**
* @return string
*/

45
src/Factory.php Normal file
View File

@@ -0,0 +1,45 @@
<?php
/**
* Created by PhpStorm.
* User: mbrzuchalski
* Date: 14.06.16
* Time: 11:22
*/
namespace Madkom\NginxConfigurator;
use Madkom\NginxConfigurator\Config\Location;
use Madkom\NginxConfigurator\Config\Server;
use Madkom\NginxConfigurator\Node\Directive;
use Madkom\NginxConfigurator\Node\Param;
/**
* Class Factory
* @package Madkom\NginxConfigurator
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
*/
class Factory
{
/**
* Creates Server node
* @param int $port
* @return Server
*/
public function createServer(int $port = 80) : Server
{
$listenIPv4 = new Directive('listen', [new Param($port)]);
$listenIPv6 = new Directive('listen', [new Param("[::]:{$port}"), new Param('default'), new Param('ipv6only=on')]);
return new Server([$listenIPv4, $listenIPv6]);
}
/**
* Creates Location node
* @param string $location
* @param string|null $match
* @return Location
*/
public function createLocation(string $location, string $match = null) : Location
{
return new Location(new Param($location), is_null($match) ? null : new Param($match));
}
}