CompilerExtension a konfigurační soubor
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- David Grudl
- Nette Core | 8249
Při psaní rozšíření pro DI kompiler není nutné všechny služby a parametry nastavovat ve zdrojovém kódu a lze s výhodou využít zápis v NEON. Například NetteExtension by mohl vypadat takto:
class NetteExtension extends Nette\Config\CompilerExtension
{
public function loadConfiguration()
{
// lze zapsat i $this->compiler->parseServices()
Nette\Config\Compiler::parseServices(
$this->getContainer(),
$this->loadFromFile(__DIR__ . '/framework.neon')
);
}
...
}
A soubor framework.neon takto:
services:
# cache
cacheJournal:
class: Nette\Caching\Storages\FileJournal(%tempDir%)
cacheStorage:
class: Nette\Caching\Storages\FileStorage(%tempDir%/cache)
templateCacheStorage:
class: Nette\Caching\Storages\PhpFileStorage(%tempDir%/cache)
autowired: no
# http
httpRequest:
class: Nette\Http\Request
factory: @httpRequestFactory::createHttpRequest
httpResponse: Nette\Http\Response
httpContext: Nette\Http\Context
user: Nette\Http\User
session:
class: Nette\Http\Session
setup:
# - setOptions
# application
router: Nette\Application\Routers\RouteList
presenterFactory: Nette\Application\PresenterFactory(%appDir%)
application:
class: Nette\Application\Application
setup:
- $catchExceptions(%productionMode%)
- Nette\Application\Diagnostics\RoutingPanel::initialize
# mailer
mailer: Nette\Mail\SendmailMailer
factories:
httpRequestFactory:
class: Nette\Http\RequestFactory
setup:
- setEncoding(UTF-8)
internal: TRUE
- David Grudl
- Nette Core | 8249
Ještě je možný alternativní zápis pomocí „anonymních“ služeb, tedy služeb, které jako název mají rovnou název třídy a voláme je čistě přes auto-wiring. Výše uvedený konfigurák by vypadal takto:
services:
# aliases for back compatibility
application: @Nette\Application\Application
router: @Nette\Application\Routers\RouteList
session: @Nette\Http\Session
user: @Nette\Http\User
cacheStorage: @Nette\Caching\Storages\FileStorage
# cache
Nette\Caching\Storages\FileJournal: self(%tempDir%)
Nette\Caching\Storages\FileStorage: self(%tempDir%/cache)
templateCacheStorage:
class: Nette\Caching\Storages\PhpFileStorage(%tempDir%/cache)
autowired: no
# http
Nette\Http\Request:
class: self
factory: @httpRequestFactory::createHttpRequest
Nette\Http\Response: self
Nette\Http\Context: self
Nette\Http\User: self
Nette\Http\Session:
class: self
setup:
# - setOptions
# application
Nette\Application\Routers\RouteList: self
Nette\Application\PresenterFactory: self(%appDir%)
Nette\Application\Application:
class: self
setup:
- $catchExceptions(%productionMode%)
- Nette\Application\Diagnostics\RoutingPanel::initialize
# mailer
Nette\Mail\SendmailMailer: self
factories:
httpRequestFactory:
class: Nette\Http\RequestFactory
setup:
- setEncoding(UTF-8)
internal: TRUE