Skip to content

Commit

Permalink
Add meta command for phpstorm
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Mar 16, 2015
1 parent 81b7feb commit 70c06f5
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
97 changes: 97 additions & 0 deletions src/Console/MetaCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Laravel IDE Helper Generator
*
* @author Barry vd. Heuvel <[email protected]>
* @copyright 2015 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link https://github.com/barryvdh/laravel-ide-helper
*/

namespace Barryvdh\LaravelIdeHelper\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;

/**
* A command to generate phpstorm meta data
*
* @author Barry vd. Heuvel <[email protected]>
*/
class MetaCommand extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = 'ide-helper:meta';
protected $filename = '.phpstorm.meta.php';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate metadata for PhpStorm';

/** @var \Illuminate\Filesystem\Filesystem */
protected $files;

/** @var \Illuminate\View\Factory */
protected $view;

/**
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Illuminate\View\Factory $view
*/
public function __construct($files, $view) {
$this->files = $files;
$this->view = $view;
parent::__construct();
}

/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$filename = $this->option('filename');

$bindings = array();

foreach ($this->laravel->getBindings() as $abstract => $options) {
try {
$concrete = $this->laravel->make($abstract);
if (is_object($concrete)) {
$bindings[$abstract] = get_class($concrete);
}
}catch (\Exception $e) {
$this->error("Cannot make $abstract: ".$e->getMessage());
}
}
$content = $this->view->make('laravel-ide-helper::meta', ['bindings' => $bindings])->render();
$written = $this->files->put($filename, $content);

if ($written !== false) {
$this->info("A new meta file was written to $filename");
} else {
$this->error("The meta file could not be created at $filename");
}
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('filename', 'F', InputOption::VALUE_OPTIONAL, 'The path to the meta file', $this->filename),
);
}
}
8 changes: 7 additions & 1 deletion src/IdeHelperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ function () {
return new ModelsCommand();
}
);

$this->app['command.ide-helper.meta'] = $this->app->share(
function ($app) {
return new MetaCommand($app['files'], $app['view']);
}
);

$this->commands('command.ide-helper.generate', 'command.ide-helper.models');
$this->commands('command.ide-helper.generate', 'command.ide-helper.models', 'command.ide-helper.meta');
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/views/meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?= '<?php' ?> namespace PHPSTORM_META {

/**
* PhpStorm Meta file, to provide autocomplete information for PhpStorm
* Generated on <?= date("Y-m-d") ?>.
*
* @author Barry vd. Heuvel <[email protected]>
* @see https://github.com/barryvdh/laravel-ide-helper
*/

/** @noinspection PhpUnusedLocalVariableInspection */
/** @noinspection PhpIllegalArrayKeyTypeInspection */
$STATIC_METHOD_TYPES = [
\App::make('') => [
<?php foreach($bindings as $abstract => $class): ?>
'<?= $abstract ?>' instanceof \<?= $class ?>,
<?php endforeach ?> ],
app('') => [
<?php foreach($bindings as $abstract => $class): ?>
'<?= $abstract ?>' instanceof \<?= $class ?>,
<?php endforeach ?> ],
];

}

0 comments on commit 70c06f5

Please sign in to comment.