Skip to content

Instantly share code, notes, and snippets.

@lavoiesl

lavoiesl/app-console.php

Last active Dec 18, 2015
Embed
What would you like to do?
Symfony 2.3 environment in parameters.yml
#!/usr/bin/env php
<?php
// app/console
// if you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);
set_time_limit(0);
require_once __DIR__.'/bootstrap.php.cache';
require_once __DIR__.'/AppKernel.php';
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
$parameters_file = __DIR__ . '/config/parameters.yml';
$environment = getenv('SYMFONY_ENV') ?: 'dev';
if (file_exists($parameters_file) && ($parameters = file_get_contents($parameters_file))) {
preg_match('/^\s*environment:\s*(dev|test|prod)/m', $parameters, $matches);
if (isset($matches[1])) {
$environment = $matches[1];
}
}
$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), $environment);
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';
$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$application->run($input);
<?php
/**
* Since Symfony 2.3, parameters.yml is ignored and generated via parameters.yml.dist
* This file generates an app.php based on the original app.php and app_dev.php, depending on a parameter from parameters.yml
*
* Used parameters are:
*
* environment: (dev|test|prod)
* localhost_only_dev: (true|false) whether or not to limit request to localhost when in dev mode
* apc_cache_id: (false|string) if a valid string, use this as an APC id for the caches
*
* If either of those variables are changed, you will have to remove app/cache/app.php
*/
use Symfony\Component\Yaml\Yaml;
////////
use Symfony\Component\HttpFoundation\Request;
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
////////
$app_file = __DIR__.'/../app/cache/app.php';
if (!file_exists($app_file)) {
$parameters_file = dirname(__DIR__) . '/app/config/parameters.yml';
try {
if (!file_exists($parameters_file)) {
throw new Exception("Unable to read parameters.yml");
}
$parameters = Yaml::parse($parameters_file);
// Extract parameters
$parameters = $parameters['parameters'];
$environment = $parameters['environment'];
$debug = $environment == 'prod' ? 'false' : 'true';
$localhost_only_dev = $parameters['environment'] != 'prod' && !empty($parameters['localhost_only_dev']) ? $parameters['localhost_only_dev'] : false;
$apc_cache_id = $parameters['environment'] == 'prod' && !empty($parameters['apc_cache_id']) ? $parameters['apc_cache_id'] : false;
$app_content = "<?php\n# This file was generated in " . __FILE__;
if ($localhost_only_dev) $app_content .= '
if (isset($_SERVER[\'HTTP_CLIENT_IP\'])
|| isset($_SERVER[\'HTTP_X_FORWARDED_FOR\'])
|| !in_array(@$_SERVER[\'REMOTE_ADDR\'], array(\'127.0.0.1\', \'fe80::1\', \'::1\'))
) {
header(\'HTTP/1.0 403 Forbidden\');
exit(\'You are not allowed to access this file.\');
}';
// Use APC for autoloading to improve performance.
// $apc_cache_id needs to be a unique prefix in order to prevent cache key conflicts with other applications also using APC.
if ($apc_cache_id) $app_content .= '
use Symfony\Component\ClassLoader\ApcClassLoader;
$loader = new ApcClassLoader(\''.$apc_cache_id.'\', $loader);
$loader->register(true);';
// Common code
$app_content .= '
$kernel = new AppKernel(\''.$environment.'\', '.$debug.');
$kernel->loadClassCache();';
if ($apc_cache_id) $app_content .= '
require_once __DIR__.\'/../AppCache.php\';
$kernel = new AppCache($kernel);';
// Strip whitespace;
$app_content = preg_replace('/^\s+/m','',$app_content);
if (!is_dir(dirname($app_file)) && !mkdir(dirname($app_file), 0777, true)) {
throw new Exception("Unable to generate app/cache/app.php");
}
if (!file_put_contents($app_file, $app_content)) {
throw new Exception("Unable to generate app/cache/app.php");
}
} catch (Exception $e) {
header('HTTP/1.0 500 Internal Server Error');
die($e->getMessage());
}
}
require $app_file;
////////
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
////////
# app/config/parameters.yml.dist
#
parameters:
# ...
environment: dev
localhost_only_dev: true
apc_cache_id: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment