You are browsing the unmaintained documentation for old Nette 2.0. See documentation for current Nette.

System container

Basic system container example.

<?php

/**
 * @property Nette\Application\Application $application
 * @property Authenticator $authenticator
 * @property Nette\Caching\Storages\FileStorage $cacheStorage
 * @property Nette\DI\NestedAccessor $constants
 * @property Nette\DI\Container $container
 * @property Nette\Http\Request $httpRequest
 * @property Nette\Http\Response $httpResponse
 * @property SystemContainer_nette $nette
 * @property Nette\DI\NestedAccessor $php
 * @property Nette\Application\IRouter $router
 * @property RouterFactory $routerFactory
 * @property Nette\Http\Session $session
 * @property Nette\Http\User $user
 */
class SystemContainer extends Nette\DI\Container
{

    public function __construct()
    {
        parent::__construct(array(
            'appDir' => '...',
            'wwwDir' => '...',
            'debugMode' => TRUE,
            'productionMode' => FALSE,
            'environment' => 'development',
            'consoleMode' => FALSE,
            'tempDir' => '...',
        ));
    }


    /**
     * @return Nette\Application\Application
     */
    protected function createServiceApplication()
    {
        $service = new Nette\Application\Application($this->getService('nette.presenterFactory'), $this->getService('router'), $this->getService('httpRequest'), $this->getService('httpResponse'));
        $service->catchExceptions = FALSE;
        $service->errorPresenter = 'Error';
        Nette\Application\Diagnostics\RoutingPanel::initializePanel($service);
        Nette\Diagnostics\Debugger::$bar->addPanel(new Nette\Application\Diagnostics\RoutingPanel($this->getService('router'), $this->getService('httpRequest')));
        return $service;
    }


    /**
     * @return Authenticator
     */
    protected function createServiceAuthenticator()
    {
        $service = new Authenticator($this->getService('nette.database.default'));
        return $service;
    }


    /**
     * @return Nette\Caching\Storages\FileStorage
     */
    protected function createServiceCacheStorage()
    {
        $service = new Nette\Caching\Storages\FileStorage('...', $this->getService('nette.cacheJournal'));
        return $service;
    }


    /**
     * @return Nette\DI\NestedAccessor
     */
    protected function createServiceConstants()
    {
        $service = new Nette\DI\NestedAccessor($this, 'constants');
        return $service;
    }


    /**
     * @return Nette\DI\Container
     */
    protected function createServiceContainer()
    {
        return $this;
    }


    /**
     * @return Nette\Http\Request
     */
    protected function createServiceHttpRequest()
    {
        $service = $this->getService('nette.httpRequestFactory')->createHttpRequest();
        if (!$service instanceof Nette\Http\Request) {
            throw new Nette\UnexpectedValueException('Unable to create service \'httpRequest\', value returned by factory is not Nette\\Http\\Request type.');
        }
        return $service;
    }


    /**
     * @return Nette\Http\Response
     */
    protected function createServiceHttpResponse()
    {
        $service = new Nette\Http\Response;
        return $service;
    }


    /**
     * @return Nette\DI\NestedAccessor
     */
    protected function createServiceNette()
    {
        $service = new Nette\DI\NestedAccessor($this, 'nette');
        return $service;
    }


    /**
     * @return Nette\Forms\Form
     */
    public function createNette__basicForm()
    {
        $service = new Nette\Forms\Form;
        return $service;
    }


    /**
     * @return Nette\Callback
     */
    protected function createServiceNette__basicFormFactory()
    {
        $service = new Nette\Callback($this, 'createNette__basicForm');
        return $service;
    }


    /**
     * @return Nette\Caching\Cache
     */
    public function createNette__cache($namespace = NULL)
    {
        $service = new Nette\Caching\Cache($this->getService('cacheStorage'), $namespace);
        return $service;
    }


    /**
     * @return Nette\Callback
     */
    protected function createServiceNette__cacheFactory()
    {
        $service = new Nette\Callback($this, 'createNette__cache');
        return $service;
    }


    /**
     * @return Nette\Caching\Storages\FileJournal
     */
    protected function createServiceNette__cacheJournal()
    {
        $service = new Nette\Caching\Storages\FileJournal('...');
        return $service;
    }


    /**
     * @return Nette\DI\NestedAccessor
     */
    protected function createServiceNette__database()
    {
        $service = new Nette\DI\NestedAccessor($this, 'nette.database');
        return $service;
    }


    /**
     * @return Nette\Database\Connection
     */
    protected function createServiceNette__database__default()
    {
        $service = new Nette\Database\Connection('mysql:host=localhost;dbname=test', NULL, NULL, NULL);
        $service->setCacheStorage($this->getService('cacheStorage'));
        Nette\Diagnostics\Debugger::$blueScreen->addPanel('Nette\\Database\\Diagnostics\\ConnectionPanel::renderException');
        $service->setDatabaseReflection(new Nette\Database\Reflection\DiscoveredReflection($this->getService('cacheStorage')));
        $service->onQuery[] = array(
            $this->getService('nette.database.defaultConnectionPanel'),
            'logQuery',
        );
        return $service;
    }


