Skip to content

Commit

Permalink
Merge pull request #40 from humanmade/configure-sub-domain
Browse files Browse the repository at this point in the history
Allow project subdomain to be configured.
  • Loading branch information
joehoyle authored Jul 8, 2019
2 parents 9cfb590 + b3291e5 commit edfb34c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
16 changes: 16 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ To access your site visit: https://my-site.altis.dev/

Visiting your site's URL should now work. Visit `/wp-admin/` and login with `admin` / `admin` to get started!

The subdomain used for the project can be configured via the `modules.local-server.name` setting:

```json
{
"extra": {
"altis": {
"modules": {
"local-server": {
"name": "my-project"
}
}
}
}
}
```

## Stopping the Local Server

To stop the Local Server containers, simply run `composer local-server stop`.
36 changes: 27 additions & 9 deletions inc/composer/class-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function start( InputInterface $input, OutputInterface $output ) {

$compose = new Process( 'docker-compose up -d', 'vendor/altis/local-server/docker', [
'VOLUME' => getcwd(),
'COMPOSE_PROJECT_NAME' => basename( getcwd() ),
'COMPOSE_PROJECT_NAME' => $this->get_project_subdomain(),
'PATH' => getenv( 'PATH' ),
] );
$compose->setTimeout( 0 );
Expand Down Expand Up @@ -115,7 +115,7 @@ protected function start( InputInterface $input, OutputInterface $output ) {
$output->writeln( 'Installed database.' );
}

$site_url = 'https://' . basename( getcwd() ) . '.altis.dev/';
$site_url = 'https://' . $this->get_project_subdomain() . '.altis.dev/';
$output->writeln( 'Startup completed.' );
$output->writeln( 'To access your site visit: ' . $site_url );
}
Expand All @@ -128,7 +128,7 @@ protected function stop( InputInterface $input, OutputInterface $output ) {

$compose = new Process( 'docker-compose stop', 'vendor/altis/local-server/docker', [
'VOLUME' => getcwd(),
'COMPOSE_PROJECT_NAME' => basename( getcwd() ),
'COMPOSE_PROJECT_NAME' => $this->get_project_subdomain(),
] );
$compose->run( function ( $type, $buffer ) {
echo $buffer;
Expand All @@ -145,7 +145,7 @@ protected function destroy( InputInterface $input, OutputInterface $output ) {

$compose = new Process( 'docker-compose down -v', 'vendor/altis/local-server/docker', [
'VOLUME' => getcwd(),
'COMPOSE_PROJECT_NAME' => basename( getcwd() ),
'COMPOSE_PROJECT_NAME' => $this->get_project_subdomain(),
] );
$compose->run( function ( $type, $buffer ) {
echo $buffer;
Expand All @@ -160,7 +160,7 @@ protected function restart( InputInterface $input, OutputInterface $output ) {
}

protected function cli( InputInterface $input, OutputInterface $output ) {
$site_url = 'https://' . basename( getcwd() ) . '.altis.dev/';
$site_url = 'https://' . $this->get_project_subdomain() . '.altis.dev/';
$options = $input->getArgument( 'options' );

$passed_url = false;
Expand Down Expand Up @@ -195,7 +195,7 @@ protected function cli( InputInterface $input, OutputInterface $output ) {
'cd %s; VOLUME=%s COMPOSE_PROJECT_NAME=%s docker-compose exec %s -u nobody php wp %s',
'vendor/altis/local-server/docker',
getcwd(),
basename( getcwd() ),
$this->get_project_subdomain(),
$has_stdin || ! posix_isatty( STDOUT ) ? '-T' : '', // forward wp-cli's isPiped detection
implode( ' ', $options )
);
Expand All @@ -208,7 +208,7 @@ protected function cli( InputInterface $input, OutputInterface $output ) {
protected function status( InputInterface $input, OutputInterface $output ) {
$compose = new Process( 'docker-compose ps', 'vendor/altis/local-server/docker', [
'VOLUME' => getcwd(),
'COMPOSE_PROJECT_NAME' => basename( getcwd() ),
'COMPOSE_PROJECT_NAME' => $this->get_project_subdomain(),
] );
$compose->run( function ( $type, $buffer ) {
echo $buffer;
Expand All @@ -219,7 +219,7 @@ protected function logs( InputInterface $input, OutputInterface $output ) {
$log = $input->getArgument( 'options' )[0];
$compose = new Process( 'docker-compose logs -f ' . $log, 'vendor/altis/local-server/docker', [
'VOLUME' => getcwd(),
'COMPOSE_PROJECT_NAME' => basename( getcwd() ),
'COMPOSE_PROJECT_NAME' => $this->get_project_subdomain(),
] );
$compose->setTimeout( 0 );
$compose->run( function ( $type, $buffer ) {
Expand All @@ -232,9 +232,27 @@ protected function shell( InputInterface $input, OutputInterface $output ) {
'cd %s; VOLUME=%s COMPOSE_PROJECT_NAME=%s docker-compose exec php /bin/bash',
'vendor/altis/local-server/docker',
getcwd(),
basename( getcwd() )
$this->get_project_subdomain()
), $return_val );

return $return_val;
}

/**
* Get the name of the project for the local subdomain
*
* @return string
*/
protected function get_project_subdomain() : string {

$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'];
} else {
$project_name = basename( getcwd() );
}

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

0 comments on commit edfb34c

Please sign in to comment.