Skip to content

Instantly share code, notes, and snippets.

@Caffe1neAdd1ct
Last active June 25, 2020 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Caffe1neAdd1ct/826e75a8448575442c464357efcfd429 to your computer and use it in GitHub Desktop.
Save Caffe1neAdd1ct/826e75a8448575442c464357efcfd429 to your computer and use it in GitHub Desktop.
Laravel IDE Helper Netbeans
<?php
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem as File;
/**
* Modify Generated IDE Helper Files for compatibility with NetBeans
*/
class ComposerIdeHelperNetbeans extends Command
{
protected $name = 'composer:netbeans';
protected $description = 'Run post-composer netbeans ide helper compatability commands';
/**
* Removes class references on @method static hints
*/
public function fire()
{
if(!$this->laravel->environment('local')) {
$this->info($this->name . ' App not running locally, refusing to run netbeans autocomplete helper.');
return true;
}
$ideHelperModelFilePath = base_path('_ide_helper_models.php');
$filesystem = new File();
if(!$filesystem->exists($ideHelperModelFilePath)) {
$this->error($this->name . ' Helper Models file unavailable: ' . $ideHelperModelFilePath);
return false;
}
$content = $filesystem->get($ideHelperModelFilePath);
if(!$content) {
$this->error('Failed to retrieve file content ' . $ideHelperModelFilePath);
return false;
}
$newContentPostExtends = $this->addEloquentExtends($this->replaceStatic($content));
$newContentPostMethods = $this->addEloquentMethods($this->replaceStatic($newContentPostExtends));
$writeResult = $filesystem->put($ideHelperModelFilePath, $newContentPostMethods);
if(!$writeResult) {
$this->error('Failed to write new content to ' . $ideHelperModelFilePath);
return false;
}
return;
}
protected function replaceStatic($content)
{
return preg_replace("/\@method static \\\\(.*)\s([a-zA-Z0-9]+)\((.*)\)/", "@method static $2($3)", $content);
}
protected function addEloquentExtends($content)
{
return preg_replace("/class ([a-zA-Z]+) \{\}/", "class $1 extends \\Illuminate\\Database\\Eloquent\\Model {}", $content);
}
protected function addEloquentMethods($content)
{
return preg_replace("/\s\*\/\n[\s{4}]class/", " * @method static with(\$array)\n * @method \\Illuminate\\Database\\Eloquent\\Builder where(\$column, \$operator = null, \$value = null, \$boolean = 'and')\n * @method static first(\$columns)\n */\n class", $content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment