Extracted NginxConfigrator library with feature plan for CLI manipulation

This commit is contained in:
Michał Brzuchalski
2016-06-13 15:03:18 +02:00
parent dcdce081ad
commit b8bd71e265
14 changed files with 379 additions and 38 deletions

View File

@@ -10,6 +10,7 @@ namespace Madkom\NginxConfigurator;
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;
@@ -53,25 +54,14 @@ class Builder
}
/**
* @param Server $server
* @return Server
* @param Node $node
* @return Node
*/
public function appendServerNode(Server $server) : Server
public function append(Node $node) : Node
{
$this->rootNode->append($server);
$this->rootNode->append($node);
return $server;
}
/**
* @param Upstream $upstream
* @return Upstream
*/
public function appendUpstreamNode(Upstream $upstream) : Upstream
{
$this->rootNode->append($upstream);
return $upstream;
return $node;
}
/**

View File

@@ -0,0 +1,47 @@
<?php
/**
* Created by PhpStorm.
* User: mbrzuchalski
* Date: 10.06.16
* Time: 14:25
*/
namespace Madkom\NginxConfigurator\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class AddLocationCommand extends BaseCommand
{
protected function configure()
{
parent::configure();
$this->setName('location:add');
$this->setDescription("Adds location context and configuration");
$this->addArgument('name', InputArgument::OPTIONAL, 'Server hostname:port', 'localhost:80');
$this->addOption('internal', null, InputOption::VALUE_NONE, 'Adds internal directive');
$this->addOption('proxy_pass', null, InputOption::VALUE_OPTIONAL, 'Adds proxy_pass url <comment>(eg. http://proxy/)</comment>');
$this->addOption('proxy_bind', null, InputOption::VALUE_OPTIONAL, 'Adds proxy_bind directive url or variable <comment>(eg. $server_addr)</comment>');
$this->addOption('proxy_redirect', null, InputOption::VALUE_OPTIONAL ^ InputOption::VALUE_IS_ARRAY, 'Adds proxy_redirect directive <comment>(eg. http://$host or https://$host)</comment>');
$this->addOption('proxy_set_header', null, InputOption::VALUE_OPTIONAL ^ InputOption::VALUE_IS_ARRAY, 'Adds proxy_set_header directive <comment>(eg. "Content-Type: text/html"</comment>');
$this->addOption('proxy_pass_request_body', null, InputOption::VALUE_OPTIONAL, 'Adds proxy_pass_requeest_body directive <comment>(on|off)</comment>', 'on');
// // new Directive('internal'),
// new Directive('expires', [new Param('-1')]),
// new Directive('proxy_pass', [new Param('http://172.17.0.1:7777')]),
// new Directive('proxy_bind', [new Param('$server_addr')]),
// new Directive('proxy_redirect', [new Param('http://$host'), new Param('https://$host')]),
// new Directive('proxy_set_header', [new Param('Content-Length'), new Literal("")]),
// new Directive('proxy_pass_request_body', [new Param('off')]),
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$proxy_pass = $input->getOption('proxy_pass');
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* Created by PhpStorm.
* User: mbrzuchalski
* Date: 10.06.16
* Time: 13:38
*/
namespace Madkom\NginxConfigurator\Command;
use Madkom\NginxConfigurator\Builder;
use Madkom\NginxConfigurator\Config\Server;
use Madkom\NginxConfigurator\Node\Directive;
use Madkom\NginxConfigurator\Node\Param;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class AddServerCommand
* @package Madkom\NginxConfigurator\Command
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
*/
class AddServerCommand extends BaseCommand
{
protected function configure()
{
parent::configure();
$this->setName('server:add');
$this->setDescription("Adds server context and configuration of port and name");
$this->addArgument('name', InputArgument::OPTIONAL, 'Server hostname:port', 'localhost:80');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$filename = $input->getOption('file');
$config = $this->getConfig($input);
list($name, $port) = explode(':', $input->getArgument('name') . ':80');
$listenIPv4 = new Directive('listen', [new Param($port)]);
$listenIPv6 = new Directive('listen', [new Param("[::]:{$port}"), new Param('default'), new Param('ipv6only=on')]);
// TODO: Find server by name
$server = new Server([$listenIPv4, $listenIPv6]);
if ($name != 'localhost' && !empty($name)) {
$server->append(new Directive('server_name', [new Param($name)]));
}
$config->append($server);
$builder = new Builder();
$builder->appendServerNode($server);
$builder->dumpFile($filename);
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: mbrzuchalski
* Date: 10.06.16
* Time: 14:25
*/
namespace Madkom\NginxConfigurator\Command;
/**
* Class AddUpstreamCommand
* @package Madkom\NginxConfigurator\Command
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
*/
class AddUpstreamCommand extends BaseCommand
{
protected function configure()
{
parent::configure();
$this->setName('upstream:add');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Created by PhpStorm.
* User: mbrzuchalski
* Date: 10.06.16
* Time: 14:31
*/
namespace Madkom\NginxConfigurator\Command;
/**
* Class AddUpstreamServerCommand
* @package Madkom\NginxConfigurator\Command
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
*/
class AddUpstreamServerCommand extends BaseCommand
{
protected function configure()
{
parent::configure();
$this->setName('upstream:server:add');
$this->setDescription("Adds server directive to upstream context and configuration");
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* Created by PhpStorm.
* User: mbrzuchalski
* Date: 10.06.16
* Time: 14:01
*/
namespace Madkom\NginxConfigurator\Command;
use Exception;
use Madkom\NginxConfigurator\Builder;
use Madkom\NginxConfigurator\Node\Node;
use Madkom\NginxConfigurator\Node\RootNode;
use Madkom\NginxConfigurator\Parser;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
/**
* Class BaseCommand
* @package Madkom\NginxConfigurator\Command
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
*/
abstract class BaseCommand extends Command
{
protected function configure()
{
$this->addOption('file', 'f', InputOption::VALUE_OPTIONAL, 'Output filename', 'php://stdout');
}
/**
* @param InputInterface $input
* @return RootNode
* @throws Exception
*/
protected function getConfig(InputInterface $input) : RootNode
{
$filename = $input->getOption('file');
if ($filename != 'php://stdout' && !file_exists($filename)) {
@touch($filename);
}
if ($filename != 'php://stdout' && file_exists($filename)) {
if (!is_writable($filename)) {
throw new Exception('Given filename is not writable!');
}
}
if ($filename != 'php://stdout' && file_exists($filename)) {
$parser = new Parser();
return $parser->parseFile($filename);
}
return new RootNode();
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Created by PhpStorm.
* User: mbrzuchalski
* Date: 10.06.16
* Time: 14:25
*/
namespace Madkom\NginxConfigurator\Command;
/**
* Class RemoveLocationCommand
* @package Madkom\NginxConfigurator\Command
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
*/
class RemoveLocationCommand extends BaseCommand
{
protected function configure()
{
parent::configure();
$this->setName('location:remove');
$this->setDescription("Remove location context and it's configuration");
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* Created by PhpStorm.
* User: mbrzuchalski
* Date: 10.06.16
* Time: 13:38
*/
namespace Madkom\NginxConfigurator\Command;
use Madkom\NginxConfigurator\Builder;
use Madkom\NginxConfigurator\Config\Server;
use Madkom\NginxConfigurator\Node\Directive;
use Madkom\NginxConfigurator\Node\Param;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class RemoveServerCommand
* @package Madkom\NginxConfigurator\Command
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
*/
class RemoveServerCommand extends BaseCommand
{
protected function configure()
{
parent::configure();
$this->setName('server:remove');
$this->setDescription("Removes server context and it's configuration");
$this->addArgument('name', InputArgument::OPTIONAL, 'Server hostname:port', 'localhost:80');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$filename = $input->getOption('file');
$builder = $this->getConfig($input);
list($name, $port) = explode(':', $input->getArgument('name') . ':80');
$listenIPv4 = new Directive('listen', [new Param($port)]);
$listenIPv6 = new Directive('listen', [new Param("[::]:{$port}"), new Param('default'), new Param('ipv6only=on')]);
$server = new Server([$listenIPv4, $listenIPv6]);
if ($name != 'localhost' && !empty($name)) {
$server->append(new Directive('server_name', [new Param($name)]));
}
$builder->appendServerNode($server);
$builder->dumpFile($filename);
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: mbrzuchalski
* Date: 10.06.16
* Time: 14:25
*/
namespace Madkom\NginxConfigurator\Command;
/**
* Class RemoveUpstreamCommand
* @package Madkom\NginxConfigurator\Command
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
*/
class RemoveUpstreamCommand extends BaseCommand
{
protected function configure()
{
parent::configure();
$this->setName('upstream:remove');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Created by PhpStorm.
* User: mbrzuchalski
* Date: 10.06.16
* Time: 14:31
*/
namespace Madkom\NginxConfigurator\Command;
/**
* Class RemoveUpstreamServerCommand
* @package Madkom\NginxConfigurator\Command
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
*/
class RemoveUpstreamServerCommand extends BaseCommand
{
protected function configure()
{
parent::configure();
$this->setName('upstream:server:remove');
$this->setDescription("Removes server directive from upstream context and configuration");
}
}