From 671f6711957e3e230a6696724a61c32ec77c77f3 Mon Sep 17 00:00:00 2001 From: IN Date: Sun, 22 Mar 2015 18:09:08 +0000 Subject: [PATCH] Intuitive app namespace determination Removed unused import --- .../Console/AppNamespaceDetectorTrait.php | 12 +++---- src/Illuminate/Console/GeneratorCommand.php | 8 ++--- src/Illuminate/Foundation/Application.php | 35 +++++++++++++++++++ .../Foundation/Console/AppNameCommand.php | 5 +-- .../Console/HandlerEventCommand.php | 4 +-- .../Console/ListenerMakeCommand.php | 4 +-- 6 files changed, 47 insertions(+), 21 deletions(-) diff --git a/src/Illuminate/Console/AppNamespaceDetectorTrait.php b/src/Illuminate/Console/AppNamespaceDetectorTrait.php index fbd3f3e2ea67..c9a680c59081 100644 --- a/src/Illuminate/Console/AppNamespaceDetectorTrait.php +++ b/src/Illuminate/Console/AppNamespaceDetectorTrait.php @@ -1,21 +1,17 @@ runningInConsole() - ? 'Illuminate\Contracts\Console\Kernel' - : 'Illuminate\Contracts\Http\Kernel'; - - return strtok(get_class(app($kernelContract)), '\\').'\\'; + return Container::getInstance()->getNamespace(); } } diff --git a/src/Illuminate/Console/GeneratorCommand.php b/src/Illuminate/Console/GeneratorCommand.php index f2795b84de97..d27b2c6b2c7a 100644 --- a/src/Illuminate/Console/GeneratorCommand.php +++ b/src/Illuminate/Console/GeneratorCommand.php @@ -5,8 +5,6 @@ abstract class GeneratorCommand extends Command { - use AppNamespaceDetectorTrait; - /** * The filesystem instance. * @@ -70,7 +68,7 @@ public function fire() */ protected function getPath($name) { - $name = str_replace($this->getAppNamespace(), '', $name); + $name = str_replace($this->laravel->getNamespace(), '', $name); return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php'; } @@ -83,7 +81,7 @@ protected function getPath($name) */ protected function parseName($name) { - $rootNamespace = $this->getAppNamespace(); + $rootNamespace = $this->laravel->getNamespace(); if (starts_with($name, $rootNamespace)) { @@ -150,7 +148,7 @@ protected function replaceNamespace(&$stub, $name) ); $stub = str_replace( - 'DummyRootNamespace', $this->getAppNamespace(), $stub + 'DummyRootNamespace', $this->laravel->getNamespace(), $stub ); return $this; diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index 0d65c5ac3c78..ecb1273bf29a 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -99,6 +99,13 @@ class Application extends Container implements ApplicationContract, HttpKernelIn */ protected $environmentFile = '.env'; + /** + * The application namespace. + * + * @var string + */ + protected $namespace = null; + /** * Create a new Illuminate application instance. * @@ -937,4 +944,32 @@ public function flush() $this->loadedProviders = []; } + /** + * Get the used kernel object. + * + * @return \Illuminate\Contracts\Console\Kernel|\Illuminate\Contracts\Http\Kernel + */ + protected function getKernel() + { + $kernelContract = $this->runningInConsole() + ? 'Illuminate\Contracts\Console\Kernel' + : 'Illuminate\Contracts\Http\Kernel'; + + return $this->make($kernelContract); + } + + /** + * Get the application namespace. + * + * @return string + */ + public function getNamespace() + { + if ( ! is_null($this->namespace)) return $this->namespace; + + $this->namespace = strtok(get_class($this->getKernel()), '\\').'\\'; + + return $this->namespace; + } + } diff --git a/src/Illuminate/Foundation/Console/AppNameCommand.php b/src/Illuminate/Foundation/Console/AppNameCommand.php index 1ea55c331b18..287d659aa20a 100644 --- a/src/Illuminate/Foundation/Console/AppNameCommand.php +++ b/src/Illuminate/Foundation/Console/AppNameCommand.php @@ -4,13 +4,10 @@ use Illuminate\Foundation\Composer; use Symfony\Component\Finder\Finder; use Illuminate\Filesystem\Filesystem; -use Illuminate\Console\AppNamespaceDetectorTrait; use Symfony\Component\Console\Input\InputArgument; class AppNameCommand extends Command { - use AppNamespaceDetectorTrait; - /** * The console command name. * @@ -68,7 +65,7 @@ public function __construct(Composer $composer, Filesystem $files) */ public function fire() { - $this->currentRoot = trim($this->getAppNamespace(), '\\'); + $this->currentRoot = trim($this->laravel->getNamespace(), '\\'); $this->setBootstrapNamespaces(); diff --git a/src/Illuminate/Foundation/Console/HandlerEventCommand.php b/src/Illuminate/Foundation/Console/HandlerEventCommand.php index b458c88a13af..03bde4463b26 100644 --- a/src/Illuminate/Foundation/Console/HandlerEventCommand.php +++ b/src/Illuminate/Foundation/Console/HandlerEventCommand.php @@ -38,9 +38,9 @@ protected function buildClass($name) $event = $this->option('event'); - if ( ! starts_with($event, $this->getAppNamespace())) + if ( ! starts_with($event, $this->laravel->getNamespace())) { - $event = $this->getAppNamespace().'Events\\'.$event; + $event = $this->laravel->getNamespace().'Events\\'.$event; } $stub = str_replace( diff --git a/src/Illuminate/Foundation/Console/ListenerMakeCommand.php b/src/Illuminate/Foundation/Console/ListenerMakeCommand.php index d2648889b75d..b1617aff6ab4 100644 --- a/src/Illuminate/Foundation/Console/ListenerMakeCommand.php +++ b/src/Illuminate/Foundation/Console/ListenerMakeCommand.php @@ -38,9 +38,9 @@ protected function buildClass($name) $event = $this->option('event'); - if ( ! starts_with($event, $this->getAppNamespace())) + if ( ! starts_with($event, $this->laravel->getNamespace())) { - $event = $this->getAppNamespace().'Events\\'.$event; + $event = $this->laravel->getNamespace().'Events\\'.$event; } $stub = str_replace(