/** * Test settings can be edited */ public function testSettingsCanBeEdited() { $c = new Container(); $this->assertSame('1.1', $c->get('settings')['httpVersion']); $c->get('settings')['httpVersion'] = '1.2'; $this->assertSame('1.2', $c->get('settings')['httpVersion']); }
public function __construct(\Slim\Container $container) { $this->logger = $container->get('logger'); $this->view = $container->get('view'); $this->request = $container->request; $this->response = $container->response; }
/** * Create Controller\Base instance * * @param Container $container */ public function __construct(Container $container) { $this->container = $container; $this->setPageTitle(); $this->view->addData(['gcaptchaSitekey' => null, 'gcaptchaSecret' => null, 'gcaptchaEnable' => false], 'sections::captcha'); $this->view->addData(['session' => $container->get('session')->all()]); }
public function __construct(Container $c) { $this->view = $c->get('view'); $this->logger = $c->get('logger'); $this->flash = $c->get('flash'); $this->em = $c->get('em'); }
/** * @covers Jgut\Slim\Controller\Resolver::resolve */ public function testDefaultRegistration() { $controllers = ['Jgut\\Slim\\Controller\\Base']; $container = new Container(); foreach (Resolver::resolve($controllers) as $controller => $callback) { $container[$controller] = $callback; } $this->assertInstanceOf('Jgut\\Slim\\Controller\\Base', $container->get('Jgut\\Slim\\Controller\\Base')); }
/** * @param ServerRequestInterface $request * @param Response $response * @param callable $next * @return Response */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $authContainer = $this->container->get('auth-container'); if ($authContainer->user == null) { $response = $response->withRedirect($this->container->router->pathFor('home')); } else { $response = $next($request, $response); } return $response; }
/** * Call \Slim\Container callable name * * @param string $method Container Name * @return mixed * @throws \BadMethodCallException */ public function __call($method, $params) { if ($this->container->has($method)) { $obj = $this->container->get($method); if (is_callable($obj)) { return call_user_func_array($obj, $params); } } throw new BadMethodCallException("Method {$method} is not a valid method"); }
/** * @todo Move services to configuration value so it can be tested with different services. */ public function registerServices() { /** @var ServiceProviderInterface[] $services */ $services = [new TwigServiceProvider(), new PDOServiceProvider(), new EventDispatcherServiceProvider(), new FlashServiceProvider(), new FlashListenerServiceProvider(), new LoggerServiceProvider()]; foreach ($services as $service) { if ($service instanceof ServiceProviderInterface) { $this->container->register($service); } } }
public function __construct(Container $c) { $this->view = $c->get('view'); $this->logger = $c->get('logger'); $this->flash = $c->get('flash'); $this->validator = $c->get('validator'); $this->currentUser = $c->get('current_user'); $this->currentCompany = $c->get('current_company'); $this->userRepository = $c->get('user_repository'); $this->companyRepository = $c->get('company_repository'); $this->addressRepository = $c->get('address_repository'); }
/** * Create new container * * @param array $values The parameters or objects. */ public function __construct(array $values = []) { // Initialize container for Slim and Pimple parent::__construct($values); $this['config'] = isset($values['config']) ? $values['config'] : []; if (!isset($container['provider/factory'])) { $this['provider/factory'] = function (Container $container) { return new Factory(['base_class' => ServiceProviderInterface::class, 'resolver_options' => ['suffix' => 'ServiceProvider']]); }; } $defaults = ['charcoal/app/service-provider/app' => [], 'charcoal/app/service-provider/cache' => [], 'charcoal/app/service-provider/database' => [], 'charcoal/app/service-provider/logger' => [], 'charcoal/app/service-provider/translator' => [], 'charcoal/app/service-provider/view' => []]; if (!empty($this['config']['service_providers'])) { $providers = array_replace($defaults, $this['config']['service_providers']); } else { $providers = $defaults; } foreach ($providers as $provider => $values) { if (false === $values) { continue; } if (!is_array($values)) { $values = []; } $provider = $this['provider/factory']->get($provider); $this->register($provider, $values); } }
public function __construct(Container $container) { $this->view = $container->get('view'); $this->logger = $container->get('logger'); $this->pdo = $container->get('pdo'); $this->flash = $container->get('flash'); $this->mailer = $container->get('mailer'); $this->router = $container->get('router'); $this->userManager = $container->get('userManager'); }
public function __construct($settings = []) { parent::__construct($settings || ['settings' => ['displayErrorDetails' => true]]); $this['foundHandler'] = function () { return new \Slim\Handlers\Strategies\RequestResponseArgs(); }; $this['log'] = function ($container) { return new \App\Service\Logger(); }; }
/** * Settings constructor. * * @param array $settings */ public function __construct(array $settings = []) { $settings = array_merge($this->default_settings, $settings); $DS = DIRECTORY_SEPARATOR; $this->document_root = !empty($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] . $DS : ''; $this->root_path = !empty($this->document_root) ? dirname($this->document_root) . $DS : ''; $config_directory = $this->root_path . $DS . trim($settings['config_directory'], '/'); $this->config_directory = !empty($this->root_path) && is_dir($config_directory) ? $config_directory . $DS : ''; $this->configureMode()->parseConfig(); parent::__construct(['settings' => $this->settings]); }
/** * Renders the given template path, and returns the resultant HTTP response. * * @param string $path - The path to the template to be rendered. * @param array $data - The data to pass into the template. * * @return Response */ public function rendered($path, array $data) { // Format path. $path = $this->container->get('settings')['templatePath'] . DIRECTORY_SEPARATOR . $path; $path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path); $path .= '.blade.php'; $response = $this->container['response']; /* @var Response $response */ $response->getBody()->write($this->factory->file($path, $data)->render()); return $response; }
public function __construct(\Slim\Container $container) { $this->view = $container->get('view'); $this->router = $container->get('router'); $this->logger = $container->get('logger'); $this->settings = $container->get('settings'); $this->session = $container->get('session'); $this->facebook = $container->get('facebook'); $this->quiz = $container->get('quiz'); $this->viewData = array(); $this->viewData['permissions'] = json_encode($this->settings["facebook-permissions"]); $this->viewData['settings'] = $this->settings; $this->viewData['theme'] = 'starwars'; $this->viewData['version'] = 1; }
public function __construct(array $settings = []) { parent::__construct($settings); $this['log'] = function ($container) { return new \App\Service\Logger(); }; $this['cacheDriver'] = function ($container) { $options = ['ttl' => 3600, 'namespace' => md5(__FILE__)]; $driver = new Stash\Driver\Apc(); $driver->setOptions($options); return $driver; }; $this['cachePool'] = function ($container) { return new Stash\Pool($container['cacheDriver']); }; }
public function __construct(array $settings = []) { parent::__construct($settings); $this['view'] = function ($container) { return new \Slim\Views\JsonView(); }; $this['log'] = function ($container) { return new \App\Service\Logger(); }; $this['errorHandler'] = function ($container) { return new \App\Handlers\Error($container['log']); }; $this['friendship'] = function ($container) { $client = new Client(NEO4J_HOST, NEO4J_PORT); $client->getTransport()->setAuth(NEO4J_LOGIN, NEO4J_PASSWORD); return new \App\Service\Friendship($client); }; }
public function __construct() { parent::__construct(); $this['errorHandler'] = function ($container) { return function (RequestInterface $request, $response, $exception) use($container) { switch ($request->getMethod()) { case "POST": return Reponse::postError($response, $exception); break; case "GET": return Reponse::getError($response, $exception); break; default: return Reponse::error($response, $exception); break; } }; }; }
use League\Plates\Extension\Asset as PlatesAsset; use Psr\Http\Message\UploadedFileInterface; use Valitron\Validator; use Membership\Models; /** * Settings file */ $settingsFile = APP_DIR . 'settings.php'; file_exists($settingsFile) || die('Setting file not available'); session_start(); /** * Slim Container * * @var \Slim\Container $container */ $container = new Container(['settings' => require $settingsFile]); /** * Setup session * * @return \SLim\Interfaces\CollectionInterface */ $container['session'] = function ($container) { if (!isset($_SESSION['MembershipAuth'])) { $_SESSION['MembershipAuth'] = []; } return new Collection($_SESSION['MembershipAuth']); }; /** * Setup database container * * @return \Slim\PDO\Database
public function __construct(Container $c) { $this->router = $c->get('router'); $this->view = $c->get('view'); }
/** * @param Container $container */ public function __construct(Container $container) { $this->view = $container->get('view'); }
public function __construct(Container $c) { $this->renderer = $c->get('renderer'); $this->logger = $c->get('logger'); $this->db = $c->get('db'); }
/** * Test `get()` throws error if item does not exist * * @expectedException \Slim\Exception\NotFoundException */ public function testGetWithError() { $c = new Container(); $c->get('foo'); }
<?php require 'vendor/autoload.php'; use Slim\App; use Slim\Container; use BB8\Emoji\Database\Connection; use BB8\Emoji\Database\Schema; use BB8\Emoji\Middleware; use BB8\Emoji\Models\User; //Create connection to database $connection = new Connection(); //Creaet database tables if table does not exist Schema::createSchema(); //Initialize a new dependency container $container = new Container(); //Add container to handle all exceptions/errors, fail safe and return json $container['errorHandler'] = function ($container) { return function ($request, $response, $exception) use($container) { //Format of exception to return $data = ['message' => $exception->getMessage()]; return $container->get('response')->withStatus(500)->withHeader('Content-Type', 'application/json')->write(json_encode($data)); }; }; //Register authentication container Dependency $container['auth'] = function ($container) { return new BB8\Emoji\Auth($container); }; //Initialize the slim app $app = new App($container); //Add middleware at app level $app->add('BB8\\Emoji\\Middleware:init');
/** * Get \Slim\Container name * * @param string $name Container Name * @return mixed * @throws \Slim\Exception\ContainerValueNotFoundException */ public function __get($name) { return $this->container->get($name); }
public function __construct(Container $container) { $this->container = $container; $this->db = $container->get('db'); }
/** * Register Slim's default services. * * @param Container $container A DI container implementing ArrayAccess and container-interop. */ public function register($container) { if (!isset($container['environment'])) { /** * This service MUST return a shared instance * of \Slim\Interfaces\Http\EnvironmentInterface. * * @return EnvironmentInterface */ $container['environment'] = function () { return new Environment($_SERVER); }; } if (!isset($container['request'])) { /** * PSR-7 Request object * * @param Container $container * * @return ServerRequestInterface */ $container['request'] = function ($container) { return Request::createFromEnvironment($container->get('environment')); }; } if (!isset($container['response'])) { /** * PSR-7 Response object * * @param Container $container * * @return ResponseInterface */ $container['response'] = function ($container) { $headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']); $response = new Response(200, $headers); return $response->withProtocolVersion($container->get('settings')['httpVersion']); }; } if (!isset($container['router'])) { /** * This service MUST return a SHARED instance * of \Slim\Interfaces\RouterInterface. * * @return RouterInterface */ $container['router'] = function () { return new Router(); }; } if (!isset($container['foundHandler'])) { /** * This service MUST return a SHARED instance * of \Slim\Interfaces\InvocationStrategyInterface. * * @return InvocationStrategyInterface */ $container['foundHandler'] = function () { return new RequestResponse(); }; } if (!isset($container['phpErrorHandler'])) { /** * This service MUST return a callable * that accepts three arguments: * * 1. Instance of \Psr\Http\Message\ServerRequestInterface * 2. Instance of \Psr\Http\Message\ResponseInterface * 3. Instance of \Error * * The callable MUST return an instance of * \Psr\Http\Message\ResponseInterface. * * @param Container $container * * @return callable */ $container['phpErrorHandler'] = function ($container) { return new PhpError($container->get('settings')['displayErrorDetails']); }; } if (!isset($container['errorHandler'])) { /** * This service MUST return a callable * that accepts three arguments: * * 1. Instance of \Psr\Http\Message\ServerRequestInterface * 2. Instance of \Psr\Http\Message\ResponseInterface * 3. Instance of \Exception * * The callable MUST return an instance of * \Psr\Http\Message\ResponseInterface. * * @param Container $container * * @return callable */ $container['errorHandler'] = function ($container) { return new Error($container->get('settings')['displayErrorDetails']); }; } if (!isset($container['notFoundHandler'])) { /** * This service MUST return a callable * that accepts two arguments: * * 1. Instance of \Psr\Http\Message\ServerRequestInterface * 2. Instance of \Psr\Http\Message\ResponseInterface * * The callable MUST return an instance of * \Psr\Http\Message\ResponseInterface. * * @return callable */ $container['notFoundHandler'] = function () { return new NotFound(); }; } if (!isset($container['notAllowedHandler'])) { /** * This service MUST return a callable * that accepts three arguments: * * 1. Instance of \Psr\Http\Message\ServerRequestInterface * 2. Instance of \Psr\Http\Message\ResponseInterface * 3. Array of allowed HTTP methods * * The callable MUST return an instance of * \Psr\Http\Message\ResponseInterface. * * @return callable */ $container['notAllowedHandler'] = function () { return new NotAllowed(); }; } if (!isset($container['callableResolver'])) { /** * Instance of \Slim\Interfaces\CallableResolverInterface * * @param Container $container * * @return CallableResolverInterface */ $container['callableResolver'] = function ($container) { return new CallableResolver($container); }; } }
public function __construct(Container $container) { $this->db = $container->get('pdo'); }
/** * @return mixed */ public function __get($var) { return $this->container->get($var); }
public function __construct(Container $container) { $this->view = $container->get('view'); $this->logger = $container->get('logger'); $this->dm = $container->get('dm'); }