Skip to content

Commit

Permalink
Allow custom domain name/tld from #341
Browse files Browse the repository at this point in the history
  • Loading branch information
shadyvb committed Apr 26, 2022
1 parent b0bb233 commit 1be8adf
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
63 changes: 55 additions & 8 deletions inc/composer/class-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ protected function execute( InputInterface $input, OutputInterface $output ) : i
'xdebug' => 'off',
'mutagen' => 'off',
'tmp' => false,
'secure' => $this->get_composer_config()['secure'] ?? true,
];

// If Xdebug switch is passed add to docker compose args.
Expand Down Expand Up @@ -200,6 +201,7 @@ protected function get_env() : array {
return [
'VOLUME' => getcwd(),
'COMPOSE_PROJECT_NAME' => $this->get_project_subdomain(),
'COMPOSE_PROJECT_TLD' => $this->get_project_tld(),
'DOCKER_CLIENT_TIMEOUT' => 120,
'COMPOSE_HTTP_TIMEOUT' => 120,
'PATH' => getenv( 'PATH' ),
Expand Down Expand Up @@ -285,7 +287,7 @@ protected function start( InputInterface $input, OutputInterface $output ) {
$output->writeln( '<info>WP Password:</> <comment>password</>' );
}

$site_url = 'https://' . $this->get_project_subdomain() . '.altis.dev/';
$site_url = $this->get_project_url();
$output->writeln( '<info>Startup completed.</>' );
$output->writeln( '<info>To access your site visit:</> <comment>' . $site_url . '</>' );

Expand Down Expand Up @@ -467,7 +469,7 @@ protected function restart( InputInterface $input, OutputInterface $output ) {
* @return int
*/
protected function exec( InputInterface $input, OutputInterface $output, ?string $program = null ) {
$site_url = 'https://' . $this->get_project_subdomain() . '.altis.dev/';
$site_url = $this->get_project_url();
$options = $input->getArgument( 'options' );

$passed_url = false;
Expand Down Expand Up @@ -868,7 +870,7 @@ protected function get_mkcert_binary() : ?string {
* @return void
*/
protected function generate_docker_compose( array $args = [] ) : void {
$docker_compose = new Docker_Compose_Generator( $this->get_project_subdomain(), getcwd(), $args );
$docker_compose = new Docker_Compose_Generator( $this->get_project_subdomain(), getcwd(), $this->get_project_tld(), $args );
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
file_put_contents(
getcwd() . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'docker-compose.yml',
Expand Down Expand Up @@ -973,25 +975,70 @@ protected function minio_client( string $command ) {
return $return_var;
}

/**
* Get the config from the composer.json project file.
*
* @return array
*/
protected function get_composer_config() : array {
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$composer_json = json_decode( file_get_contents( getcwd() . '/composer.json' ), true );
$config = $composer_json['extra']['altis']['modules']['local-server'] ?? [];

return $config;
}

/**
* Get the root name to use for the project.
*
* @return string
*/
protected function get_project_tld() : string {
$config = $this->get_composer_config();

if ( isset( $config['tld'] ) ) {
$project_name = $config['tld'];
} else {
$project_name = 'altis.dev';
}

return $project_name;
}

/**
* Get the name of the project for the local subdomain
*
* @return string
*/
protected function get_project_subdomain() : string {
$config = $this->get_composer_config();

// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$composer_json = json_decode( file_get_contents( getcwd() . '/composer.json' ), true );

if ( isset( $composer_json['extra']['altis']['modules']['local-server']['name'] ) ) {
$project_name = $composer_json['extra']['altis']['modules']['local-server']['name'];
if ( isset( $config['name'] ) ) {
$project_name = $config['name'];
} else {
$project_name = basename( getcwd() );
}

return preg_replace( '/[^A-Za-z0-9\-\_]/', '', $project_name );
}

/**
* Get the name of the project for the local subdomain
*
* @return string
*/
protected function get_project_url() : string {
$is_secure = $this->get_composer_config()['secure'] ?? true;
$tld = $this->get_project_tld();
$site_url = sprintf(
'http%s://%s%s/',
$is_secure ? 's' : '',
$this->get_project_subdomain(),
$tld ? '.' . $tld : ''
);
return $site_url;
}

/**
* Run a prepared process command for various versions of Symfony Console.
*
Expand Down
9 changes: 5 additions & 4 deletions inc/composer/class-docker-compose-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ class Docker_Compose_Generator {
*
* @param string $project_name The docker compose project name.
* @param string $root_dir The project root directory.
* @param string $tld The primary top level domain for the server.
* @param array $args An optional array of arguments to modify the behaviour of the generator.
*/
public function __construct( string $project_name, string $root_dir, array $args = [] ) {
public function __construct( string $project_name, string $root_dir, string $tld, array $args = [] ) {
$this->project_name = $project_name;
$this->root_dir = $root_dir;
$this->config_dir = dirname( __DIR__, 2 ) . '/docker';
$this->tld = 'altis.dev';
$this->hostname = $this->project_name . '.' . $this->tld;
$this->root_dir = $root_dir;
$this->tld = $tld;
$this->hostname = $this->tld ? $this->project_name . '.' . $this->tld : $this->project_name;
$this->args = $args;
}

Expand Down
2 changes: 1 addition & 1 deletion inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function bootstrap() {
}

if ( empty( $_SERVER['HTTP_HOST'] ) ) {
$_SERVER['HTTP_HOST'] = getenv( 'COMPOSE_PROJECT_NAME' );
$_SERVER['HTTP_HOST'] = getenv( 'COMPOSE_PROJECT_NAME' ) . '.' . getenv( 'COMPOSE_PROJECT_TLD' );
}

defined( 'DB_HOST' ) or define( 'DB_HOST', getenv( 'DB_HOST' ) );
Expand Down

0 comments on commit 1be8adf

Please sign in to comment.