Skip to content

Commit

Permalink
Merge pull request #1 from droid-php/develop
Browse files Browse the repository at this point in the history
Add module and site enable and disable commands
  • Loading branch information
joostfaassen committed Jun 21, 2016
2 parents 2855748 + 7d4f156 commit 9aee8b7
Show file tree
Hide file tree
Showing 15 changed files with 1,727 additions and 0 deletions.
30 changes: 30 additions & 0 deletions bin/droid-apache
@@ -0,0 +1,30 @@
#!/usr/bin/env php
<?php

use Symfony\Component\Console\Application;

use Droid\Plugin\Apache\DroidPlugin;

$loader = __DIR__ . '/../vendor/autoload.php';

if (!file_exists($loader)) {
$loader = __DIR__ . '/../../../autoload.php';
}

if (!file_exists($loader)) {
die(
'You must set up the project dependencies, run the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
}

require $loader;

$application = new Application('Droid Apache', '1.0.0');
$application->setCatchExceptions(true);
$registry = new DroidPlugin($application);
foreach ($registry->getCommands() as $command) {
$application->add($command);
}
$application->run();
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -26,5 +26,8 @@
"Droid\\Plugin\\Apache\\": "src/"
}
},
"bin": [
"bin/droid-apache"
],
"license": "MIT"
}
133 changes: 133 additions & 0 deletions src/Command/AbstractApacheCommand.php
@@ -0,0 +1,133 @@
<?php

namespace Droid\Plugin\Apache\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Process\ProcessBuilder;

use Droid\Plugin\Apache\Util\Normaliser;

abstract class AbstractApacheCommand extends Command
{
private $processBuilder;
private $normaliser;
private $confDir;

public function __construct(
ProcessBuilder $processBuilder,
Normaliser $normaliser,
$configDirectory = '/etc/apache2',
$name = null
) {
$this->processBuilder = $processBuilder;
$this->normaliser = $normaliser;
$this->confDir = $configDirectory;
return parent::__construct($name);
}

abstract protected function getEnabledDir();
abstract protected function getAvailableDir();

protected function getConfName($argument)
{
return $this->normaliser->normaliseConfName($argument);
}

protected function getConfFilename($argument)
{
return $this->normaliser->normaliseConfFilename($argument);
}

protected function available($confFilename)
{
$p = $this->getProcess(
array(
'stat',
'-c',
'%F',
$this->getTargetPath($confFilename)
)
);
if (!$p->run()
&& preg_match('/^regular(?: empty)? file/', $p->getOutput())
) {
return true;
}
return false;
}

protected function enabled($confFilename)
{
$p = $this->getProcess(
array(
'stat',
'-c',
'%F',
$this->getLinkPath($confFilename)
)
);
if (!$p->run()
&& substr($p->getOutput(), 0, 13) === 'symbolic link'
) {
return true;
}
return false;
}

protected function enable($confFilename)
{
$p = $this->getProcess(
array(
'ln',
'-s',
$this->getTargetPath($confFilename),
$this->getLinkPath($confFilename),
)
);
return $p->run() === 0;
}

protected function disable($confFilename)
{
$p = $this->getProcess(
array(
'unlink',
$this->getLinkPath($confFilename),
)
);
return $p->run() === 0;
}

private function getLinkPath($confFilename)
{
return implode(
DIRECTORY_SEPARATOR,
array(
$this->confDir,
$this->getEnabledDir(),
$confFilename,
)
);
}

private function getTargetPath($confFilename)
{
return implode(
DIRECTORY_SEPARATOR,
array(
$this->confDir,
$this->getAvailableDir(),
$confFilename,
)
);
}

private function getProcess($arguments)
{
return $this
->processBuilder
->setArguments($arguments)
->getProcess()
;
}
}
87 changes: 87 additions & 0 deletions src/Command/ApacheModuleDisableCommand.php
@@ -0,0 +1,87 @@
<?php

namespace Droid\Plugin\Apache\Command;

use RuntimeException;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Droid\Lib\Plugin\Command\CheckableTrait;

class ApacheModuleDisableCommand extends AbstractApacheCommand
{
use CheckableTrait;

protected $enabledDir = 'mods-enabled';
protected $availableDir = 'mods-available';

public function configure()
{
$this
->setName('apache:dismod')
->setDescription('Disable Apache modules.')
->addArgument(
'module-name',
InputArgument::REQUIRED,
'Disable the named module.'
)
;
$this->configureCheckMode();
}

public function execute(InputInterface $input, OutputInterface $output)
{
$this->activateCheckMode($input);

$confname = $this->getConfName($input->getArgument('module-name'));
$confFilename = $this->getConfFilename($input->getArgument('module-name'));

if (! $this->available($confFilename)) {
throw new RuntimeException(
sprintf('I am not aware of a module named "%s".', $confname)
);
}

if (! $this->enabled($confFilename)) {
$output->writeLn(
sprintf(
'The module "%s" is already disabled. Nothing to do.',
$confname
)
);
$this->reportChange($output);
return 0;
}

$this->markChange();

if (! $this->checkMode() && ! $this->disable($confFilename)) {
throw new RuntimeException(
sprintf('I cannot disable module "%s".', $confname)
);
}

$output->writeLn(
sprintf(
'I %s "%s".',
$this->checkMode() ? 'would disable' : 'have disabled',
$confname
)
);

$this->reportChange($output);
return 0;
}

protected function getAvailableDir()
{
return $this->availableDir;
}

protected function getEnabledDir()
{
return $this->enabledDir;
}
}
87 changes: 87 additions & 0 deletions src/Command/ApacheModuleEnableCommand.php
@@ -0,0 +1,87 @@
<?php

namespace Droid\Plugin\Apache\Command;

use RuntimeException;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Droid\Lib\Plugin\Command\CheckableTrait;

class ApacheModuleEnableCommand extends AbstractApacheCommand
{
use CheckableTrait;

protected $enabledDir = 'mods-enabled';
protected $availableDir = 'mods-available';

public function configure()
{
$this
->setName('apache:enmod')
->setDescription('Enable Apache modules.')
->addArgument(
'module-name',
InputArgument::REQUIRED,
'Enable the named module.'
)
;
$this->configureCheckMode();
}

public function execute(InputInterface $input, OutputInterface $output)
{
$this->activateCheckMode($input);

$confname = $this->getConfName($input->getArgument('module-name'));
$confFilename = $this->getConfFilename($input->getArgument('module-name'));

if (! $this->available($confFilename)) {
throw new RuntimeException(
sprintf('I am not aware of a module named "%s".', $confname)
);
}

if ($this->enabled($confFilename)) {
$output->writeLn(
sprintf(
'The module "%s" is already enabled. Nothing to do.',
$confname
)
);
$this->reportChange($output);
return 0;
}

$this->markChange();

if (! $this->checkMode() && ! $this->enable($confFilename)) {
throw new RuntimeException(
sprintf('I cannot enable module "%s".', $confname)
);
}

$output->writeLn(
sprintf(
'I %s "%s".',
$this->checkMode() ? 'would enable' : 'have enabled',
$confname
)
);

$this->reportChange($output);
return 0;
}

protected function getAvailableDir()
{
return $this->availableDir;
}

protected function getEnabledDir()
{
return $this->enabledDir;
}
}

0 comments on commit 9aee8b7

Please sign in to comment.