diff --git a/docs/README.md b/docs/README.md index 7020fbd3..29a895d0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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`. diff --git a/inc/composer/class-command.php b/inc/composer/class-command.php index 0ad6a296..4c8d31c5 100644 --- a/inc/composer/class-command.php +++ b/inc/composer/class-command.php @@ -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 ); @@ -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 ); } @@ -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; @@ -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; @@ -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; @@ -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 ) ); @@ -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; @@ -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 ) { @@ -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 ); + } }