Commit 61261a10 authored by Jon Pugh's avatar Jon Pugh

Move Services methods from ServerContext to Context.

parent 82c8d05c
......@@ -6,6 +6,8 @@
namespace Aegir\Provision;
use Consolidation\AnnotatedCommand\CommandFileDiscovery;
use Drupal\Console\Core\Style\DrupalStyle;
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Processor;
......@@ -49,7 +51,13 @@ class Context
* init(), set defaults with setProperty().
*/
protected $properties = [];
/**
* @var array
* A list of services associated with this context.
*/
protected $services = [];
/**
* @var \Aegir\Provision\Application;
*/
......@@ -72,6 +80,7 @@ class Context
$this->name = $name;
$this->application = $application;
$this->loadContextConfig($console_config, $options);
$this->prepareServices();
}
/**
......@@ -111,6 +120,112 @@ class Context
}
}
/**
* Load Service classes from config into Context..
*/
protected function prepareServices() {
if (isset($this->config['services'])) {
foreach ($this->config['services'] as $service_name => $service) {
$service_name = ucfirst($service_name);
$service_type = ucfirst($service['type']);
$service_class = "\\Aegir\\Provision\\Service\\{$service_name}\\{$service_name}{$service_type}Service";
$this->services[strtolower($service_name)] = new $service_class($service, $this);
}
}
else {
$this->services = [];
}
}
/**
* Loads all available \Aegir\Provision\Service classes
*
* @return array
*/
public function getAvailableServices($service = NULL) {
// Load all service classes
$classes = [];
$discovery = new CommandFileDiscovery();
$discovery->setSearchPattern('*Service.php');
$servicesFiles = $discovery->discover(__DIR__ .'/Service', '\Aegir\Provision\Service');
foreach ($servicesFiles as $serviceClass) {
$classes[$serviceClass::SERVICE] = $serviceClass;
}
if ($service && isset($classes[$service])) {
return $classes[$service];
}
elseif ($service && !isset($classes[$service])) {
throw new \Exception("No service with name $service was found.");
}
else {
return $classes;
}
}
/**
* Lists all available services as a simple service => name array.
* @return array
*/
public function getServiceOptions() {
$options = [];
$services = $this->getAvailableServices();
foreach ($services as $service => $class) {
$options[$service] = $class::SERVICE_NAME;
}
return $options;
}
/**
* @return array
*/
protected function getAvailableServiceTypes($service, $service_type = NULL) {
// Load all service classes
$classes = [];
$discovery = new CommandFileDiscovery();
$discovery->setSearchPattern(ucfirst($service) . '*Service.php');
$serviceTypesFiles = $discovery->discover(__DIR__ .'/Service/' . ucfirst($service), '\Aegir\Provision\Service\\' . ucfirst($service));
foreach ($serviceTypesFiles as $serviceTypeClass) {
$classes[$serviceTypeClass::SERVICE_TYPE] = $serviceTypeClass;
}
if ($service_type && isset($classes[$service_type])) {
return $classes[$service_type];
}
elseif ($service_type && !isset($classes[$service_type])) {
throw new \Exception("No service type with name $service_type was found.");
}
else {
return $classes;
}
}
/**
* Lists all available services as a simple service => name array.
* @return array
*/
public function getServiceTypeOptions($service) {
$options = [];
$service_types = $this->getAvailableServiceTypes($service);
foreach ($service_types as $service_type => $class) {
$options[$service_type] = $class::SERVICE_TYPE_NAME;
}
return $options;
}
/**
* Return all services for this context.
*
* @return array
*/
public function getServices() {
return $this->services;
}
/**
* {@inheritdoc}
*/
......@@ -125,6 +240,19 @@ class Context
->end()
->end();
// Load Services
$root_node
->children()
->arrayNode('services')
->prototype('array')
->children()
->scalarNode('type')
->isRequired(true)
->end()
->append($this->addServiceProperties())
->end()
->end();
// @TODO: Figure out how we can let other classes add to Context properties.
foreach ($this->option_documentation() as $name => $description) {
$root_node
......@@ -142,6 +270,52 @@ class Context
return $tree_builder;
}
/**
* Append Service class options_documentation to config tree.
*/
public function addServiceProperties()
{
$builder = new TreeBuilder();
$node = $builder->root('properties');
// Load config tree from Service type classes
if (!empty($this->getProperty('services')) && !empty($this->getProperty('services'))) {
foreach ($this->getProperty('services') as $service => $info) {
$service = ucfirst($service);
$service_type = ucfirst($info['type']);
$class = "\Aegir\Provision\Service\\{$service}\\{$service}{$service_type}Service";
foreach ($class::option_documentation() as $name => $description) {
$node
->children()
->scalarNode($name)->end()
->end()
->end();
}
}
}
return $node;
}
/**
* Output a list of all services for this context.
*/
public function showServices(DrupalStyle $io) {
if (!empty($this->getServices())) {
$rows = [];
foreach ($this->getServices() as $name => $service) {
$rows[] = [$name, $service->type];
// Show all properties.
if (!empty($service->properties )) {
foreach ($service->properties as $name => $value) {
$rows[] = [' ' . $name, $value];
}
}
}
$io->table(['Services'], $rows);
}
}
/**
* Return all properties for this context.
*
......
......@@ -24,42 +24,6 @@ class ServerContext extends Context implements ConfigurationInterface
*/
public $type = 'server';
/**
* @var array
* A list of services needed for this context.
*/
protected $services = [];
/**
* ServerContext constructor.
*
* @param $name
* @param $console_config
* @param array $options
*/
function __construct($name, $console_config, Application $application,array $options = [])
{
parent::__construct($name, $console_config, $application, $options);
if (isset($this->config['services'])) {
$this->prepareServices();
}
else {
$this->services = [];
}
}
/**
* Load Service classes from config into Context..
*/
protected function prepareServices() {
foreach ($this->config['services'] as $service_name => $service) {
$service_name = ucfirst($service_name);
$service_type = ucfirst($service['type']);
$service_class = "\\Aegir\\Provision\\Service\\{$service_name}\\{$service_name}{$service_type}Service";
$this->services[strtolower($service_name)] = new $service_class($service, $this);
}
}
static function option_documentation()
{
$options = [
......@@ -72,139 +36,6 @@ class ServerContext extends Context implements ConfigurationInterface
return $options;
}
/**
* Loads all available \Aegir\Provision\Service classes
*
* @return array
*/
public function getAvailableServices($service = NULL) {
// Load all service classes
$classes = [];
$discovery = new CommandFileDiscovery();
$discovery->setSearchPattern('*Service.php');
$servicesFiles = $discovery->discover(__DIR__ .'/../Service', '\Aegir\Provision\Service');
foreach ($servicesFiles as $serviceClass) {
$classes[$serviceClass::SERVICE] = $serviceClass;
}
if ($service && isset($classes[$service])) {
return $classes[$service];
}
elseif ($service && !isset($classes[$service])) {
throw new \Exception("No service with name $service was found.");
}
else {
return $classes;
}
}
/**
* Lists all available services as a simple service => name array.
* @return array
*/
public function getServiceOptions() {
$options = [];
$services = $this->getAvailableServices();
foreach ($services as $service => $class) {
$options[$service] = $class::SERVICE_NAME;
}
return $options;
}
/**
* @return array
*/
protected function getAvailableServiceTypes($service, $service_type = NULL) {
// Load all service classes
$classes = [];
$discovery = new CommandFileDiscovery();
$discovery->setSearchPattern(ucfirst($service) . '*Service.php');
$serviceTypesFiles = $discovery->discover(__DIR__ .'/../Service/' . ucfirst($service), '\Aegir\Provision\Service\\' . ucfirst($service));
foreach ($serviceTypesFiles as $serviceTypeClass) {
$classes[$serviceTypeClass::SERVICE_TYPE] = $serviceTypeClass;
}
if ($service_type && isset($classes[$service_type])) {
return $classes[$service_type];
}
elseif ($service_type && !isset($classes[$service_type])) {
throw new \Exception("No service type with name $service_type was found.");
}
else {
return $classes;
}
}
/**
* Lists all available services as a simple service => name array.
* @return array
*/
public function getServiceTypeOptions($service) {
$options = [];
$service_types = $this->getAvailableServiceTypes($service);
foreach ($service_types as $service_type => $class) {
$options[$service_type] = $class::SERVICE_TYPE_NAME;
}
return $options;
}
/**
* Return all services for this context.
*
* @return array
*/
public function getServices() {
return $this->services;
}
/**
* Pass server specific config to Context configTreeBuilder.
*/
public function configTreeBuilder(&$root_node)
{
$root_node
->children()
->arrayNode('services')
->prototype('array')
->children()
->scalarNode('type')
->isRequired(true)
->end()
->append($this->addServiceProperties())
->end()
->end();
}
/**
* Append Service class options_documentation to config tree.
*/
public function addServiceProperties()
{
$builder = new TreeBuilder();
$node = $builder->root('properties');
// Load config tree from Service type classes
if (!empty($this->getProperty('services')) && !empty($this->getProperty('services'))) {
foreach ($this->getProperty('services') as $service => $info) {
$service = ucfirst($service);
$service_type = ucfirst($info['type']);
$class = "\Aegir\Provision\Service\\{$service}\\{$service}{$service_type}Service";
foreach ($class::option_documentation() as $name => $description) {
$node
->children()
->scalarNode($name)->end()
->end()
->end();
}
}
}
return $node;
}
public function verify() {
// Run verify method on all services.
......@@ -215,23 +46,4 @@ class ServerContext extends Context implements ConfigurationInterface
return "Server Context Verified: " . $this->name;
}
/**
* Output a list of all services for this service.
*/
public function showServices(DrupalStyle $io) {
if (!empty($this->getServices())) {
$rows = [];
foreach ($this->getServices() as $name => $service) {
$rows[] = [$name, $service->type];
// Show all properties.
if (!empty($service->properties )) {
foreach ($service->properties as $name => $value) {
$rows[] = [' ' . $name, $value];
}
}
}
$io->table(['Services'], $rows);
}
}
}
......@@ -48,6 +48,6 @@ class SiteConfiguration extends Configuration {
if (!$this->site_enabled) {
$this->template = $this->disabled_template;
}
}
}
\ No newline at end of file
<VirtualHost *:<?php print $http_port; ?>>
<?php if ($this->site_mail) : ?>
ServerAdmin <?php print $this->site_mail; ?>
<?php endif;?>
<?php
$aegir_root = drush_get_option('aegir_root');
if (!$aegir_root && $server->aegir_root) {
$aegir_root = $server->aegir_root;
}
?>
DocumentRoot <?php print $this->root; ?>
ServerName <?php print $this->uri; ?>
SetEnv db_type <?php print urlencode($db_type); ?>
SetEnv db_name <?php print urlencode($db_name); ?>
SetEnv db_user <?php print urlencode($db_user); ?>
SetEnv db_passwd <?php print urlencode($db_passwd); ?>
SetEnv db_host <?php print urlencode($db_host); ?>
SetEnv db_port <?php print urlencode($db_port); ?>
<?php
if (sizeof($this->aliases)) {
foreach ($this->aliases as $alias) {
print " ServerAlias " . $alias . "\n";
}
}
?>
<IfModule mod_rewrite.c>
RewriteEngine on
<?php
if ($this->redirection || $ssl_redirection) {
if ($ssl_redirection && !$this->redirection) {
print " # Redirect aliases in non-ssl to the same alias on ssl.\n";
print " RewriteRule ^/*(.*)$ https://%{HTTP_HOST}/$1 [NE,L,R=301]\n";
}
elseif ($ssl_redirection && $this->redirection) {
print " # Redirect all aliases + main uri to the main https uri.\n";
print " RewriteRule ^/*(.*)$ https://{$this->uri}/$1 [NE,L,R=301]\n";
}
elseif (!$ssl_redirection && $this->redirection) {
print " # Redirect all aliases to the main http url.\n";
print " RewriteCond %{HTTP_HOST} !^{$this->redirection}$ [NC]\n";
print " RewriteRule ^/*(.*)$ http://{$this->redirection}/$1 [NE,L,R=301]\n";
}
}
?>
RewriteRule ^/files/(.*)$ /sites/<?php print $this->uri; ?>/files/$1 [L]
RewriteCond <?php print $this->site_path; ?>/files/robots.txt -f
RewriteRule ^/robots.txt /sites/<?php print $this->uri; ?>/files/robots.txt [L]
</IfModule>
<?php print $extra_config; ?>
# Error handler for Drupal > 4.6.7
<Directory ~ "sites/.*/files">
<Files *>
SetHandler This_is_a_Drupal_security_line_do_not_remove
</Files>
Options None
Options +FollowSymLinks
# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
</Directory>
# Prevent direct reading of files in the private dir.
# This is for Drupal7 compatibility, which would normally drop
# a .htaccess in those directories, but we explicitly ignore those
<Directory "<?php print $this->site_path; ?>/private/" >
<Files *>
SetHandler This_is_a_Drupal_security_line_do_not_remove
</Files>
Deny from all
Options None
Options +FollowSymLinks
# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
</Directory>
<?php
$if_subsite = $this->data['http_subdird_path'] . '/' . $this->uri;
if (provision_hosting_feature_enabled('subdirs') && provision_file()->exists($if_subsite)->status()) {
print " Include " . $if_subsite . "/*.conf\n";
}
?>
</VirtualHost>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment