Skip to content

Instantly share code, notes, and snippets.

@zdenekdrahos
Last active March 21, 2016 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zdenekdrahos/8999a11347cb0df2be5a to your computer and use it in GitHub Desktop.
Save zdenekdrahos/8999a11347cb0df2be5a to your computer and use it in GitHub Desktop.
Nette - bootstrap (https://quip.com/1DAjAVxx9gZ8)
<?php
// app/bootstrap.php
function getService($service, array $config = [])
{
return initApplication($config)->getService($service);
}
function initApplication(array $config)
{
static $container = null;
if ($container == null) {
$configurator = new Nette\Configurator;
if (in_array('enableProduction', $config)) {
$configurator->setDebugMode(Nette\Configurator::NONE);
}
if (in_array('enableDebugger', $config)) {
$configurator->enableDebugger(__DIR__ . '/../var/log');
}
$configurator->setTempDirectory(__DIR__ . '/../var/temp');
// define autoloader in composer.json, don't use RobotLoader
$configurator->addConfig(__DIR__ . '/config/config.neon');
$configurator->addConfig(__DIR__ . '/config/parameters.neon');
$container = $configurator->createContainer();
convertPhpErrorsToExceptions();
}
return $container;
}
function convertPhpErrorsToExceptions()
{
set_error_handler('phpErrorToException');
}
function phpErrorToException($severity, $message, $filename, $lineno)
{
if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
}
{
"autoload": {
"psr-4": {
"App\\": ["app/console/", "app/model/", "app/presenters/", "app/router/"],
"Spider\\": "src/"
},
"files": ["app/bootstrap.php"]
},
"config": {
"bin-dir": "bin",
"optimize-autoloader": true
}
}
#!/usr/bin/env php
<?php
// bin/console
clearCache();
runNetteApp();
/**
* Console always runs in production mode :(
* https://forum.nette.org/cs/7001-mode-a-environment-console-vs-production-vs-development
* Otherwise Nette\DI\MissingServiceException can be thrown in bin/console
* http://ci.edgedesign.cz:8080/job/EON_019/119/console
*/
function clearCache()
{
exec('(rm -rf var/temp) && (mkdir var/temp) && (touch var/temp/.gitkeep)');
}
function runNetteApp()
{
require __DIR__ . '/../vendor/autoload.php';
getService('application')->run();
}
<?php
// www/index.php
require __DIR__ . '/../vendor/autoload.php';
getService('application', ['enableDebugger'])->run();
<?php
// tests/bootstrap.php
require_once __DIR__ . '/../vendor/autoload.php';
function givenNetteService($service)
{
return getService($service);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment