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
+244 -234
View File
@@ -1,235 +1,245 @@
<?php <?php
/** /**
* Laravel IDE Helper Generator * Laravel IDE Helper Generator
* *
* @author Barry vd. Heuvel <[email protected]> * @author Barry vd. Heuvel <[email protected]>
* @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl) * @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
* @license http://www.opensource.org/licenses/mit-license.php MIT * @license http://www.opensource.org/licenses/mit-license.php MIT
* @link https://github.com/barryvdh/laravel-ide-helper * @link https://github.com/barryvdh/laravel-ide-helper
*/ */
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;
class Generator class Generator
{ {
/** @var \Illuminate\Config\Repository */ /** @var \Illuminate\Config\Repository */
protected $config; protected $config;
/** @var \Illuminate\View\Factory */ /** @var \Illuminate\View\Factory */
protected $view; protected $view;
/** @var \Symfony\Component\Console\Output\OutputInterface */ /** @var \Symfony\Component\Console\Output\OutputInterface */
protected $output; protected $output;
protected $extra = array(); protected $extra = array();
protected $magic = array(); protected $magic = array();
protected $interfaces = array(); protected $interfaces = array();
protected $helpers; protected $helpers;
/** /**
* @param \Illuminate\Config\Repository $config * @param \Illuminate\Config\Repository $config
* @param \Illuminate\View\Factory $view * @param \Illuminate\View\Factory $view
* @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, $this->config = $config;
$helpers = '' $this->view = $view;
) {
$this->config = $config; // Find the drivers to add to the extra/interfaces
$this->view = $view; $this->detectDrivers();
// Find the drivers to add to the extra/interfaces $this->extra = array_merge($this->extra, $this->config->get('laravel-ide-helper::extra'));
$this->detectDrivers(); $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->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')); // Make all interface classes absolute
$this->interfaces = array_merge($this->interfaces, $this->config->get('laravel-ide-helper::interfaces')); foreach ($this->interfaces as &$interface) {
// Make all interface classes absolute $interface = '\\' . ltrim($interface, '\\');
foreach ($this->interfaces as &$interface) { }
$interface = '\\' . ltrim($interface, '\\'); $this->helpers = $helpers;
} }
$this->helpers = $helpers;
} /**
* Generate the helper file contents;
/** *
* Generate the helper file contents; * @param string $format The format to generate the helper in (php/json)
* * @return string;
* @param string $format The format to generate the helper in (php/json) */
* @return string; public function generate($format = 'php')
*/ {
public function generate($format = 'php') // Check if the generator for this format exists
{ $method = 'generate' . ucfirst($format) . 'Helper';
// Check if the generator for this format exists if (method_exists($this, $method)) {
$method = 'generate'.ucfirst($format).'Helper'; return $this->$method();
if (method_exists($this, $method)) { }
return $this->$method();
} return $this->generatePhpHelper();
}
return $this->generatePhpHelper();
} public function generatePhpHelper()
{
public function generatePhpHelper() $app = app();
{ return $this->view->make('laravel-ide-helper::ide-helper')
$app = app(); ->with('namespaces', $this->getNamespaces())
return $this->view->make('laravel-ide-helper::ide-helper') ->with('helpers', $this->helpers)
->with('namespaces', $this->getNamespaces()) ->with('version', $app::VERSION)
->with('helpers', $this->helpers) ->render();
->with('version', $app::VERSION) }
->render();
} public function generateJsonHelper()
{
public function generateJsonHelper() $classes = array();
{ foreach ($this->getNamespaces() as $aliases) {
$classes = array(); foreach ($aliases as $alias) {
foreach ($this->getNamespaces() as $aliases) { $functions = array();
foreach($aliases as $alias) { foreach ($alias->getMethods() as $method) {
$functions = array(); $functions[$method->getName()] = '(' . $method->getParamsWithDefault() . ')';
foreach ($alias->getMethods() as $method) { }
$functions[$method->getName()] = '('. $method->getParamsWithDefault().')'; $classes[$alias->getAlias()] = array(
} 'functions' => $functions,
$classes[$alias->getAlias()] = array( );
'functions' => $functions, }
); }
}
} $flags = JSON_FORCE_OBJECT;
if (defined('JSON_PRETTY_PRINT')) {
$flags = JSON_FORCE_OBJECT; $flags |= JSON_PRETTY_PRINT;
if (defined('JSON_PRETTY_PRINT')) { }
$flags |= JSON_PRETTY_PRINT;
} return json_encode(array(
'php' => array(
return json_encode(array( 'classes' => $classes,
'php' => array( ),
'classes' => $classes, ), $flags);
), }
), $flags);
} 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)) {
$class = get_class(\Auth::driver()); /** @noinspection PhpUndefinedClassInspection */
$this->extra['Auth'] = array($class); $class = get_class(\Auth::driver());
$this->interfaces['\Illuminate\Auth\UserProviderInterface'] = $class; $this->extra['Auth'] = array($class);
} $this->interfaces['\Illuminate\Auth\UserProviderInterface'] = $class;
}catch (\Exception $e) {} }
} catch (\Exception $e) {}
try{
if (class_exists('DB') && is_a('DB', '\Illuminate\Support\Facades\DB', true)) { try {
$class = get_class(\DB::connection()); if (class_exists('DB') && is_a('DB', '\Illuminate\Support\Facades\DB', true)) {
$this->extra['DB'] = array($class); /** @noinspection PhpUndefinedClassInspection */
$this->interfaces['\Illuminate\Database\ConnectionInterface'] = $class; $class = get_class(\DB::connection());
} $this->extra['DB'] = array($class);
}catch (\Exception $e) {} $this->interfaces['\Illuminate\Database\ConnectionInterface'] = $class;
}
try{ } catch (\Exception $e) {}
if (class_exists('Cache') && is_a('Cache', '\Illuminate\Support\Facades\Cache', true)) {
$driver = get_class(\Cache::driver()); try {
$store = get_class(\Cache::getStore()); if (class_exists('Cache') && is_a('Cache', '\Illuminate\Support\Facades\Cache', true)) {
$this->extra['Cache'] = array($driver, $store); /** @noinspection PhpUndefinedClassInspection */
$this->interfaces['\Illuminate\Cache\StoreInterface'] = $store; $driver = get_class(\Cache::driver());
} /** @noinspection PhpUndefinedClassInspection */
}catch (\Exception $e) {} $store = get_class(\Cache::getStore());
$this->extra['Cache'] = array($driver, $store);
try{ $this->interfaces['\Illuminate\Cache\StoreInterface'] = $store;
if (class_exists('Queue') && is_a('Queue', '\Illuminate\Support\Facades\Queue', true)) { }
$class = get_class(\Queue::connection()); } catch (\Exception $e) {}
$this->extra['Queue'] = array($class);
$this->interfaces['\Illuminate\Queue\QueueInterface'] = $class; try {
} if (class_exists('Queue') && is_a('Queue', '\Illuminate\Support\Facades\Queue', true)) {
}catch (\Exception $e) {} /** @noinspection PhpUndefinedClassInspection */
$class = get_class(\Queue::connection());
try{ $this->extra['Queue'] = array($class);
if (class_exists('SSH') && is_a('SSH', '\Illuminate\Support\Facades\SSH', true)){ $this->interfaces['\Illuminate\Queue\QueueInterface'] = $class;
$class = get_class(\SSH::connection()); }
$this->extra['SSH'] = array($class); } catch (\Exception $e) {}
$this->interfaces['\Illuminate\Remote\ConnectionInterface'] = $class;
} try {
}catch (\Exception $e) {} if (class_exists('SSH') && is_a('SSH', '\Illuminate\Support\Facades\SSH', true)) {
/** @noinspection PhpUndefinedClassInspection */
} $class = get_class(\SSH::connection());
$this->extra['SSH'] = array($class);
/** $this->interfaces['\Illuminate\Remote\ConnectionInterface'] = $class;
* Find all namespaces/aliases that are valid for us to render }
* } catch (\Exception $e) {}
* @return array
*/ }
protected function getNamespaces()
{ /**
$namespaces = array(); * Find all namespaces/aliases that are valid for us to render
*
// Get all aliases * @return array|Alias[][]
foreach (AliasLoader::getInstance()->getAliases() as $name => $facade) { */
$magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : array(); protected function getNamespaces()
$alias = new Alias($name, $facade, $magicMethods, $this->interfaces); {
if ($alias->isValid()) { $namespaces = array();
//Add extra methods, from other classes (magic static calls) // Get all aliases
if (array_key_exists($name, $this->extra)) { /** @noinspection PhpUndefinedClassInspection */
$alias->addClass($this->extra[$name]); foreach (AliasLoader::getInstance()->getAliases() as $name => $facade) {
} $magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : array();
$alias = new Alias($name, $facade, $magicMethods, $this->interfaces);
$namespace = $alias->getNamespace(); if ($alias->isValid()) {
if (!isset($namespaces[$namespace])) {
$namespaces[$namespace] = array(); //Add extra methods, from other classes (magic static calls)
} if (array_key_exists($name, $this->extra)) {
$namespaces[$namespace][] = $alias; $alias->addClass($this->extra[$name]);
} }
} $namespace = $alias->getNamespace();
if (!isset($namespaces[$namespace])) {
return $namespaces; $namespaces[$namespace] = array();
} }
$namespaces[$namespace][] = $alias;
/** }
* Get the driver/connection/store from the managers
* }
* @param $alias
* @return array|bool|string return $namespaces;
*/ }
public function getDriver($alias)
{ /**
try { * Get the driver/connection/store from the managers
if ($alias == "Auth") { *
$driver = \Auth::driver(); * @param $alias
} elseif ($alias == "DB") { * @return array|bool|string
$driver = \DB::connection(); */
} elseif ($alias == "Cache") { public function getDriver($alias)
$driver = get_class(\Cache::driver()); {
$store = get_class(\Cache::getStore()); try {
return array($driver, $store); if ($alias == 'Auth') {
} elseif ($alias == "Queue") { /** @noinspection PhpUndefinedClassInspection */
$driver = \Queue::connection(); $driver = \Auth::driver();
} else { } elseif ($alias == 'DB') {
return false; /** @noinspection PhpUndefinedClassInspection */
} $driver = \DB::connection();
} elseif ($alias == 'Cache') {
return get_class($driver); /** @noinspection PhpUndefinedClassInspection */
} catch (\Exception $e) { $driver = get_class(\Cache::driver());
$this->error("Could not determine driver/connection for $alias."); /** @noinspection PhpUndefinedClassInspection */
return false; $store = get_class(\Cache::getStore());
} return array($driver, $store);
} } elseif ($alias == 'Queue') {
/** @noinspection PhpUndefinedClassInspection */
/** $driver = \Queue::connection();
* Write a string as error output. } else {
* return false;
* @param string $string }
* @return void
*/ return get_class($driver);
protected function error($string) } catch (\Exception $e) {
{ $this->error("Could not determine driver/connection for $alias.");
if($this->output){ return false;
$this->output->writeln("<error>$string</error>"); }
}else{ }
echo $string . "\r\n";
} /**
} * Write a string as error output.
} *
* @param string $string
* @return void
*/
protected function error($string)
{
if ($this->output) {
$this->output->writeln("<error>$string</error>");
} else {
echo $string . "\r\n";
}
}
}