/** * Setup the IoC container instance. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ protected function setupContainer(Container $container) { $this->container = $container; if (!$this->container->bound('config')) { $this->container->instance('config', new Fluent()); } }
/** * Create the VersionFactory for the Version * * @return \LaraPackage\Api\Contracts\Factory\VersionFactory * @internal param $version * */ protected function createVersionFactory() { $version = $this->requestParser->version(); $factory = $this->apiVersion->factory($version); $instance = $this->app->make($factory); $this->app->instance(\LaraPackage\Api\Contracts\Factory\VersionFactory::class, $instance); return $instance; }
/** * {@inheritdoc} */ public function __invoke(Request $request, Response $response, callable $out = null) { if (($token = array_get($request->getCookieParams(), 'flarum_remember')) && ($accessToken = AccessToken::valid($token))) { $this->app->instance('flarum.actor', $user = $accessToken->user); $user->updateLastSeen()->save(); } return $out ? $out($request, $response) : $response; }
/** * {@inheritdoc} */ public function __invoke(Request $request, Response $response, callable $out = null) { $header = $request->getHeaderLine('authorization'); if (starts_with($header, $this->prefix) && ($token = substr($header, strlen($this->prefix))) && ($accessToken = AccessToken::valid($token))) { $this->app->instance('flarum.actor', $user = $accessToken->user); $user->updateLastSeen()->save(); } return $out ? $out($request, $response) : $response; }
/** * {@inheritdoc} */ public function __invoke(Request $request, Response $response, callable $out = null) { if (($token = array_get($request->getCookieParams(), 'flarum_remember')) && ($accessToken = AccessToken::valid($token)) && $accessToken->user->isAdmin()) { $this->app->instance('flarum.actor', $accessToken->user); } else { die('Access Denied'); } return $out ? $out($request, $response) : $response; }
/** * Dispatch a request. * * @param \Illuminate\Http\Request $request * @param string $version * * @return mixed */ public function dispatch(Request $request, $version) { if (!isset($this->routes[$version])) { throw new UnknownVersionException(); } $this->router->setRoutes($this->routes[$version]); // Because the above call will reset the routes defined on the applications // UrlGenerator we will simply rebind the routes to the application // container which will trigger the rebinding event. $this->container->instance('routes', $this->applicationRoutes); return $this->router->dispatch($request); }
/** * Set the application's actor instance according to the request token. * * @param Request $request * @return bool */ protected function logIn(Request $request) { if ($token = $this->getToken($request)) { if (!$token->isValid()) { // TODO: https://github.com/flarum/core/issues/253 } elseif ($token->user) { $this->app->instance('flarum.actor', $user = $token->user); $user->updateLastSeen()->save(); return true; } } return false; }
/** * Send the request through the Dingo router. * * @param \Dingo\Api\Http\Request $request * * @return \Dingo\Api\Http\Response */ protected function sendRequestThroughRouter(HttpRequest $request) { $this->app->instance('request', $request); return (new Pipeline($this->app))->send($request)->through($this->middleware)->then(function ($request) { return $this->router->dispatch($request); }); }
/** * @param App $app * @param \LaraPackage\Api\Contracts\Request\Parser $requestParser * @param \LaraPackage\Api\Contracts\Factory\VersionFactory $versionFactory */ protected function versionFactoryExpectations(App $app, \LaraPackage\Api\Contracts\Request\Parser $requestParser, \LaraPackage\Api\Contracts\Factory\VersionFactory $versionFactory, ApiVersion $apiVersion) { $requestParser->version()->shouldBeCalled()->willReturn($this->version); $apiVersion->factory($this->version)->shouldBeCalled()->willReturn(VersionFactory::class); $app->instance(\LaraPackage\Api\Contracts\Factory\VersionFactory::class, $versionFactory)->shouldBeCalled(); $app->make(VersionFactory::class)->shouldBeCalledTimes(1)->willReturn($versionFactory); }
/** * Bind the given security context to the Request and Container. * * @param string $context * @param Request $request */ public function bindContext($context, Request $request) { $security = $this->getSecurity($context); $this->container->instance(SecurityApi::class, $security); $this->container->bind(UrlGeneratorContract::class, function () use($security) { return $security->url(); }); $this->container->bind([UrlGenerator::class => 'url'], function (Container $container) use($security) { /** @var PermissionAwareUrlGeneratorExtension $url */ $url = $container->make(PermissionAwareUrlGeneratorExtension::class); $url->setUrlGenerator($security->url()); return $url; }); $request->setUserResolver(function () use($security) { return $security->getUser(); }); }
/** * {@inheritdoc} */ public function __invoke(Request $request, Response $response, callable $out = null) { $header = $request->getHeaderLine('authorization'); $parts = explode(';', $header); if (isset($parts[0]) && starts_with($parts[0], $this->prefix)) { $token = substr($parts[0], strlen($this->prefix)); if ($accessToken = AccessToken::valid($token)) { $this->app->instance('flarum.actor', $user = $accessToken->user); $user->updateLastSeen()->save(); } elseif (isset($parts[1]) && ($apiKey = ApiKey::valid($token))) { $userParts = explode('=', trim($parts[1])); if (isset($userParts[0]) && $userParts[0] === 'userId') { $this->app->instance('flarum.actor', $user = User::find($userParts[1])); } } } return $out ? $out($request, $response) : $response; }
protected function storeConfiguration() { $dbConfig = $this->dataSource->getDatabaseConfiguration(); $config = ['debug' => true, 'database' => ['driver' => $dbConfig['driver'], 'host' => $dbConfig['host'], 'database' => $dbConfig['database'], 'username' => $dbConfig['username'], 'password' => $dbConfig['password'], 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => $dbConfig['prefix'], 'strict' => false], 'url' => $this->dataSource->getBaseUrl(), 'paths' => ['api' => 'api', 'admin' => 'admin']]; $this->info('Testing config'); $this->container->instance('flarum.config', $config); $this->container->make('flarum.db'); $this->info('Writing config'); file_put_contents($this->getConfigFile(), '<?php return ' . var_export($config, true) . ';'); }
/** * Start JSON API support. * * This middleware: * - Loads the configuration for the named API that this request is being routed to. * - Registers the API in the service container. * - Triggers client/server content negotiation as per the JSON API spec. * * @param Request $request * @param Closure $next * @param $namespace * the API namespace, as per your JSON API configuration. * @return mixed */ public function handle($request, Closure $next, $namespace) { /** @var ApiFactory $factory */ $factory = $this->container->make(ApiFactoryInterface::class); /** @var ServerRequestInterface $request */ $serverRequest = $this->container->make(ServerRequestInterface::class); /** @var RequestFactoryInterface $requestFactory */ $requestFactory = $this->container->make(RequestFactoryInterface::class); /** Build and register the API */ $api = $factory->createApi($namespace, $request->getSchemeAndHttpHost()); $this->container->instance(ApiInterface::class, $api); /** Build and register the JSON API request */ $jsonApiRequest = $requestFactory->build($api, $serverRequest); $this->container->instance(RequestInterface::class, $jsonApiRequest); /** Override the current page resolution */ AbstractPaginator::currentPageResolver(function () { /** @var PaginatorInterface $paginator */ $paginator = $this->container->make(PaginatorInterface::class); return $paginator->getCurrentPage(); }); return $next($request); }
/** * Register an addon. * * @param $path * @param $enabled * @param $installed */ public function register($path, array $enabled, array $installed) { if (!is_dir($path)) { return; } $vendor = strtolower(basename(dirname($path))); $slug = strtolower(substr(basename($path), 0, strpos(basename($path), '-'))); $type = strtolower(substr(basename($path), strpos(basename($path), '-') + 1)); $class = studly_case($vendor) . '\\' . studly_case($slug) . studly_case($type) . '\\' . studly_case($slug) . studly_case($type); /* @var Addon|Module|Extension|Twig_ExtensionInterface $addon */ $addon = app($class)->setPath($path)->setType($type)->setSlug($slug)->setVendor($vendor); // If the addon supports states - set the state now. if ($addon->getType() === 'module' || $addon->getType() === 'extension') { $addon->setInstalled(in_array($addon->getNamespace(), $installed)); $addon->setEnabled(in_array($addon->getNamespace(), $enabled)); } // Bind to the service container. $this->container->alias($addon->getNamespace(), $alias = get_class($addon)); $this->container->instance($alias, $addon); // Load package configuration. $this->configurator->addNamespace($addon->getNamespace(), $addon->getPath('resources/config')); // Load system overrides. $this->configurator->addNamespaceOverrides($addon->getNamespace(), base_path('resources/addons/' . $addon->getVendor() . '/' . $addon->getSlug() . '-' . $addon->getType())); // Load application overrides. $this->configurator->addNamespaceOverrides($addon->getNamespace(), $this->application->getResourcesPath('addons/' . $addon->getVendor() . '/' . $addon->getSlug() . '-' . $addon->getType() . '/config')); // Continue loading things. $this->provider->register($addon); // Add the view / translation namespaces. $this->views->addNamespace($addon->getNamespace(), $addon->getPath('resources/views')); $this->translator->addNamespace($addon->getNamespace(), $addon->getPath('resources/lang')); /* * If the addon is a plugin then * load it into Twig when appropriate. */ if ($addon->getType() === 'plugin') { $this->events->listen('Anomaly\\Streams\\Platform\\View\\Event\\RegisteringTwigPlugins', function (RegisteringTwigPlugins $event) use($addon) { $twig = $event->getTwig(); $twig->addExtension($addon); }); } $this->collection->put($addon->getNamespace(), $addon); $this->events->fire(new AddonWasRegistered($addon)); }
/** * This method saves analysis result in Illuminate Container for * using it in other parts of the application (e.g. in exception handler). * * @param Request $request * * @return AnalysisResultInterface */ protected function getCorsAnalysis(Request $request) { $analysis = $this->analyzer->analyze($this->getRequestAdapter($request)); $this->container->instance(AnalysisResultInterface::class, $analysis); return $analysis; }
protected function registerConfig() { $config = (require CONFIG_FILE); // Register the configuration $this->container->instance('config', $config); }
/** * @param string $abstract * @param object $instance * * @return object */ private function bindAndReturn($abstract, $instance) { $this->container->instance($abstract, $instance); return $instance; }