Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from droid-php/develop
Add module and site enable and disable commands
- Loading branch information
Showing
15 changed files
with
1,727 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,5 +26,8 @@ | |
"Droid\\Plugin\\Apache\\": "src/" | ||
} | ||
}, | ||
"bin": [ | ||
"bin/droid-apache" | ||
], | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.