Compare commits

...
14 Commits
Author SHA1 Message Date
Barry vd. Heuvel 9a67d9132f Skip decrement/increment
They are a bit weird.
2014-08-26 10:54:34 +02:00
Barry vd. Heuvel bd33e05304 Don't add magic methods
Fixes #105
2014-08-26 10:53:32 +02:00
Barry vd. Heuvel 83bdb7acd7 Use shortname instead of alias
Fixes #106
2014-08-26 10:49:07 +02:00
Barry vd. Heuvel 4fc9c671e8 Add getShortName
For #106
2014-08-26 10:48:29 +02:00
Barry vd. Heuvel 6c18c3f39a Add json format 2014-08-19 09:49:07 +02:00
Barry vd. Heuvel 19aa77ac49 Indent with 8 spaces instead of 2 tabs
Similar to the rest.
2014-08-12 14:34:30 +02:00
Barry vd. Heuvel 25ec6bd1ea Remove message. 2014-08-12 09:14:43 +02:00
Barry vd. Heuvel 1b608fc446 Get correct namespace
The other one was empty, now it should be correct. Fixes #97
2014-08-11 13:26:11 +02:00
Barry vd. Heuvel 09055582af Add correct context for inheritdoc
Attempt 2 to fix #97
2014-08-11 13:05:28 +02:00
Barry vd. Heuvel 4cfcabb63d Normalize return/params from inheritdocs before appending
This normalizes return/param tags, before the tags are added to the docblock. This will preserve the correct context (and namespace).
Should fix #93
2014-08-11 12:07:00 +02:00
Barry vd. Heuvel cbfd53e0f9 Merge pull request #99 from alxy/patch-1
Fix a copy paste error (probably)
2014-08-11 11:01:57 +02:00
Barry vd. Heuvel fd11d89fa7 Clarify trait/interface message in model generator
Makes it clear why a trait/interface is skipped.
Fixes #98
2014-08-11 11:01:19 +02:00
alxy 7f335274bf Fix a copy paste error (probably)
I don't use this helper, however, I guess that line wasnt correct.
2014-08-11 10:30:52 +02:00
Barry vd. Heuvel c01769aebd Add note to config for interfaces 2014-08-10 10:02:50 +02:00
7 changed files with 113 additions and 31 deletions
+14 -6
View File
@@ -12,11 +12,11 @@ namespace Barryvdh\LaravelIdeHelper;
class Alias class Alias
{ {
protected $alias; protected $alias;
protected $facade; protected $facade;
protected $extends = null; protected $extends = null;
protected $classType = 'class'; protected $classType = 'class';
protected $short;
protected $namespace = '__root'; protected $namespace = '__root';
protected $root = null; protected $root = null;
protected $classes = array(); protected $classes = array();
@@ -42,7 +42,6 @@ class Alias
$facade = '\\' . ltrim($facade, '\\'); $facade = '\\' . ltrim($facade, '\\');
$this->facade = $facade; $this->facade = $facade;
$this->detectRoot(); $this->detectRoot();
if ((!$this->isTrait() && $this->root)) { if ((!$this->isTrait() && $this->root)) {
@@ -54,8 +53,10 @@ class Alias
$this->addClass($this->root); $this->addClass($this->root);
$this->detectNamespace(); $this->detectNamespace();
$this->detectClassType(); $this->detectClassType();
if($facade === '\Illuminate\Database\Eloquent\Model'){
$this->usedMethods = array('decrement', 'increment');
}
} }
/** /**
@@ -114,6 +115,12 @@ class Alias
return $this->alias; return $this->alias;
} }
/**
* Return the short name (without namespace)
*/
public function getShortName(){
return $this->short;
}
/** /**
* Get the namespace from the alias * Get the namespace from the alias
* *
@@ -145,6 +152,8 @@ class Alias
$nsParts = explode('\\', $this->alias); $nsParts = explode('\\', $this->alias);
$this->short = array_pop($nsParts); $this->short = array_pop($nsParts);
$this->namespace = implode('\\', $nsParts); $this->namespace = implode('\\', $nsParts);
}else{
$this->short = $this->alias;
} }
} }
@@ -184,7 +193,6 @@ class Alias
//If it doesn't exist, skip it //If it doesn't exist, skip it
if (!class_exists($root) && !interface_exists($root)) { if (!class_exists($root) && !interface_exists($root)) {
$this->error("Class $root is not found.");
return; return;
} }
@@ -253,7 +261,7 @@ class Alias
if (!in_array($method->name, $this->usedMethods)) { if (!in_array($method->name, $this->usedMethods)) {
// Only add the methods to the output when the root is not the same as the class. // Only add the methods to the output when the root is not the same as the class.
// And don't add the __*() methods // And don't add the __*() methods
if ($this->extends !== $class && $method->name !== '__clone') { if ($this->extends !== $class && substr($method->name, 0, 2) !== '__') {
$this->methods[] = new Method($method, $this->alias, $reflection, $method->name, $this->interfaces); $this->methods[] = new Method($method, $this->alias, $reflection, $method->name, $this->interfaces);
} }
$this->usedMethods[] = $method->name; $this->usedMethods[] = $method->name;
+15 -5
View File
@@ -82,6 +82,14 @@ class GeneratorCommand extends Command
); );
} else { } else {
$filename = $this->argument('filename'); $filename = $this->argument('filename');
$format = $this->option('format');
// Strip the php extension
if (substr($filename, -4, 4) == '.php') {
$filename = substr($filename, 0, -4);
}
$filename .= '.' . $format;
if ($this->option('memory')) { if ($this->option('memory')) {
$this->useMemoryDriver(); $this->useMemoryDriver();
@@ -100,7 +108,7 @@ class GeneratorCommand extends Command
} }
$generator = new Generator($this->config, $this->view, $this->getOutput(), $helpers); $generator = new Generator($this->config, $this->view, $this->getOutput(), $helpers);
$content = $generator->generate(); $content = $generator->generate($format);
$written = $this->files->put($filename, $content); $written = $this->files->put($filename, $content);
if ($written !== false) { if ($written !== false) {
@@ -131,12 +139,11 @@ class GeneratorCommand extends Command
*/ */
protected function getArguments() protected function getArguments()
{ {
$filename = $this->config->get('laravel-ide-helper::filename');
return array( return array(
array( array(
'filename', 'filename', InputArgument::OPTIONAL, 'The path to the helper file', $filename
InputArgument::OPTIONAL,
'The path to the helper file',
$this->config->get('laravel-ide-helper::filename')
), ),
); );
} }
@@ -148,7 +155,10 @@ class GeneratorCommand extends Command
*/ */
protected function getOptions() protected function getOptions()
{ {
$format = $this->config->get('laravel-ide-helper::format');
return array( return array(
array('format', "F", InputOption::VALUE_OPTIONAL, 'The format for the IDE Helper', $format),
array('helpers', "H", InputOption::VALUE_NONE, 'Include the helper files'), array('helpers', "H", InputOption::VALUE_NONE, 'Include the helper files'),
array('memory', "M", InputOption::VALUE_NONE, 'Use sqlite memory driver'), array('memory', "M", InputOption::VALUE_NONE, 'Use sqlite memory driver'),
array('sublime', "S", InputOption::VALUE_NONE, 'DEPRECATED: Use different style for SublimeText CodeIntel'), array('sublime', "S", InputOption::VALUE_NONE, 'DEPRECATED: Use different style for SublimeText CodeIntel'),
+2
View File
@@ -175,6 +175,8 @@ class ModelsCommand extends Command
} catch (\Exception $e) { } catch (\Exception $e) {
$this->error("Exception: " . $e->getMessage() . "\nCould not analyze class $name."); $this->error("Exception: " . $e->getMessage() . "\nCould not analyze class $name.");
} }
} elseif (interface_exists($name) || (function_exists('trait_exists') && trait_exists($name))) {
$this->info("Skipping interface/trait $name");
} else { } else {
$this->error("Class $name does not exist"); $this->error("Class $name does not exist");
} }
+41 -2
View File
@@ -56,9 +56,21 @@ class Generator
/** /**
* Generate the helper file contents; * Generate the helper file contents;
* *
* @param string $format The format to generate the helper in (php/json)
* @return string; * @return string;
*/ */
public function generate() public function generate($format = 'php')
{
// Check if the generator for this format exists
$method = 'generate'.ucfirst($format).'Helper';
if (method_exists($this, $method)) {
return $this->$method();
}
return $this->generatePhpHelper();
}
public function generatePhpHelper()
{ {
$app = app(); $app = app();
return $this->view->make('laravel-ide-helper::ide-helper') return $this->view->make('laravel-ide-helper::ide-helper')
@@ -68,10 +80,37 @@ class Generator
->render(); ->render();
} }
public function generateJsonHelper()
{
$classes = array();
foreach ($this->getNamespaces() as $aliases) {
foreach($aliases as $alias) {
$functions = array();
foreach ($alias->getMethods() as $method) {
$functions[$method->getName()] = '('. $method->getParamsWithDefault().')';
}
$classes[$alias->getAlias()] = array(
'functions' => $functions,
);
}
}
$flags = JSON_FORCE_OBJECT;
if (defined('JSON_PRETTY_PRINT')) {
$flags |= JSON_PRETTY_PRINT;
}
return json_encode(array(
'php' => array(
'classes' => $classes,
),
), $flags);
}
protected function detectDrivers() protected function detectDrivers()
{ {
try{ try{
if (class_exists('Cache')) { if (class_exists('Auth')) {
$class = get_class(\Auth::driver()); $class = get_class(\Auth::driver());
$this->extra['Auth'] = array($class); $this->extra['Auth'] = array($class);
$this->interfaces['\Illuminate\Auth\UserProviderInterface'] = $class; $this->interfaces['\Illuminate\Auth\UserProviderInterface'] = $class;
+24 -13
View File
@@ -52,9 +52,9 @@ class Method
//Normalize the description and inherit the docs from parents/interfaces //Normalize the description and inherit the docs from parents/interfaces
try { try {
$this->normalizeDescription(); $this->normalizeParams($this->phpdoc);
$this->normalizeParams(); $this->normalizeReturn($this->phpdoc);
$this->normalizeReturn(); $this->normalizeDescription($this->phpdoc);
} catch (\Exception $e) {} } catch (\Exception $e) {}
//Get the parameters, including formatted default values //Get the parameters, including formatted default values
@@ -136,11 +136,12 @@ class Method
/** /**
* Get the description and get the inherited docs. * Get the description and get the inherited docs.
* *
* @param DocBlock $phpdoc
*/ */
protected function normalizeDescription() protected function normalizeDescription(DocBlock $phpdoc)
{ {
//Get the short + long description from the DocBlock //Get the short + long description from the DocBlock
$description = $this->phpdoc->getText(); $description = $phpdoc->getText();
//Loop through parents/interfaces, to fill in {@inheritdoc} //Loop through parents/interfaces, to fill in {@inheritdoc}
if (strpos($description, '{@inheritdoc}') !== false) { if (strpos($description, '{@inheritdoc}') !== false) {
@@ -148,14 +149,18 @@ class Method
$inheritDescription = $inheritdoc->getText(); $inheritDescription = $inheritdoc->getText();
$description = str_replace('{@inheritdoc}', $inheritDescription, $description); $description = str_replace('{@inheritdoc}', $inheritDescription, $description);
$this->phpdoc->setText($description); $phpdoc->setText($description);
$this->normalizeParams($inheritdoc);
$this->normalizeReturn($inheritdoc);
//Add the tags that are inherited //Add the tags that are inherited
$inheritTags = $inheritdoc->getTags(); $inheritTags = $inheritdoc->getTags();
if ($inheritTags) { if ($inheritTags) {
/** @var Tag $tag */
foreach ($inheritTags as $tag) { foreach ($inheritTags as $tag) {
$tag->setDocBlock(); $tag->setDocBlock();
$this->phpdoc->appendTag($tag); $phpdoc->appendTag($tag);
} }
} }
} }
@@ -163,11 +168,13 @@ class Method
/** /**
* Normalize the parameters * Normalize the parameters
*
* @param DocBlock $phpdoc
*/ */
protected function normalizeParams() protected function normalizeParams(DocBlock $phpdoc)
{ {
//Get the return type and adjust them for beter autocomplete //Get the return type and adjust them for beter autocomplete
$paramTags = $this->phpdoc->getTagsByName('param'); $paramTags = $phpdoc->getTagsByName('param');
if ($paramTags) { if ($paramTags) {
/** @var ParamTag $tag */ /** @var ParamTag $tag */
foreach($paramTags as $tag){ foreach($paramTags as $tag){
@@ -184,11 +191,13 @@ class Method
/** /**
* Normalize the return tag (make full namespace, replace interfaces) * Normalize the return tag (make full namespace, replace interfaces)
*
* @param DocBlock $phpdoc
*/ */
protected function normalizeReturn() protected function normalizeReturn(DocBlock $phpdoc)
{ {
//Get the return type and adjust them for beter autocomplete //Get the return type and adjust them for beter autocomplete
$returnTags = $this->phpdoc->getTagsByName('return'); $returnTags = $phpdoc->getTagsByName('return');
if ($returnTags) { if ($returnTags) {
/** @var ReturnTag $tag */ /** @var ReturnTag $tag */
$tag = reset($returnTags); $tag = reset($returnTags);
@@ -275,7 +284,7 @@ class Method
/** /**
* @param \ReflectionMethod $reflectionMethod * @param \ReflectionMethod $reflectionMethod
* @return string * @return DocBlock
*/ */
protected function getInheritDoc($reflectionMethod) protected function getInheritDoc($reflectionMethod)
{ {
@@ -288,7 +297,9 @@ class Method
$method = $reflectionMethod->getPrototype(); $method = $reflectionMethod->getPrototype();
} }
if ($method) { if ($method) {
$phpdoc = new DocBlock($method); $namespace = $method->getDeclaringClass()->getNamespaceName();
$phpdoc = new DocBlock($method, new Context($namespace));
if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) { if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) {
//Not at the end yet, try another parent/interface.. //Not at the end yet, try another parent/interface..
return $this->getInheritDoc($method); return $this->getInheritDoc($method);
+15 -3
View File
@@ -4,14 +4,15 @@ return array(
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Filename | Filename & Format
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| The default path to the helper file | The default filename (without extension) and the format (php or json)
| |
*/ */
'filename' => '_ide_helper.php', 'filename' => '_ide_helper',
'format' => 'php',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@@ -71,6 +72,17 @@ return array(
'emergency' => 'Monolog\Logger::addEmergency', 'emergency' => 'Monolog\Logger::addEmergency',
) )
), ),
/*
|--------------------------------------------------------------------------
| Interface implementations
|--------------------------------------------------------------------------
|
| These interfaces will be replaced with the implementing class. Some interfaces
| are detected by the helpers, others can be listed below.
|
*/
'interfaces' => array( 'interfaces' => array(
'\Illuminate\Auth\UserInterface' => '\User', '\Illuminate\Auth\UserInterface' => '\User',
) )
+2 -2
View File
@@ -16,10 +16,10 @@ namespace <?= $namespace == '__root' ? '' : $namespace ?>{
<?php endif; ?> <?php endif; ?>
<?php foreach($aliases as $alias): ?> <?php foreach($aliases as $alias): ?>
<?= $alias->getClassType() ?> <?= $alias->getAlias() ?> <?= $alias->getExtends() ? 'extends ' . $alias->getExtends() : '' ?>{ <?= $alias->getClassType() ?> <?= $alias->getShortName() ?> <?= $alias->getExtends() ? 'extends ' . $alias->getExtends() : '' ?>{
<?php foreach($alias->getMethods() as $method): ?> <?php foreach($alias->getMethods() as $method): ?>
<?= trim($method->getDocComment()) ?> <?= trim($method->getDocComment(' ')) ?>
public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>){<?php if($method->getDeclaringClass() !== $method->getRoot()): ?> public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>){<?php if($method->getDeclaringClass() !== $method->getRoot()): ?>