12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655 |
- <?php namespace Laravel\Lumen;
- use Closure;
- use Exception;
- use ErrorException;
- use Monolog\Logger;
- use FastRoute\Dispatcher;
- use Illuminate\Http\Request;
- use Illuminate\Http\Response;
- use Illuminate\Pipeline\Pipeline;
- use Monolog\Handler\StreamHandler;
- use Illuminate\Container\Container;
- use Illuminate\Foundation\Composer;
- use Monolog\Formatter\LineFormatter;
- use Illuminate\Filesystem\Filesystem;
- use Illuminate\Support\Facades\Facade;
- use Illuminate\Support\ServiceProvider;
- use Symfony\Component\Console\Output\ConsoleOutput;
- use Illuminate\Http\Exception\HttpResponseException;
- use Illuminate\Config\Repository as ConfigRepository;
- use Symfony\Component\HttpKernel\HttpKernelInterface;
- use Illuminate\Contracts\Routing\TerminableMiddleware;
- use Symfony\Component\HttpFoundation\BinaryFileResponse;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
- use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Illuminate\Contracts\Foundation\Application as ApplicationContract;
- use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
- class Application extends Container implements ApplicationContract, HttpKernelInterface
- {
- /**
- * Indicates if the class aliases have been registered.
- *
- * @var bool
- */
- protected static $aliasesRegistered = false;
- /**
- * The base path of the application installation.
- *
- * @var string
- */
- protected $basePath;
- /**
- * The storage path of the application installation.
- *
- * @var string
- */
- protected $storagePath;
- /**
- * The configuration path of the application installation.
- *
- * @var string
- */
- protected $configPath;
- /**
- * The resource path of the application installation.
- *
- * @var string
- */
- protected $resourcePath;
- /**
- * All of the loaded configuration files.
- *
- * @var array
- */
- protected $loadedConfigurations = [];
- /**
- * All of the routes waiting to be registered.
- *
- * @var array
- */
- protected $routes = [];
- /**
- * All of the named routes and URI pairs.
- *
- * @var array
- */
- public $namedRoutes = [];
- /**
- * The shared attributes for the current route group.
- *
- * @var array|null
- */
- protected $groupAttributes;
- /**
- * All of the global middleware for the application.
- *
- * @var array
- */
- protected $middleware = [];
- /**
- * All of the route specific middleware short-hands.
- *
- * @var array
- */
- protected $routeMiddleware = [];
- /**
- * The current route being dispatched.
- *
- * @var array
- */
- protected $currentRoute;
- /**
- * The loaded service providers.
- *
- * @var array
- */
- protected $loadedProviders = [];
- /**
- * The service binding methods that have been executed.
- *
- * @var array
- */
- protected $ranServiceBinders = [];
- /**
- * The FastRoute dispatcher.
- *
- * @var \FastRoute\Dispatcher
- */
- protected $dispatcher;
- /**
- * Create a new Lumen application instance.
- *
- * @param string|null $basePath
- * @return void
- */
- public function __construct($basePath = null)
- {
- date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));
- $this->basePath = $basePath;
- $this->bootstrapContainer();
- $this->registerErrorHandling();
- }
- /**
- * Bootstrap the application container.
- *
- * @return void
- */
- protected function bootstrapContainer()
- {
- static::setInstance($this);
- $this->instance('app', $this);
- $this->registerContainerAliases();
- }
- /**
- * Get the version number of the application.
- *
- * @return string
- */
- public function version()
- {
- return 'Lumen (5.0.10) (Laravel Components 5.0.*)';
- }
- /**
- * Get or check the current application environment.
- *
- * @param mixed
- * @return string
- */
- public function environment()
- {
- return env('APP_ENV', 'production');
- }
- /**
- * Determine if the application is currently down for maintenance.
- *
- * @return bool
- */
- public function isDownForMaintenance()
- {
- return false;
- }
- /**
- * Register all of the configured providers.
- *
- * @return void
- */
- public function registerConfiguredProviders()
- {
- //
- }
- /**
- * Register a service provider with the application.
- *
- * @param \Illuminate\Support\ServiceProvider|string $provider
- * @param array $options
- * @param bool $force
- * @return \Illuminate\Support\ServiceProvider
- */
- public function register($provider, $options = array(), $force = false)
- {
- if (!$provider instanceof ServiceProvider) {
- $provider = new $provider($this);
- }
- if (array_key_exists($providerName = get_class($provider), $this->loadedProviders)) {
- return;
- }
- $this->loadedProviders[$providerName] = true;
- $provider->register();
- $provider->boot();
- }
- /**
- * Register a deferred provider and service.
- *
- * @param string $provider
- * @param string $service
- * @return void
- */
- public function registerDeferredProvider($provider, $service = null)
- {
- return $this->register($provider);
- }
- /**
- * Boot the application's service providers.
- *
- * @return void
- */
- public function boot()
- {
- //
- }
- /**
- * Register a new boot listener.
- *
- * @param mixed $callback
- * @return void
- */
- public function booting($callback)
- {
- //
- }
- /**
- * Register a new "booted" listener.
- *
- * @param mixed $callback
- * @return void
- */
- public function booted($callback)
- {
- //
- }
- /**
- * Set the error handling for the application.
- *
- * @return void
- */
- protected function registerErrorHandling()
- {
- error_reporting(-1);
- set_error_handler(function ($level, $message, $file = '', $line = 0) {
- if (error_reporting() & $level) {
- throw new ErrorException($message, 0, $level, $file, $line);
- }
- });
- set_exception_handler(function ($e) {
- $this->handleUncaughtException($e);
- });
- }
- /**
- * Send the exception to the handler and retunr the response.
- *
- * @param Exception $e
- * @return Response
- */
- protected function sendExceptionToHandler($e)
- {
- $handler = $this->make('Illuminate\Contracts\Debug\ExceptionHandler');
- $handler->report($e);
- return $handler->render($this->make('request'), $e);
- }
- /**
- * Handle an uncaught exception instance.
- *
- * @param Exception $e
- * @return void
- */
- protected function handleUncaughtException($e)
- {
- $handler = $this->make('Illuminate\Contracts\Debug\ExceptionHandler');
- $handler->report($e);
- if ($this->runningInConsole()) {
- $handler->renderForConsole(new ConsoleOutput, $e);
- } else {
- $handler->render($this->make('request'), $e)->send();
- }
- }
- /**
- * Throw an HttpException with the given data.
- *
- * @param int $code
- * @param string $message
- * @param array $headers
- * @return void
- *
- * @throws \Symfony\Component\HttpKernel\Exception\HttpException
- */
- public function abort($code, $message = '', array $headers = array())
- {
- if ($code == 404) {
- throw new NotFoundHttpException($message);
- }
- throw new HttpException($code, $message, null, $headers);
- }
- /**
- * Resolve the given type from the container.
- *
- * @param string $abstract
- * @param array $parameters
- * @return mixed
- */
- public function make($abstract, $parameters = [])
- {
- if (array_key_exists($abstract, $this->availableBindings) &&
- ! array_key_exists($this->availableBindings[$abstract], $this->ranServiceBinders)) {
- $this->{$method = $this->availableBindings[$abstract]}();
- $this->ranServiceBinders[$method] = true;
- }
- return parent::make($abstract, $parameters);
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerAuthBindings()
- {
- $this->singleton('auth', function () {
- return $this->loadComponent('auth', 'Illuminate\Auth\AuthServiceProvider', 'auth');
- });
- $this->singleton('auth.driver', function () {
- return $this->loadComponent('auth', 'Illuminate\Auth\AuthServiceProvider', 'auth.driver');
- });
- $this->singleton('auth.password', function () {
- return $this->loadComponent('auth', 'Illuminate\Auth\Passwords\PasswordResetServiceProvider', 'auth.password');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerBusBindings()
- {
- $this->singleton('Illuminate\Contracts\Bus\Dispatcher', function () {
- $this->register('Illuminate\Bus\BusServiceProvider');
- return $this->make('Illuminate\Contracts\Bus\Dispatcher');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerCacheBindings()
- {
- $this->singleton('cache', function () {
- return $this->loadComponent('cache', 'Illuminate\Cache\CacheServiceProvider');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerConfigBindings()
- {
- $this->singleton('config', function () {
- return new ConfigRepository;
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerComposerBindings()
- {
- $this->app->singleton('composer', function ($app) {
- return new Composer($app->make('files'), $this->basePath());
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerCookieBindings()
- {
- $this->singleton('cookie', function () {
- return $this->loadComponent('session', 'Illuminate\Cookie\CookieServiceProvider', 'cookie');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerDatabaseBindings()
- {
- $this->singleton('db', function () {
- return $this->loadComponent(
- 'database', [
- 'Illuminate\Database\DatabaseServiceProvider',
- 'Illuminate\Pagination\PaginationServiceProvider'],
- 'db'
- );
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerEncrypterBindings()
- {
- $this->singleton('encrypter', function () {
- return $this->loadComponent('app', 'Illuminate\Encryption\EncryptionServiceProvider', 'encrypter');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerEventBindings()
- {
- $this->singleton('events', function () {
- $this->register('Illuminate\Events\EventServiceProvider');
- return $this->make('events');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerErrorBindings()
- {
- if (! $this->bound('Illuminate\Contracts\Debug\ExceptionHandler')) {
- $this->singleton(
- 'Illuminate\Contracts\Debug\ExceptionHandler', 'Laravel\Lumen\Exceptions\Handler'
- );
- }
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerFilesBindings()
- {
- $this->singleton('files', function () {
- return new Filesystem;
- });
- $this->singleton('filesystem', function () {
- return $this->loadComponent('filesystems', 'Illuminate\Filesystem\FilesystemServiceProvider', 'filesystem');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerHashBindings()
- {
- $this->singleton('hash', function () {
- $this->register('Illuminate\Hashing\HashServiceProvider');
- return $this->make('hash');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerLogBindings()
- {
- $this->singleton('Psr\Log\LoggerInterface', function () {
- return new Logger('lumen', [$this->getMonologHandler()]);
- });
- }
- /**
- * Get the Monolog handler for the application.
- *
- * @return \Monolog\Handler\AbstractHandler
- */
- protected function getMonologHandler()
- {
- return (new StreamHandler(getcwd().'/vendor/laravel/lumen-framework/storage/logs/lumen.log', Logger::DEBUG))
- ->setFormatter(new LineFormatter(null, null, true, true));
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerMailBindings()
- {
- $this->singleton('mailer', function () {
- $this->configure('services');
- return $this->loadComponent('mail', 'Illuminate\Mail\MailServiceProvider', 'mailer');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerQueueBindings()
- {
- $this->singleton('queue', function () {
- return $this->loadComponent('queue', 'Illuminate\Queue\QueueServiceProvider', 'queue');
- });
- $this->singleton('queue.connection', function () {
- return $this->loadComponent('queue', 'Illuminate\Queue\QueueServiceProvider', 'queue.connection');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerRedisBindings()
- {
- $this->singleton('redis', function() {
- return $this->loadComponent('database', 'Illuminate\Redis\RedisServiceProvider', 'redis');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerRequestBindings()
- {
- $this->singleton('Illuminate\Http\Request', function () {
- return Request::capture()->setUserResolver(function () {
- return $this->make('auth')->user();
- })->setRouteResolver(function () {
- return $this->currentRoute;
- });
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerSessionBindings()
- {
- $this->singleton('session', function () {
- return $this->loadComponent('session', 'Illuminate\Session\SessionServiceProvider');
- });
- $this->singleton('session.store', function () {
- return $this->loadComponent('session', 'Illuminate\Session\SessionServiceProvider', 'session.store');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerTranslationBindings()
- {
- $this->singleton('translator', function () {
- $this->configure('app');
- $this->instance('path.lang', $this->getLanguagePath());
- $this->register('Illuminate\Translation\TranslationServiceProvider');
- return $this->make('translator');
- });
- }
- /**
- * Get the path to the application's language files.
- *
- * @return string
- */
- protected function getLanguagePath()
- {
- if (is_dir($appPath = $this->resourcePath('lang'))) {
- return $appPath;
- } else {
- return __DIR__.'/../lang';
- }
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerUrlGeneratorBindings()
- {
- $this->singleton('url', function () {
- return new Routing\UrlGenerator($this);
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerValidatorBindings()
- {
- $this->singleton('validator', function () {
- $this->register('Illuminate\Validation\ValidationServiceProvider');
- return $this->make('validator');
- });
- }
- /**
- * Register container bindings for the application.
- *
- * @return void
- */
- protected function registerViewBindings()
- {
- $this->singleton('view', function () {
- return $this->loadComponent('view', 'Illuminate\View\ViewServiceProvider');
- });
- }
- /**
- * Configure and load the given component and provider.
- *
- * @param string $config
- * @param array|string $providers
- * @param string|null $return
- * @return mixed
- */
- protected function loadComponent($config, $providers, $return = null)
- {
- $this->configure($config);
- foreach ((array) $providers as $provider) {
- $this->register($provider);
- }
- return $this->make($return ?: $config);
- }
- /**
- * Load a configuration file into the application.
- *
- * @return void
- */
- public function configure($name)
- {
- if (isset($this->loadedConfigurations[$name])) {
- return;
- }
- $this->loadedConfigurations[$name] = true;
- $path = $this->getConfigurationPath($name);
- if ($path) {
- $this->make('config')->set($name, require $path);
- }
- }
- /**
- * Get the path to the given configuration file.
- *
- * @param string $name
- * @return string
- */
- protected function getConfigurationPath($name)
- {
- $appConfigPath = ($this->configPath ?: $this->basePath('config')).'/'.$name.'.php';
- if (file_exists($appConfigPath)) {
- return $appConfigPath;
- } elseif (file_exists($path = __DIR__.'/../config/'.$name.'.php')) {
- return $path;
- }
- }
- /**
- * Register the facades for the application.
- *
- * @return void
- */
- public function withFacades()
- {
- Facade::setFacadeApplication($this);
- if (! static::$aliasesRegistered) {
- static::$aliasesRegistered = true;
- class_alias('Illuminate\Support\Facades\App', 'App');
- class_alias('Illuminate\Support\Facades\Auth', 'Auth');
- class_alias('Illuminate\Support\Facades\Bus', 'Bus');
- class_alias('Illuminate\Support\Facades\DB', 'DB');
- class_alias('Illuminate\Support\Facades\Cache', 'Cache');
- class_alias('Illuminate\Support\Facades\Cookie', 'Cookie');
- class_alias('Illuminate\Support\Facades\Crypt', 'Crypt');
- class_alias('Illuminate\Support\Facades\Event', 'Event');
- class_alias('Illuminate\Support\Facades\Hash', 'Hash');
- class_alias('Illuminate\Support\Facades\Log', 'Log');
- class_alias('Illuminate\Support\Facades\Mail', 'Mail');
- class_alias('Illuminate\Support\Facades\Queue', 'Queue');
- class_alias('Illuminate\Support\Facades\Request', 'Request');
- class_alias('Illuminate\Support\Facades\Schema', 'Schema');
- class_alias('Illuminate\Support\Facades\Session', 'Session');
- class_alias('Illuminate\Support\Facades\Storage', 'Storage');
- class_alias('Illuminate\Support\Facades\Validator', 'Validator');
- }
- }
- /**
- * Load the Eloquent library for the application.
- *
- * @return void
- */
- public function withEloquent()
- {
- $this->make('db');
- }
- /**
- * Register a set of routes with a set of shared attributes.
- *
- * @param array $attributes
- * @param Closure $callback
- * @return void
- */
- public function group(array $attributes, Closure $callback)
- {
- $this->groupAttributes = $attributes;
- call_user_func($callback, $this);
- $this->groupAttributes = null;
- }
- /**
- * Register a route with the application.
- *
- * @param string $uri
- * @param mixed $action
- * @return $this
- */
- public function get($uri, $action)
- {
- $this->addRoute('GET', $uri, $action);
- return $this;
- }
- /**
- * Register a route with the application.
- *
- * @param string $uri
- * @param mixed $action
- * @return $this
- */
- public function post($uri, $action)
- {
- $this->addRoute('POST', $uri, $action);
- return $this;
- }
- /**
- * Register a route with the application.
- *
- * @param string $uri
- * @param mixed $action
- * @return $this
- */
- public function put($uri, $action)
- {
- $this->addRoute('PUT', $uri, $action);
- return $this;
- }
- /**
- * Register a route with the application.
- *
- * @param string $uri
- * @param mixed $action
- * @return $this
- */
- public function patch($uri, $action)
- {
- $this->addRoute('PATCH', $uri, $action);
- return $this;
- }
- /**
- * Register a route with the application.
- *
- * @param string $uri
- * @param mixed $action
- * @return $this
- */
- public function delete($uri, $action)
- {
- $this->addRoute('DELETE', $uri, $action);
- return $this;
- }
- /**
- * Register a route with the application.
- *
- * @param string $uri
- * @param mixed $action
- * @return $this
- */
- public function options($uri, $action)
- {
- $this->addRoute('OPTIONS', $uri, $action);
- return $this;
- }
- /**
- * Add a route to the collection.
- *
- * @param string $method
- * @param string $uri
- * @param mixed $action
- */
- protected function addRoute($method, $uri, $action)
- {
- $action = $this->parseAction($action);
- $uri = $uri === '/' ? $uri : '/'.trim($uri, '/');
- if (isset($this->groupAttributes)) {
- if (isset($this->groupAttributes['prefix'])) {
- $uri = rtrim('/'.trim($this->groupAttributes['prefix'], '/').$uri, '/');
- }
- $action = $this->mergeGroupAttributes($action);
- }
- if (isset($action['as'])) {
- $this->namedRoutes[$action['as']] = $uri;
- }
- $this->routes[$method.$uri] = ['method' => $method, 'uri' => $uri, 'action' => $action];
- }
- /**
- * Parse the action into an array format.
- *
- * @param mixed $action
- * @return array
- */
- protected function parseAction($action)
- {
- if (is_string($action)) {
- return ['uses' => $action];
- } elseif (! is_array($action)) {
- return [$action];
- }
- return $action;
- }
- /**
- * Merge the group attributes into the action.
- *
- * @param array $action
- * @return array
- */
- protected function mergeGroupAttributes(array $action)
- {
- return $this->mergeNamespaceGroup(
- $this->mergeMiddlewareGroup($action)
- );
- }
- /**
- * Merge the namespace group into the action.
- *
- * @param array $action
- * @return array
- */
- protected function mergeNamespaceGroup(array $action)
- {
- if (isset($this->groupAttributes['namespace']) && isset($action['uses'])) {
- $action['uses'] = $this->groupAttributes['namespace'].'\\'.$action['uses'];
- }
- return $action;
- }
- /**
- * Merge the middleware group into the action.
- *
- * @param array $action
- * @return array
- */
- protected function mergeMiddlewareGroup($action)
- {
- if (isset($this->groupAttributes['middleware'])) {
- if (isset($action['middleware'])) {
- $action['middleware'] .= '|'.$this->groupAttributes['middleware'];
- } else {
- $action['middleware'] = $this->groupAttributes['middleware'];
- }
- }
- return $action;
- }
- /**
- * Add new middleware to the application.
- *
- * @param array $middleware
- * @return $this
- */
- public function middleware(array $middleware)
- {
- $this->middleware = array_unique(array_merge($this->middleware, $middleware));
- return $this;
- }
- /**
- * Define the route middleware for the application.
- *
- * @param array $middleware
- * @return $this
- */
- public function routeMiddleware(array $middleware)
- {
- $this->routeMiddleware = array_merge($this->routeMiddleware, $middleware);
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function handle(SymfonyRequest $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
- {
- return $this->dispatch($request);
- }
- /**
- * Run the application and send the response.
- *
- * @param SymfonyRequest|null $request
- * @return void
- */
- public function run($request = null)
- {
- $response = $this->dispatch($request);
- if ($response instanceof SymfonyResponse) {
- $response->send();
- } else {
- echo (string) $response;
- }
- if (count($this->middleware) > 0) {
- $this->callTerminableMiddleware($response);
- }
- }
- /**
- * Call the terminable middleware.
- *
- * @param mixed $response
- * @return void
- */
- protected function callTerminableMiddleware($response)
- {
- $response = $this->prepareResponse($response);
- foreach ($this->middleware as $middleware) {
- $instance = $this->make($middleware);
- if ($instance instanceof TerminableMiddleware) {
- $instance->terminate(
- $this->make('request'), $response
- );
- }
- }
- }
- /**
- * Dispatch the incoming request.
- *
- * @param SymfonyRequest|null $request
- * @return Response
- */
- public function dispatch($request = null)
- {
- if ($request) {
- $this->instance('Illuminate\Http\Request', $request);
- $this->ranServiceBinders['registerRequestBindings'] = true;
- $method = $request->getMethod();
- $pathInfo = $request->getPathInfo();
- } else {
- $method = $this->getMethod();
- $pathInfo = $this->getPathInfo();
- }
- try {
- return $this->sendThroughPipeline($this->middleware, function () use ($method, $pathInfo) {
- if (isset($this->routes[$method.$pathInfo])) {
- return $this->handleFoundRoute([true, $this->routes[$method.$pathInfo]['action'], []]);
- }
- return $this->handleDispatcherResponse(
- $this->createDispatcher()->dispatch($method, $pathInfo)
- );
- });
- } catch (Exception $e) {
- return $this->sendExceptionToHandler($e);
- }
- }
- /**
- * Create a FastRoute dispatcher instance for the application.
- *
- * @return Dispatcher
- */
- protected function createDispatcher()
- {
- return $this->dispatcher ?: \FastRoute\simpleDispatcher(function ($r) {
- foreach ($this->routes as $route) {
- $r->addRoute($route['method'], $route['uri'], $route['action']);
- }
- });
- }
- /**
- * Set the FastRoute dispatcher instance.
- *
- * @param \FastRoute\Dispatcher $dispatcher
- * @return void
- */
- public function setDispatcher(Dispatcher $dispatcher)
- {
- $this->dispatcher = $dispatcher;
- }
- /**
- * Handle the response from the FastRoute dispatcher.
- *
- * @param array $routeInfo
- * @return mixed
- */
- protected function handleDispatcherResponse($routeInfo)
- {
- switch ($routeInfo[0]) {
- case Dispatcher::NOT_FOUND:
- throw new NotFoundHttpException;
- case Dispatcher::METHOD_NOT_ALLOWED:
- throw new MethodNotAllowedHttpException($routeInfo[1]);
- case Dispatcher::FOUND:
- return $this->handleFoundRoute($routeInfo);
- }
- }
- /**
- * Handle a route found by the dispatcher.
- *
- * @param array $routeInfo
- * @return mixed
- */
- protected function handleFoundRoute($routeInfo)
- {
- $this->currentRoute = $routeInfo;
- $action = $routeInfo[1];
- // Pipe through route middleware...
- if (isset($action['middleware'])) {
- $middleware = $this->gatherMiddlewareClassNames($action['middleware']);
- return $this->prepareResponse($this->sendThroughPipeline($middleware, function () use ($routeInfo) {
- return $this->callActionOnArrayBasedRoute($routeInfo);
- }));
- }
- return is_string($response = $this->callActionOnArrayBasedRoute($routeInfo))
- ? $response : $this->prepareResponse($response);
- }
- /**
- * Call the Closure on the array based route.
- *
- * @param array $routeInfo
- * @return mixed
- */
- protected function callActionOnArrayBasedRoute($routeInfo)
- {
- $action = $routeInfo[1];
- if (isset($action['uses'])) {
- return $this->callControllerAction($routeInfo);
- }
- foreach ($action as $value) {
- if ($value instanceof Closure) {
- $closure = $value->bindTo(new Routing\Closure);
- break;
- }
- }
- try {
- return $this->call($closure, $routeInfo[2]);
- } catch (HttpResponseException $e) {
- return $e->getResponse();
- }
- }
- /**
- * Call a controller based route.
- *
- * @param array $routeInfo
- * @return mixed
- */
- protected function callControllerAction($routeInfo)
- {
- list($controller, $method) = explode('@', $routeInfo[1]['uses']);
- if (! method_exists($instance = $this->make($controller), $method)) {
- throw new NotFoundHttpException;
- }
- if ($instance instanceof Routing\Controller) {
- return $this->callLumenController($instance, $method, $routeInfo);
- } else {
- return $this->callControllerCallable(
- [$instance, $method], $routeInfo[2]
- );
- }
- }
- /**
- * Send the request through a Lumen controller.
- *
- * @param mixed $instance
- * @param string $method
- * @param array $routeInfo
- * @return mixed
- */
- protected function callLumenController($instance, $method, $routeInfo)
- {
- $middleware = $instance->getMiddlewareForMethod(
- $this->make('request'), $method
- );
- if (count($middleware) > 0) {
- return $this->callLumenControllerWithMiddleware(
- $instance, $method, $routeInfo, $middleware
- );
- } else {
- return $this->callControllerCallable(
- [$instance, $method], $routeInfo[2]
- );
- }
- }
- /**
- * Send the request through a set of controller middleware.
- *
- * @param mixed $instance
- * @param string $method
- * @param array $routeInfo
- * @param array $middleware
- * @return mixed
- */
- protected function callLumenControllerWithMiddleware($instance, $method, $routeInfo, $middleware)
- {
- $middleware = $this->gatherMiddlewareClassNames($middleware);
- return $this->sendThroughPipeline($middleware, function () use ($instance, $method, $routeInfo) {
- return $this->callControllerCallable(
- [$instance, $method], $routeInfo[2]
- );
- });
- }
- /**
- * Call the callable for a controller action with the given parameters.
- *
- * @param array $callable
- * @param array $parameters
- * @return mixed
- */
- protected function callControllerCallable(array $callable, array $parameters)
- {
- try {
- return $this->prepareResponse(
- $this->call($callable, $parameters)
- );
- } catch (HttpResponseException $e) {
- return $e->getResponse();
- }
- }
- /**
- * Gather the full class names for the middleware short-cut string.
- *
- * @param string $middleware
- * @return array
- */
- protected function gatherMiddlewareClassNames($middleware)
- {
- $middleware = is_string($middleware) ? explode('|', $middleware) : (array) $middleware;
- return array_map(function ($name) {
- return $this->routeMiddleware[$name];
- }, $middleware);
- }
- /**
- * Send the request through the pipeline with the given callback.
- *
- * @param array $middleware
- * @param Closure $then
- * @return mixed
- */
- protected function sendThroughPipeline(array $middleware, Closure $then)
- {
- if (count($middleware) > 0) {
- return (new Pipeline($this))
- ->send($this->make('request'))
- ->through($middleware)
- ->then($then);
- }
- return $then();
- }
- /**
- * Prepare the response for sending.
- *
- * @param mixed $response
- * @return Response
- */
- public function prepareResponse($response)
- {
- if (! $response instanceof SymfonyResponse) {
- $response = new Response($response);
- } elseif ($response instanceof BinaryFileResponse) {
- $response = $response->prepare(Request::capture());
- }
- return $response;
- }
- /**
- * Get the current HTTP request method.
- *
- * @return string
- */
- protected function getMethod()
- {
- if (isset($_POST['_method'])) {
- return strtoupper($_POST['_method']);
- } else {
- return $_SERVER['REQUEST_METHOD'];
- }
- }
- /**
- * Get the current HTTP path info.
- *
- * @return string
- */
- public function getPathInfo()
- {
- $query = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
- return '/'.trim(str_replace('?'.$query, '', $_SERVER['REQUEST_URI']), '/');
- }
- /**
- * Get the base path for the application.
- *
- * @param string $path
- * @return string
- */
- public function basePath($path = null)
- {
- if (isset($this->basePath)) {
- return $this->basePath.($path ? '/'.$path : $path);
- }
- if ($this->runningInConsole() || php_sapi_name() === 'cli-server') {
- $this->basePath = getcwd();
- } else {
- $this->basePath = realpath(getcwd().'/../');
- }
- return $this->basePath($path);
- }
- /**
- * Get the storage path for the application.
- *
- * @param string $path
- * @return string
- */
- public function storagePath($path = null)
- {
- if ($this->storagePath) {
- return $this->storagePath.($path ? '/'.$path : $path);
- }
- return $this->basePath().'/storage'.($path ? '/'.$path : $path);
- }
- /**
- * Set a custom storage path for the application.
- *
- * @param string $path
- * @return $this
- */
- public function useStoragePath($path)
- {
- $this->storagePath = $path;
- return $this;
- }
- /**
- * Get the database path for the application.
- *
- * @return string
- */
- public function databasePath()
- {
- return $this->basePath().'/database';
- }
- /**
- * Set a custom configuration path for the application.
- *
- * @param string $path
- * @return $this
- */
- public function useConfigPath($path)
- {
- $this->configPath = $path;
- return $this;
- }
- /**
- * Get the resource path for the application.
- *
- * @param string $path
- * @return string
- */
- public function resourcePath($path = null)
- {
- if ($this->resourcePath) {
- return $this->resourcePath.($path ? '/'.$path : $path);
- }
- return $this->basePath().'/resources'.($path ? '/'.$path : $path);
- }
- /**
- * Set a custom resource path for the application.
- *
- * @param string $path
- * @return $this
- */
- public function useResourcePath($path)
- {
- $this->resourcePath = $path;
- return $this;
- }
- /**
- * Determine if the application is running in the console.
- *
- * @return string
- */
- public function runningInConsole()
- {
- return php_sapi_name() == 'cli';
- }
- /**
- * Prepare the application to execute a console command.
- *
- * @return void
- */
- public function prepareForConsoleCommand()
- {
- $this->withFacades();
- $this->make('cache');
- $this->make('queue');
- $this->configure('database');
- $this->register('Illuminate\Database\MigrationServiceProvider');
- $this->register('Illuminate\Database\SeedServiceProvider');
- $this->register('Illuminate\Queue\ConsoleServiceProvider');
- }
- /**
- * Get the raw routes for the application.
- *
- * @return array
- */
- public function getRoutes()
- {
- return $this->routes;
- }
- /**
- * Set the cached routes.
- *
- * @param array $routes
- * @return void
- */
- public function setRoutes(array $routes)
- {
- $this->routes = $routes;
- }
- /**
- * Register the core container aliases.
- *
- * @return void
- */
- protected function registerContainerAliases()
- {
- $this->aliases = [
- 'Illuminate\Contracts\Foundation\Application' => 'app',
- 'Illuminate\Contracts\Auth\Guard' => 'auth.driver',
- 'Illuminate\Contracts\Auth\PasswordBroker' => 'auth.password',
- 'Illuminate\Contracts\Cache\Factory' => 'cache',
- 'Illuminate\Contracts\Cache\Repository' => 'cache.store',
- 'Illuminate\Container\Container' => 'app',
- 'Illuminate\Contracts\Container\Container' => 'app',
- 'Illuminate\Contracts\Cookie\Factory' => 'cookie',
- 'Illuminate\Contracts\Cookie\QueueingFactory' => 'cookie',
- 'Illuminate\Contracts\Encryption\Encrypter' => 'encrypter',
- 'Illuminate\Contracts\Events\Dispatcher' => 'events',
- 'Illuminate\Contracts\Filesystem\Factory' => 'filesystem',
- 'Illuminate\Contracts\Hashing\Hasher' => 'hash',
- 'log' => 'Psr\Log\LoggerInterface',
- 'Illuminate\Contracts\Mail\Mailer' => 'mailer',
- 'Illuminate\Contracts\Queue\Queue' => 'queue.connection',
- 'Illuminate\Redis\Database' => 'redis',
- 'Illuminate\Contracts\Redis\Database' => 'redis',
- 'request' => 'Illuminate\Http\Request',
- 'Illuminate\Session\SessionManager' => 'session',
- 'Illuminate\Contracts\View\Factory' => 'view',
- ];
- }
- /**
- * The available container bindings and their respective load methods.
- *
- * @var array
- */
- public $availableBindings = [
- 'auth' => 'registerAuthBindings',
- 'auth.driver' => 'registerAuthBindings',
- 'Illuminate\Contracts\Auth\Guard' => 'registerAuthBindings',
- 'auth.password' => 'registerAuthBindings',
- 'Illuminate\Contracts\Auth\PasswordBroker' => 'registerAuthBindings',
- 'Illuminate\Contracts\Bus\Dispatcher' => 'registerBusBindings',
- 'cache' => 'registerCacheBindings',
- 'Illuminate\Contracts\Cache\Factory' => 'registerCacheBindings',
- 'Illuminate\Contracts\Cache\Repository' => 'registerCacheBindings',
- 'config' => 'registerConfigBindings',
- 'composer' => 'registerComposerBindings',
- 'cookie' => 'registerCookieBindings',
- 'Illuminate\Contracts\Cookie\Factory' => 'registerCookieBindings',
- 'Illuminate\Contracts\Cookie\QueueingFactory' => 'registerCookieBindings',
- 'db' => 'registerDatabaseBindings',
- 'encrypter' => 'registerEncrypterBindings',
- 'Illuminate\Contracts\Encryption\Encrypter' => 'registerEncrypterBindings',
- 'events' => 'registerEventBindings',
- 'Illuminate\Contracts\Events\Dispatcher' => 'registerEventBindings',
- 'Illuminate\Contracts\Debug\ExceptionHandler' => 'registerErrorBindings',
- 'files' => 'registerFilesBindings',
- 'filesystem' => 'registerFilesBindings',
- 'Illuminate\Contracts\Filesystem\Factory' => 'registerFilesBindings',
- 'hash' => 'registerHashBindings',
- 'Illuminate\Contracts\Hashing\Hasher' => 'registerHashBindings',
- 'log' => 'registerLogBindings',
- 'Psr\Log\LoggerInterface' => 'registerLogBindings',
- 'mailer' => 'registerMailBindings',
- 'Illuminate\Contracts\Mail\Mailer' => 'registerMailBindings',
- 'queue' => 'registerQueueBindings',
- 'queue.connection' => 'registerQueueBindings',
- 'Illuminate\Contracts\Queue\Queue' => 'registerQueueBindings',
- 'redis' => 'registerRedisBindings',
- 'request' => 'registerRequestBindings',
- 'Illuminate\Http\Request' => 'registerRequestBindings',
- 'session' => 'registerSessionBindings',
- 'session.store' => 'registerSessionBindings',
- 'Illuminate\Session\SessionManager' => 'registerSessionBindings',
- 'translator' => 'registerTranslationBindings',
- 'url' => 'registerUrlGeneratorBindings',
- 'validator' => 'registerValidatorBindings',
- 'view' => 'registerViewBindings',
- 'Illuminate\Contracts\View\Factory' => 'registerViewBindings',
- ];
- /**
- * Get the HTML from the Lumen welcome screen.
- *
- * @return string
- */
- public function welcome()
- {
- return "<html>
- <head>
- <title>Lumen</title>
- <link href='//fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'>
- <style>
- body {
- margin: 0;
- padding: 0;
- width: 100%;
- height: 100%;
- color: #B0BEC5;
- display: table;
- font-weight: 100;
- font-family: 'Lato';
- }
- .container {
- text-align: center;
- display: table-cell;
- vertical-align: middle;
- }
- .content {
- text-align: center;
- display: inline-block;
- }
- .title {
- font-size: 96px;
- margin-bottom: 40px;
- }
- .quote {
- font-size: 24px;
- }
- </style>
- </head>
- <body>
- <div class=\"container\">
- <div class=\"content\">
- <div class=\"title\">Lumen.</div>
- </div>
- </div>
- </body>
- </html>
- ";
- }
- }
|