Skip to content

Commit

Permalink
Feature: Copy PhpStorm meta file on project start (#120)
Browse files Browse the repository at this point in the history
* Add phpstorm meta file to internal phar storage

* Add PhpStormMeta config service + unit tests

* Copy phpstorm file during project start + feature tests

* Bump to v5.7.0
  • Loading branch information
defunctl authored May 22, 2022
1 parent 8cda1c7 commit 37f0c89
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 10 deletions.
22 changes: 21 additions & 1 deletion app/Commands/LocalDocker/Start.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
use App\Services\ComposerVersion;
use App\Services\Config\Env;
use App\Services\Config\Github;
use App\Services\Config\PhpStormMeta;
use App\Services\Docker\Container;
use App\Services\Docker\Local\Config;
use App\Services\Docker\SystemClock;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Artisan;
use M1\Env\Parser;
use Throwable;

/**
* Local docker start command
Expand Down Expand Up @@ -52,6 +54,7 @@ class Start extends BaseLocalDocker {
* @param \Illuminate\Filesystem\Filesystem $filesystem
* @param \App\Services\Config\Github $github
* @param \App\Services\Config\Env $env
* @param \App\Services\Config\PhpStormMeta $phpStormMeta
* @param \App\Services\Docker\SystemClock $clock
* @param \App\Services\ComposerVersion $composerVersion
* @param \App\Services\Docker\Container $container
Expand All @@ -65,6 +68,7 @@ public function handle(
Filesystem $filesystem,
Github $github,
Env $env,
PhpStormMeta $phpStormMeta,
SystemClock $clock,
ComposerVersion $composerVersion,
Container $container
Expand All @@ -85,6 +89,7 @@ public function handle(
}

$this->prepareComposer( $config, $filesystem, $github );
$this->copyPhpStormMetaFile( $config, $phpStormMeta );

// Start global containers
if ( ! $this->option( 'skip-global') ) {
Expand Down Expand Up @@ -166,7 +171,7 @@ protected function prepareComposer( Config $config, Filesystem $filesystem, Gith
$this->secret( 'We have detected you have not configured a GitHub oAuth token. Please go to https://github.com/settings/tokens/new?scopes=repo and create one or enter an existing token' );

// Save the default token to the so config directory.
$github->save( $token );
$github->save( (string) $token );

// Copy to local project.
$github->copy( $composerDirectory );
Expand Down Expand Up @@ -317,4 +322,19 @@ protected function addEnvFile( Config $config, Env $env, Filesystem $filesystem
$this->info( sprintf( '.env file created at %s', $file ) );
}

protected function copyPhpStormMetaFile( Config $config, PhpStormMeta $phpStormMeta ): void {
$projectRoot = $config->getProjectRoot();
$warning = 'Unable to copy .phpstorm.meta.php file';

if ( ! $phpStormMeta->existsInProject( $projectRoot ) ) {
try {
if ( ! $phpStormMeta->copy( $projectRoot ) ) {
$this->warn( $warning );
}
} catch ( Throwable $e ) {
$this->warn( $warning );
}
}
}

}
5 changes: 5 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use App\Services\Certificate\Trust\Strategies\MacOs;
use App\Services\Config\Env;
use App\Services\Config\Github;
use App\Services\Config\PhpStormMeta;
use App\Services\Docker\Dns\Factory;
use App\Services\Docker\Dns\Handler;
use App\Services\Docker\Dns\OsSupport\BaseSupport;
Expand Down Expand Up @@ -235,6 +236,10 @@ public function register(): void {
->needs( '$directory' )
->give( config( 'squareone.config-dir' ) );

$this->app->when( PhpStormMeta::class )
->needs( '$directory' )
->give( storage_path() );

$this->app->bind( SpinnerInterface::class, Spinner::class );

$this->app->when( MigrationChecker::class )
Expand Down
54 changes: 54 additions & 0 deletions app/Services/Config/PhpStormMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare( strict_types=1 );

namespace App\Services\Config;

use Illuminate\Filesystem\Filesystem;

/**
* Manages the .phpstorm.meta.php file to add better
* code completion for the PHP-DI container.
*/
class PhpStormMeta extends BaseConfig {

public const PHAR_META_FILE = '/phpstorm.meta.php';
public const META_FILE = '/.phpstorm.meta.php';

/**
* Path to the default meta file inside the phar.
*
* @var string
*/
protected $metaFile;

public function __construct( Filesystem $filesystem, string $directory ) {
parent::__construct( $filesystem, $directory );
$this->metaFile = $this->directory . self::PHAR_META_FILE;
}

/**
* Check if this project has a .phpstorm.meta.php file.
*
* @param string $projectRoot
*
* @return bool
*/
public function existsInProject( string $projectRoot ): bool {
return $this->filesystem->exists( $projectRoot . self::META_FILE );
}

/**
* Copy the .phpstorm.meta.php file to the project root.
*
* @param string $projectRoot The root path of the local project.
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*
* @return bool
*/
public function copy( string $projectRoot ): bool {
$content = $this->filesystem->get( $this->metaFile );

return (bool) $this->filesystem->put( $projectRoot . self::META_FILE, $content );
}

}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/

//'version' => app('git.version'),
'version' => '5.6.5',
'version' => '5.7.0',

/*
|--------------------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions storage/defaults/phpstorm.meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

// .phpstorm.meta.php
namespace PHPSTORM_META {
// PHP-DI container code completion
override( \Psr\Container\ContainerInterface::get( 0 ), map( [
'' => '@',
] ) );
override( \DI\Container::get( 0 ), map( [
'' => '@',
] ) );
override( \DI\FactoryInterface::make( 0 ), map( [
'' => '@',
] ) );
override( \DI\Container::make( 0 ), map( [
'' => '@',
] ) );
}
Loading

0 comments on commit 37f0c89

Please sign in to comment.