From 6c18c3f39a2935a20b80c6da3403d74a61a17903 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 19 Aug 2014 09:49:07 +0200 Subject: [PATCH] Add json format --- src/Console/GeneratorCommand.php | 20 ++++++++++++---- src/Generator.php | 41 +++++++++++++++++++++++++++++++- src/config/config.php | 7 +++--- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/Console/GeneratorCommand.php b/src/Console/GeneratorCommand.php index 7c40046..e1a9477 100644 --- a/src/Console/GeneratorCommand.php +++ b/src/Console/GeneratorCommand.php @@ -82,6 +82,14 @@ class GeneratorCommand extends Command ); } else { $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')) { $this->useMemoryDriver(); @@ -100,7 +108,7 @@ class GeneratorCommand extends Command } $generator = new Generator($this->config, $this->view, $this->getOutput(), $helpers); - $content = $generator->generate(); + $content = $generator->generate($format); $written = $this->files->put($filename, $content); if ($written !== false) { @@ -131,12 +139,11 @@ class GeneratorCommand extends Command */ protected function getArguments() { + $filename = $this->config->get('laravel-ide-helper::filename'); + return array( array( - 'filename', - InputArgument::OPTIONAL, - 'The path to the helper file', - $this->config->get('laravel-ide-helper::filename') + 'filename', InputArgument::OPTIONAL, 'The path to the helper file', $filename ), ); } @@ -148,7 +155,10 @@ class GeneratorCommand extends Command */ protected function getOptions() { + $format = $this->config->get('laravel-ide-helper::format'); + 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('memory', "M", InputOption::VALUE_NONE, 'Use sqlite memory driver'), array('sublime', "S", InputOption::VALUE_NONE, 'DEPRECATED: Use different style for SublimeText CodeIntel'), diff --git a/src/Generator.php b/src/Generator.php index 27444ed..8420862 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -56,9 +56,21 @@ class Generator /** * Generate the helper file contents; * + * @param string $format The format to generate the helper in (php/json) * @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(); return $this->view->make('laravel-ide-helper::ide-helper') @@ -68,6 +80,33 @@ class Generator ->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() { try{ diff --git a/src/config/config.php b/src/config/config.php index fee6985..57f3990 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -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', /* |--------------------------------------------------------------------------