Skip to content

Commit

Permalink
Merge pull request #53 from moderntribe/feature/xdebug-command
Browse files Browse the repository at this point in the history
Feature/xdebug command
  • Loading branch information
defunctl authored Nov 13, 2020
2 parents 3953fdc + f4975fb commit 3410c91
Show file tree
Hide file tree
Showing 6 changed files with 1,208 additions and 510 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ Note: Test run in the `php-tests` container, however older projects may not have
1. Run tests in a different container: `so test --container=php-fpm -- run integration`.
1. Run tests with xdebug: `so test -x -- run integration`

### Enable/disable xdebug

**Disabling xdebug when you don't need it can improve loading performance, especially on MacOS.**

Note: This setting is **not persistent**. The default in the php-fpm container is `on`. You'll need to run `so xdebug
off` each time after starting or restarting projects.

1. cd anywhere in your SquareOne project.
1. show the current status: `so xdebug`
1. disable xdebug: `so xdebug off`
1. enabled xdebug: `so xdebug on`

### Migrate a production database to your local

If you've exported a project database for a project, we'll attempt to automatically configure it.
Expand Down
143 changes: 143 additions & 0 deletions app/Commands/LocalDocker/Xdebug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php declare( strict_types=1 );

namespace App\Commands\LocalDocker;

use App\Commands\DockerCompose;
use App\Services\Docker\Local\Config;
use Illuminate\Support\Facades\Artisan;

/**
* Enable/Disable Xdebug in the php-fpm container.
*
* @package App\Commands\LocalDocker
*/
class Xdebug extends BaseLocalDocker {

/**
* The path to the xdebug.ini in the container.
*/
public const XDEBUG_CONFIG_PATH = '/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini';

/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'xdebug {action? : on|off}';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Enable/disable Xdebug in the php-fpm container to increase performance on MacOS';

/**
* Execute the console command.
*
* @param \App\Services\Docker\Local\Config $config
*
* @return int
*/
public function handle( Config $config ) {
$action = $this->argument( 'action' );

chdir( $config->getDockerDir() );

if ( empty( $action ) ) {
$result = Artisan::call( DockerCompose::class, [
'--project-name',
$config->getProjectName(),
'exec',
'--user',
'root',
'php-fpm',
'bash',
'-c',
sprintf( '[[ -f %s ]]', self::XDEBUG_CONFIG_PATH ),
] );

if ( self::EXIT_SUCCESS === $result ) {
$this->info( 'xdebug is on' );
} else {
$this->info( 'xdebug is off' );
}

return self::EXIT_SUCCESS;
}

if ( 'on' === $action ) {
$this->enable( $config );
$this->reload( $config );
$this->info( 'xdebug enabled' );
} elseif ( 'off' === $action ) {
$this->disable( $config );
$this->reload( $config );
$this->info( 'xdebug disabled' );
} else {
$this->error( sprintf( 'Invalid argument: %s. Allowed values: on|off', $action ) );
}

return self::EXIT_SUCCESS;
}

/**
* Enable xdebug by renaming the .ini file.
*
* @param \App\Services\Docker\Local\Config $config
*/
protected function enable( Config $config ): void {
Artisan::call( DockerCompose::class, [
'--project-name',
$config->getProjectName(),
'exec',
'-T',
'--user',
'root',
'php-fpm',
'mv',
sprintf( '%s', self::XDEBUG_CONFIG_PATH . '.disabled' ),
sprintf( '%s', self::XDEBUG_CONFIG_PATH ),
] );
}

/**
* Disable xdebug by renaming the .ini file back.
*
* @param \App\Services\Docker\Local\Config $config
*/
protected function disable( Config $config ): void {
Artisan::call( DockerCompose::class, [
'--project-name',
$config->getProjectName(),
'exec',
'-T',
'--user',
'root',
'php-fpm',
'mv',
sprintf( '%s', self::XDEBUG_CONFIG_PATH ),
sprintf( '%s', self::XDEBUG_CONFIG_PATH . '.disabled' ),
] );
}

/**
* Reload PHP in the container.
*
* @param \App\Services\Docker\Local\Config $config
*/
protected function reload( Config $config ): void {
Artisan::call( DockerCompose::class, [
'--project-name',
$config->getProjectName(),
'exec',
'--user',
'root',
'php-fpm',
'kill',
'-USR2',
'1',
] );
}

}
Loading

0 comments on commit 3410c91

Please sign in to comment.