Přihlásit | Registrovat | English version

Systémový kontejner

Příklad základního systémového kontejneru.

<?php

/**
 * @property Nette\Caching\Storages\FileJournal $cacheJournal
 * @property Nette\Caching\Storages\FileStorage $cacheStorage
 * @property Nette\Caching\Storages\PhpFileStorage $templateCacheStorage
 * @property Nette\Http\Request $httpRequest
 * @property Nette\Http\Response $httpResponse
 * @property Nette\Http\Context $httpContext
 * @property Nette\Http\Session $session
 * @property Nette\Http\User $user
 * @property Nette\Application\Application $application
 * @property Nette\Application\Routers\RouteList $router
 * @property Nette\Application\PresenterFactory $presenterFactory
 * @property Nette\Mail\SendmailMailer $mailer
 * @property Authenticator $authenticator
 * @property Nette\Database\Connection $database
 * @property Nette\DI\Container $container
 */
class SystemContainer extends Nette\DI\Container
{

    public function __construct()
    {
        parent::__construct(array(
            'appDir' => '...',
            'wwwDir' => '...',
            'productionMode' => FALSE,
            'tempDir' => '...',
            'database' => array(
                'driver' => 'mysql',
                'host' => 'localhost',
                'dbname' => 'test',
                'user' => NULL,
                'password' => NULL,
            ),
        ));
    }


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


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


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


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


    /**
     * @return Nette\Http\Request
     */
    protected function createServiceHttpRequest()
    {
        $service = $this->createHttpRequestFactory()->createHttpRequest();
        if (!$service instanceof Nette\Http\Request) {
            throw new Nette\UnexpectedValueException('Unable to create service \'httpRequest\'.');
        }
        return $service;
    }


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


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


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


    /**
     * @return Nette\Http\User
     */
    protected function createServiceUser()
    {
        $service = new Nette\Http\User($this->session, $this);
        return $service;
    }


    /**
     * @return Nette\Application\Application
     */
    protected function createServiceApplication()
    {
        $service = new Nette\Application\Application($this->presenterFactory, $this->router,
            $this->httpRequest, $this->httpResponse, $this->session);
        $service->catchExceptions = FALSE;
        Nette\Application\Diagnostics\RoutingPanel::initialize($service, $this->httpRequest);
        return $service;
    }


    /**
     * @return Nette\Application\Routers\RouteList
     */
    protected function createServiceRouter()
    {
        $service = new Nette\Application\Routers\RouteList;
        return $service;
    }


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


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


    /**
     * @return Authenticator
     */
    protected function createServiceAuthenticator()
    {
        $service = new Authenticator($this->database->table('users'));
        return $service;
    }


    /**
     * @return Nette\Database\Connection
     */
    protected function createServiceDatabase()
    {
        $service = new Nette\Database\Connection('mysql:host=localhost;dbname=test');
        $service->setCacheStorage($this->cacheStorage);
        $service->setDatabaseReflection(
            new Nette\Database\Reflection\DiscoveredReflection($this->cacheStorage));
        return $service;
    }


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


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

}

Login to submit a comment