Commit 4c232c08 authored by anarcat's avatar anarcat

restore crappy vhost and sites.php support

parent ccc5235c
<VirtualHost *:<?php print $http_port; ?>>
ServerName <?php print $uri; ?>
Include <?php print $subdirs_path; ?>/<?php print $uri; ?>/*.conf
</VirtualHost>
<?php
/**
* @file
* Template file to generate a D7 style sites.php for subdirectories.
*/
print "<?php \n"; ?>
/**
* @file
* Configuration file for Drupal's multi-site directory aliasing feature.
*
* Drupal searches for an appropriate configuration directory based on the
* website's hostname and pathname. A detailed description of the rules for
* discovering the configuration directory can be found in the comment
* documentation in 'sites/default/default.settings.php'.
*
* This file allows you to define a set of aliases that map hostnames and
* pathnames to configuration directories. These aliases are loaded prior to
* scanning for directories, and they are exempt from the normal discovery
* rules. The aliases are defined in an associative array named $sites, which
* should look similar to the following:
*
* $sites = array(
* 'devexample.com' => 'example.com',
* 'localhost.example' => 'example.com',
* );
*
* The above array will cause Drupal to look for a directory named
* "example.com" in the sites directory whenever a request comes from
* "example.com", "devexample.com", or "localhost/example". That is useful
* on development servers, where the domain name may not be the same as the
* domain of the live server. Since Drupal stores file paths into the database
* (files, system table, etc.) this will ensure the paths are correct while
* accessed on development servers.
*
* To use this file, copy and rename it such that its path plus filename is
* 'sites/sites.php'. If you don't need to use multi-site directory aliasing,
* then you can safely ignore this file, and Drupal will ignore it too.
*/
/**
* Multi-site directory aliasing:
*
* Edit the lines below to define directory aliases. Remove the leading hash
* signs to enable.
*/
<?php
$aliases = d()->aliases;
// Fix our subdir aliases.
foreach ($aliases as $alias) {
$records[str_replace('/', '.', $alias)] = $records[$alias];
unset($records[$alias]);
}
?>
$sites = <?php print var_export($records, TRUE) ?>;
<?php
/**
* Implements hook_drush_init().
*/
function subdirs_drush_init() {
subdirs_provision_register_autoload();
}
/**
* Register our directory as a place to find Provision classes.
*
* This allows Provision to autoload our classes, so that we don't need to
* specifically include the files before we use the class.
*/
function subdirs_provision_register_autoload() {
static $loaded = FALSE;
if (!$loaded) {
$loaded = TRUE;
provision_autoload_register_prefix('Provision_', dirname(__FILE__));
}
}
/**
* Implements drush_hook_pre_provision_install().
*/
function drush_subdirs_pre_provision_install() {
_subdirs_create_site_configs();
}
/**
* Implements drush_hook_pre_provision_install_rollback().
*/
function drush_subdirs_pre_provision_install_rollback() {
_subdirs_remove_site_configs();
}
/**
* Implementation of drush_hook_pre_provision_verify()
*/
function drush_subdirs_pre_provision_verify() {
_subdirs_create_site_configs();
}
/**
* Implementation of drush_hook_post_provision_deploy()
*/
function drush_subdirs_pre_provision_deploy() {
_subdirs_create_site_configs();
}
/**
* Implementation of drush_hook_provision_delete()
*/
function drush_subdirs_pre_provision_delete() {
_subdirs_remove_site_configs();
}
/**
* Implements drush_hook_pre_provision_enable().
*/
function drush_subdirs_pre_provision_enable() {
_subdirs_create_site_configs();
}
/**
* Implements hook_provision_config_load_templates().
*/
function subdirs_provision_config_load_templates($config) {
if ($config->template == 'provision_drupal_sites.tpl.php') {
$context = d();
$use_subdirs_tpl = FALSE;
foreach ($context->aliases as $alias) {
if (strpos($alias, '/')) {
$use_subdirs_tpl = TRUE;
}
}
if ($use_subdirs_tpl) {
drush_log('Generating sites.php for use with subdirectories.', 'notice');
return dirname(__FILE__) . '/Provision/Config/subdirs_drupal_sites.tpl.php';
}
}
}
/**
* Helper function to create configuration files for subdirectories.
*/
function _subdirs_create_site_configs() {
if (d()->type == 'site') {
$aliases = d()->aliases;
if (count($aliases)) {
foreach ($aliases as $alias) {
if (strpos($alias, '/')) {
drush_log("Subdirectory alias `$alias` found. Creating configuration files.", 'notice');
_subdirs_create_domain_vhost($alias);
_subdirs_create_symlinks($alias, d()->uri);
}
}
}
}
}
/**
* Helper function to remove configuration files for subdirectories.
*/
function _subdirs_remove_site_configs() {
$context = d();
$aliases = $context->aliases;
$subdirs_path = $context->server->http_app_path . '/subdirs.d';
if (count($aliases)) {
foreach ($aliases as $alias) {
// We only want to remove the domain vhost after the last subdirectory
// site has been deleted.
$alias_parts = explode('/', $alias, 2);
$includes_path = $subdirs_path . '/' . $alias_parts[0];
if ($handle = opendir($includes_path)) {
if (FALSE === readdir($handle)) {
_subdirs_delete_domain_vhost($alias);
_subdirs_delete_symlinks($alias);
}
}
}
}
}
/**
* Create the vhost for the subdirectory domain.
*/
function _subdirs_create_domain_vhost($alias) {
$alias_parts = explode('/', $alias, 2);
$context = d();
// Generate variables to pass to template
$variables = array(
'http_port' => $context->server->http_port,
'subdirs_path' => $context->server->http_app_path . '/subdirs.d',
'uri' => $alias_parts[0],
);
$output = _subdirs_render_template('domain_vhost.tpl.php', $variables);
$domain_vhost_path = $context->server->http_vhostd_path . '/' . $alias_parts[0];
if (fwrite(fopen($domain_vhost_path, 'w'), $output)) {
drush_log('Created domain vhost for subdirectory ' . $alias . ' at ' . $domain_vhost_path, 'success');
}
else {
drush_log('Error encountered attempting to create domain vhost for subdirectory ' . $alias, 'error');
}
}
/**
* Delete the vhost for the subdirectory domain.
*/
function _subdirs_delete_domain_vhost($alias) {
$alias_parts = explode('/', $alias, 2);
$context = d();
$domain_vhost_path = $context->server->http_vhostd_path . '/' . $alias_parts[0];
if (unlink($domain_vhost_path)) {
drush_log('Deleted domain vhost for subdirectory ' . $alias, 'success');
}
else {
drush_log('Error encountered attempting to delete domain vhost for subdirectory ' . $alias, 'error');
}
}
/**
* Create subdirectory symlinks
*
* This create a sites/example.com.foo symlink pointing to the main
* URL of the site.
*/
function _subdirs_create_symlinks($alias, $main) {
provision_file()->symlink($main, 'sites/' . str_replace('/', '.', $alias))
->succeed('symlinked @path to @target')
->fail('failed to symlink @path to @target');
}
/**
* Delete subdirectory symlinks
*
* This deletes the symlinks created by _subdirs_create_symlinks().
*/
function _subdirs_delete_symlinks($alias) {
provision_file()->unlink('sites/' . str_replace('/', '.', $alias))
->succeed('removed @path symlink')
->fail('failed to remove @path symlink');
}
/**
* Render our config file templates.
*/
function _subdirs_render_template($template, $variables) {
// TODO: Remove this, once we've got a working Config class.
$template_path = dirname(__FILE__) . '/Provision/Config/' . $template;
extract($variables);
ob_start();
include $template_path;
$content = ob_get_clean();
return $content;
}
<?php
/**
* Implements hook_drush_load().
*
* Checks if the corresponsing Hosting Feature is enabled.
*/
function subdirs_drush_load() {
return in_array('subdirs', drush_get_option('hosting_features', array()));
}
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