    /**
     * @return Nette\Database\Diagnostics\ConnectionPanel
     */
    protected function createServiceNette__database__defaultConnectionPanel()
    {
        $service = new Nette\Database\Diagnostics\ConnectionPanel;
        $service->explain = TRUE;
        $service->name = 'default';
        Nette\Diagnostics\Debugger::$bar->addPanel($service);
        return $service;
    }


    /**
     * @return Nette\Http\Context
     */
    protected function createServiceNette__httpContext()
    {
        $service = new Nette\Http\Context($this->getService('httpRequest'), $this->getService('httpResponse'));
        return $service;
    }


    /**
     * @return Nette\Http\RequestFactory
     */
    protected function createServiceNette__httpRequestFactory()
    {
        $service = new Nette\Http\RequestFactory;
        $service->setEncoding('UTF-8');
        return $service;
    }


    /**
     * @return Nette\Latte\Engine
     */
    public function createNette__latte()
    {
        $service = new Nette\Latte\Engine;
        return $service;
    }


    /**
     * @return Nette\Callback
     */
    protected function createServiceNette__latteFactory()
    {
        $service = new Nette\Callback($this, 'createNette__latte');
        return $service;
    }


    /**
     * @return Nette\Mail\Message
     */
    public function createNette__mail()
    {
        $service = new Nette\Mail\Message;
        $service->setMailer($this->getService('nette.mailer'));
        return $service;
    }


    /**
     * @return Nette\Callback
     */
    protected function createServiceNette__mailFactory()
    {
        $service = new Nette\Callback($this, 'createNette__mail');
        return $service;
    }


    /**
     * @return Nette\Mail\SendmailMailer
     */
    protected function createServiceNette__mailer()
    {
        $service = new Nette\Mail\SendmailMailer;
        return $service;
    }


    /**
     * @return Nette\Application\PresenterFactory
     */
    protected function createServiceNette__presenterFactory()
    {
        $service = new Nette\Application\PresenterFactory('...', $this);
        return $service;
    }


    /**
     * @return Nette\Templating\FileTemplate
     */
    public function createNette__template()
    {
        $service = new Nette\Templating\FileTemplate;
        $service->registerFilter($this->createNette__latte());
        $service->registerHelperLoader('Nette\\Templating\\Helpers::loader');
        return $service;
    }


    /**
     * @return Nette\Caching\Storages\PhpFileStorage
     */
    protected function createServiceNette__templateCacheStorage()
    {
        $service = new Nette\Caching\Storages\PhpFileStorage('...', $this->getService('nette.cacheJournal'));
        return $service;
    }


    /**
     * @return Nette\Callback
     */
    protected function createServiceNette__templateFactory()
    {
        $service = new Nette\Callback($this, 'createNette__template');
        return $service;
    }


    /**
     * @return Nette\Http\UserStorage
     */
    protected function createServiceNette__userStorage()
    {
        $service = new Nette\Http\UserStorage($this->getService('session'));
        return $service;
    }


    /**
     * @return Nette\DI\NestedAccessor
     */
    protected function createServicePhp()
    {
        $service = new Nette\DI\NestedAccessor($this, 'php');
        return $service;
    }


    /**
     * @return Nette\Application\IRouter
     */
    protected function createServiceRouter()
    {
        $service = $this->getService('routerFactory')->createRouter();
        if (!$service instanceof Nette\Application\IRouter) {
            throw new Nette\UnexpectedValueException('Unable to create service \'router\', value returned by factory is not Nette\\Application\\IRouter type.');
        }
        return $service;
    }


    /**
     * @return RouterFactory
     */
    protected function createServiceRouterFactory()
    {
        $service = new RouterFactory;
        return $service;
    }


    /**
     * @return Nette\Http\Session
     */
    protected function createServiceSession()
    {
        $service = new Nette\Http\Session($this->getService('httpRequest'), $this->getService('httpResponse'));
        $service->setExpiration('14 days');
        return $service;
    }


    /**
     * @return Nette\Http\User
     */
    protected function createServiceUser()
    {
        $service = new Nette\Http\User($this->getService('nette.userStorage'), $this);
        Nette\Diagnostics\Debugger::$bar->addPanel(new Nette\Security\Diagnostics\UserPanel($service));
        return $service;
    }


    public function initialize()
    {
        date_default_timezone_set('Europe/Prague');
        Nette\Caching\Storages\FileStorage::$useDirectories = TRUE;

        $this->getService("session")->exists() && $this->getService("session")->start();
        header('X-Frame-Options: SAMEORIGIN');
    }

}