本文整理汇总了PHP中Slim\App::getContainer方法的典型用法代码示例。如果您正苦于以下问题:PHP App::getContainer方法的具体用法?PHP App::getContainer怎么用?PHP App::getContainer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Slim\App
的用法示例。
在下文中一共展示了App::getContainer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: hasRoutesConfigured
private function hasRoutesConfigured() : bool
{
$slimContainer = $this->slimApp->getContainer();
$slimRouter = $slimContainer->get('router');
/** @var $slimRouter SlimRouter */
return (bool) count($slimRouter->getRoutes());
}
示例2: __construct
public function __construct(\Slim\App $app)
{
$this->app = $app;
$this->view = $app->getContainer()->get('view');
$this->pdo = $app->getContainer()->get('pdo');
if (!$this->pdo instanceof PDO) {
throw new \RuntimeException(sprintf('%s requires a PDO instance, app did not contain "PDO" key', __CLASS__));
}
}
示例3: runApp
/**
* @param String $method
* @param Uri $uri
* @param array $post
*/
protected function runApp($method, $uri, $post = [])
{
$this->buildApp();
$this->buildRequest($method, $uri, $post);
$this->app->getContainer()['request'] = $this->request;
$this->app->getContainer()['response'] = $this->response;
$this->response = $this->app->run(true);
$this->response->getBody()->rewind();
$this->html = $this->response->getBody()->getContents();
}
示例4: testExtendSingle
/**
* Test extend single routes.
*/
public function testExtendSingle()
{
$this->assertCount(0, $this->slim_app->getContainer()->get('router')->getRoutes());
$this->model_router->mapModel('App\\Model\\User', null, null, function (Extender $extender) {
$extender->extend('accounts');
});
$this->assertCount(3, $this->slim_app->getContainer()->get('router')->getRoutes());
/** @var Route $user_accounts_route */
$user_accounts_route = $this->slim_app->getContainer()->get('router')->getRoutes()['route2'];
$this->assertEquals(['GET'], $user_accounts_route->getMethods());
$this->assertEquals('user_accounts', $user_accounts_route->getName());
}
示例5: setRouteForPostTypes
protected function setRouteForPostTypes($postType, $defaultController)
{
$controllerPageMapping = $this->app->getContainer()->get(ControllerPageMappingField::class);
/**
* @FIXME: In future versions, need to change
* adding routes to the map of get|post. All WP PAGES and POSTS must
* coresponds to only GET method. Because it is has content only for reading.
* All other logic like writing or another logic should be implemented in WIDGETS
* or in controllers via declarring new routes and handlers for them.
*/
foreach ($this->wpService->get_posts(['numberposts' => -1, 'post_type' => $postType]) as $post) {
$controller = $controllerPageMapping->getValue($post->ID);
$this->app->map(['get', 'post'], parse_url(get_permalink($post), PHP_URL_PATH), $this->app->getContainer()->get(empty($controller) ? $defaultController : $controller))->setArgument('requestedEntity', $post);
}
}
示例6: __invoke
/**
* @param \swoole_http_request $request
* @param \swoole_http_response $response
* @throws \Exception
*/
public function __invoke($request, $response)
{
$this->app->getContainer()['environment'] = $this->app->getContainer()->factory(function () {
return new Environment($_SERVER);
});
$this->app->getContainer()['request'] = $this->app->getContainer()->factory(function ($container) {
return Request::createFromEnvironment($container['environment']);
});
$this->app->getContainer()['response'] = $this->app->getContainer()->factory(function ($container) {
$headers = new Headers(['Content-Type' => 'text/html']);
$response = new Response(200, $headers);
return $response->withProtocolVersion($container->get('settings')['httpVersion']);
});
/**
* @var ResponseInterface $appResponse
*/
$appResponse = $this->app->run(true);
// set http header
foreach ($appResponse->getHeaders() as $key => $value) {
$filter_header = function ($header) {
$filtered = str_replace('-', ' ', $header);
$filtered = ucwords($filtered);
return str_replace(' ', '-', $filtered);
};
$name = $filter_header($key);
foreach ($value as $v) {
$response->header($name, $v);
}
}
// set http status
$response->status($appResponse->getStatusCode());
// send response to browser
if (!$this->isEmptyResponse($appResponse)) {
$body = $appResponse->getBody();
if ($body->isSeekable()) {
$body->rewind();
}
$settings = $this->app->getContainer()->get('settings');
$chunkSize = $settings['responseChunkSize'];
$contentLength = $appResponse->getHeaderLine('Content-Length');
if (!$contentLength) {
$contentLength = $body->getSize();
}
$totalChunks = ceil($contentLength / $chunkSize);
$lastChunkSize = $contentLength % $chunkSize;
$currentChunk = 0;
while (!$body->eof() && $currentChunk < $totalChunks) {
if (++$currentChunk == $totalChunks && $lastChunkSize > 0) {
$chunkSize = $lastChunkSize;
}
$response->write($body->read($chunkSize));
if (connection_status() != CONNECTION_NORMAL) {
break;
}
}
$response->end();
}
}
示例7: run
/**
* @inheritdoc
*/
public function run($appNamespace = self::ROOT_NAMESPACE)
{
//
// Each application must exist inside its own namespace. Chubby uses that namespace to search for modules.
$this->appNamespace = $appNamespace;
//
// Slim can be initiated in one of two ways:
// 1. Without a container. Slim will create the default container.
// 2. Receiving a container in the constructor. We can pass Slim some settings and services
// by passing a pre-created container. We do this here via a configuration file.
$container = $this->getContainerConfig();
$this->slim = new \Slim\App($container);
$container = $this->slim->getContainer();
//
$this->modules = \Chubby\PackageLoader::loadModules($container);
if (!is_array($this->modules) || !count($this->modules)) {
throw new \Exception("Chubby Framework requires at least one module.");
}
//
// Initialize the modules following the order given by each module's priority.
foreach ($this->modules as $priority => $modules) {
foreach ($modules as $module) {
$module['object']->setApp($this);
$module['object']->init();
}
}
//
$this->slim->run();
return $this;
}
示例8: register
public static function register(App $app, $config)
{
$app->getContainer()['imagecache'] = function () use($config) {
return new Manager($config);
};
$app->get("/{$config['path_web']}/{$config['path_cache']}/{preset}/{file:.*}", (new ImagecacheRegister())->request())->setName('onigoetz.imagecache');
}
示例9: getSlimRouteFromApplication
/**
* @param SlimApp $slimApp
* @return SlimRoute
*/
private function getSlimRouteFromApplication(SlimApp $slimApp)
{
$slimRouter = $slimApp->getContainer()->get('router');
/** @var $slimRouter SlimRouter */
$slimRoutes = $slimRouter->getRoutes();
return $slimRoutes['route0'];
}
示例10: testLoginLogout
public function testLoginLogout()
{
/**
* @var Auth $auth
* @var Request $request
* @var Response $response
*/
$container = $this->app->getContainer();
$auth = $container->get("auth");
$request = $container->get('request');
$response = $container->get('response');
$middleware = $this->middleware;
$middleware($request, $response, $this->app);
/** @var Response $response */
$response = $auth->login($response, "wrong@mail.com", "wrongpw");
$this->assertInstanceOf(Response::class, $response);
$this->assertEmpty($response->getHeaders());
$this->assertNull($auth->getUser());
$this->assertFalse($auth->isAuthenticated());
/** @var Response $response */
$response = $auth->login($response, "test@test.com", "test1234");
$this->assertInstanceOf(Response::class, $response);
$this->assertArrayHasKey("Set-Cookie", $response->getHeaders());
$this->assertInstanceOf(TestUser::class, $auth->getUser());
$this->assertTrue($auth->isAuthenticated());
/** @var Response $response */
$response = $auth->logout($response);
$this->assertInstanceOf(Response::class, $response);
$this->assertArrayHasKey("Set-Cookie", $response->getHeaders());
$this->assertNull($auth->getUser());
$this->assertFalse($auth->isAuthenticated());
}
示例11: initModules
/**
* Load the module. This will run for all modules, use for routes mainly
* @param string $moduleName Module name
*/
public function initModules(App $app)
{
$container = $app->getContainer();
$this->initDependencies($container);
$this->initMiddleware($app);
$this->initRoutes($app);
}
示例12: load
public function load(array $settings = [])
{
$service = new SlimApp($settings);
$provider = new ServiceProvider();
$provider->register($service->getContainer());
$this->setService($service);
}
示例13: setUp
public function setUp()
{
$app = new App();
$kernel = new Kernel($app, $app->getContainer());
$kernel->registerServices();
$kernel->registerRoutes();
$this->app = $app;
}
示例14:
function __construct(\Slim\App $app, RequestInterface $request, ResponseInterface $response, $args = false)
{
$this->app = $app;
$this->container = $app->getContainer();
$this->request = $request;
$this->response = $response;
$this->args = $args;
}
示例15: init
public static function init(\Slim\App $app)
{
error_reporting(E_ALL);
ini_set('display_errors', true);
date_default_timezone_set('Asia/Shanghai');
$container = $app->getContainer();
$settings = $container->get('settings');
$settings['displayErrorDetails'] = true;
$settings['core.baseNamespace'] = 'Application';
$settings['view.basePath'] = __DIR__;
return parent::init($app);
}