Re-format source code of Generator class

This commit is contained in:
Thach Nguyen
2018-01-27 03:21:08 +07:00
parent 501ef7045a
commit 042d341b65
+39 -29
View File
@@ -10,7 +10,7 @@
namespace Barryvdh\LaravelIdeHelper; namespace Barryvdh\LaravelIdeHelper;
use Illuminate\Foundation\AliasLoader; use /** @noinspection PhpUndefinedNamespaceInspection,PhpUndefinedClassInspection */Illuminate\Foundation\AliasLoader;
use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Config\Repository as ConfigRepository;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@@ -36,11 +36,8 @@ class Generator
* @param \Symfony\Component\Console\Output\OutputInterface $output * @param \Symfony\Component\Console\Output\OutputInterface $output
* @param string $helpers * @param string $helpers
*/ */
public function __construct(ConfigRepository $config, public function __construct(ConfigRepository $config, $view, OutputInterface $output = null, $helpers = '')
/* Illuminate\View\Factory */ $view, {
OutputInterface $output = null,
$helpers = ''
) {
$this->config = $config; $this->config = $config;
$this->view = $view; $this->view = $view;
@@ -50,6 +47,7 @@ class Generator
$this->extra = array_merge($this->extra, $this->config->get('laravel-ide-helper::extra')); $this->extra = array_merge($this->extra, $this->config->get('laravel-ide-helper::extra'));
$this->magic = array_merge($this->magic, $this->config->get('laravel-ide-helper::magic')); $this->magic = array_merge($this->magic, $this->config->get('laravel-ide-helper::magic'));
$this->interfaces = array_merge($this->interfaces, $this->config->get('laravel-ide-helper::interfaces')); $this->interfaces = array_merge($this->interfaces, $this->config->get('laravel-ide-helper::interfaces'));
// Make all interface classes absolute // Make all interface classes absolute
foreach ($this->interfaces as &$interface) { foreach ($this->interfaces as &$interface) {
$interface = '\\' . ltrim($interface, '\\'); $interface = '\\' . ltrim($interface, '\\');
@@ -60,13 +58,13 @@ class Generator
/** /**
* Generate the helper file contents; * Generate the helper file contents;
* *
* @param string $format The format to generate the helper in (php/json) * @param string $format The format to generate the helper in (php/json)
* @return string; * @return string;
*/ */
public function generate($format = 'php') public function generate($format = 'php')
{ {
// Check if the generator for this format exists // Check if the generator for this format exists
$method = 'generate'.ucfirst($format).'Helper'; $method = 'generate' . ucfirst($format) . 'Helper';
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
return $this->$method(); return $this->$method();
} }
@@ -88,10 +86,10 @@ class Generator
{ {
$classes = array(); $classes = array();
foreach ($this->getNamespaces() as $aliases) { foreach ($this->getNamespaces() as $aliases) {
foreach($aliases as $alias) { foreach ($aliases as $alias) {
$functions = array(); $functions = array();
foreach ($alias->getMethods() as $method) { foreach ($alias->getMethods() as $method) {
$functions[$method->getName()] = '('. $method->getParamsWithDefault().')'; $functions[$method->getName()] = '(' . $method->getParamsWithDefault() . ')';
} }
$classes[$alias->getAlias()] = array( $classes[$alias->getAlias()] = array(
'functions' => $functions, 'functions' => $functions,
@@ -113,59 +111,66 @@ class Generator
protected function detectDrivers() protected function detectDrivers()
{ {
try{ try {
if (class_exists('Auth') && is_a('Auth', '\Illuminate\Support\Facades\Auth', true)) { if (class_exists('Auth') && is_a('Auth', '\Illuminate\Support\Facades\Auth', true)) {
/** @noinspection PhpUndefinedClassInspection */
$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;
} }
}catch (\Exception $e) {} } catch (\Exception $e) {}
try{ try {
if (class_exists('DB') && is_a('DB', '\Illuminate\Support\Facades\DB', true)) { if (class_exists('DB') && is_a('DB', '\Illuminate\Support\Facades\DB', true)) {
/** @noinspection PhpUndefinedClassInspection */
$class = get_class(\DB::connection()); $class = get_class(\DB::connection());
$this->extra['DB'] = array($class); $this->extra['DB'] = array($class);
$this->interfaces['\Illuminate\Database\ConnectionInterface'] = $class; $this->interfaces['\Illuminate\Database\ConnectionInterface'] = $class;
} }
}catch (\Exception $e) {} } catch (\Exception $e) {}
try{ try {
if (class_exists('Cache') && is_a('Cache', '\Illuminate\Support\Facades\Cache', true)) { if (class_exists('Cache') && is_a('Cache', '\Illuminate\Support\Facades\Cache', true)) {
/** @noinspection PhpUndefinedClassInspection */
$driver = get_class(\Cache::driver()); $driver = get_class(\Cache::driver());
/** @noinspection PhpUndefinedClassInspection */
$store = get_class(\Cache::getStore()); $store = get_class(\Cache::getStore());
$this->extra['Cache'] = array($driver, $store); $this->extra['Cache'] = array($driver, $store);
$this->interfaces['\Illuminate\Cache\StoreInterface'] = $store; $this->interfaces['\Illuminate\Cache\StoreInterface'] = $store;
} }
}catch (\Exception $e) {} } catch (\Exception $e) {}
try{ try {
if (class_exists('Queue') && is_a('Queue', '\Illuminate\Support\Facades\Queue', true)) { if (class_exists('Queue') && is_a('Queue', '\Illuminate\Support\Facades\Queue', true)) {
/** @noinspection PhpUndefinedClassInspection */
$class = get_class(\Queue::connection()); $class = get_class(\Queue::connection());
$this->extra['Queue'] = array($class); $this->extra['Queue'] = array($class);
$this->interfaces['\Illuminate\Queue\QueueInterface'] = $class; $this->interfaces['\Illuminate\Queue\QueueInterface'] = $class;
} }
}catch (\Exception $e) {} } catch (\Exception $e) {}
try{ try {
if (class_exists('SSH') && is_a('SSH', '\Illuminate\Support\Facades\SSH', true)){ if (class_exists('SSH') && is_a('SSH', '\Illuminate\Support\Facades\SSH', true)) {
/** @noinspection PhpUndefinedClassInspection */
$class = get_class(\SSH::connection()); $class = get_class(\SSH::connection());
$this->extra['SSH'] = array($class); $this->extra['SSH'] = array($class);
$this->interfaces['\Illuminate\Remote\ConnectionInterface'] = $class; $this->interfaces['\Illuminate\Remote\ConnectionInterface'] = $class;
} }
}catch (\Exception $e) {} } catch (\Exception $e) {}
} }
/** /**
* Find all namespaces/aliases that are valid for us to render * Find all namespaces/aliases that are valid for us to render
* *
* @return array * @return array|Alias[][]
*/ */
protected function getNamespaces() protected function getNamespaces()
{ {
$namespaces = array(); $namespaces = array();
// Get all aliases // Get all aliases
/** @noinspection PhpUndefinedClassInspection */
foreach (AliasLoader::getInstance()->getAliases() as $name => $facade) { foreach (AliasLoader::getInstance()->getAliases() as $name => $facade) {
$magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : array(); $magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : array();
$alias = new Alias($name, $facade, $magicMethods, $this->interfaces); $alias = new Alias($name, $facade, $magicMethods, $this->interfaces);
@@ -197,15 +202,20 @@ class Generator
public function getDriver($alias) public function getDriver($alias)
{ {
try { try {
if ($alias == "Auth") { if ($alias == 'Auth') {
/** @noinspection PhpUndefinedClassInspection */
$driver = \Auth::driver(); $driver = \Auth::driver();
} elseif ($alias == "DB") { } elseif ($alias == 'DB') {
/** @noinspection PhpUndefinedClassInspection */
$driver = \DB::connection(); $driver = \DB::connection();
} elseif ($alias == "Cache") { } elseif ($alias == 'Cache') {
/** @noinspection PhpUndefinedClassInspection */
$driver = get_class(\Cache::driver()); $driver = get_class(\Cache::driver());
/** @noinspection PhpUndefinedClassInspection */
$store = get_class(\Cache::getStore()); $store = get_class(\Cache::getStore());
return array($driver, $store); return array($driver, $store);
} elseif ($alias == "Queue") { } elseif ($alias == 'Queue') {
/** @noinspection PhpUndefinedClassInspection */
$driver = \Queue::connection(); $driver = \Queue::connection();
} else { } else {
return false; return false;
@@ -221,14 +231,14 @@ class Generator
/** /**
* Write a string as error output. * Write a string as error output.
* *
* @param string $string * @param string $string
* @return void * @return void
*/ */
protected function error($string) protected function error($string)
{ {
if($this->output){ if ($this->output) {
$this->output->writeln("<error>$string</error>"); $this->output->writeln("<error>$string</error>");
}else{ } else {
echo $string . "\r\n"; echo $string . "\r\n";
} }
} }