Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow project subdomain to be configured. #40

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'] ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you had to check each stage of the array tree when using isset() so I've used null coalescing in most cases like this. If there're no warnings then TIL!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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