Skip to content

Instantly share code, notes, and snippets.

@mykiwi
Last active December 17, 2015 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mykiwi/5569163 to your computer and use it in GitHub Desktop.
Save mykiwi/5569163 to your computer and use it in GitHub Desktop.
Dispatcher between Symfony2 and a legacy application
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
// Hack SwiftMailer (symfony1 vs Symfony2)
define('SWIFT_REQUIRED_LOADED', true);
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$env = 'prod';
$appUrlMatcher = sprintf(__DIR__.'/../cache/%s/app%sUrlMatcher.php', $env, ucfirst($env));
if (!file_exists($appUrlMatcher)) {
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel($env, true);
$kernel->boot();
$kernel->getContainer()->get('router')->getMatcher();
}
$classUrlMatcher = basename($appUrlMatcher, '.php');
require_once $appUrlMatcher;
$context = new RequestContext();
$context->fromRequest($request);
$matcher = new $classUrlMatcher($context);
try {
$attributes = $matcher->match($request->getPathInfo());
} catch (ResourceNotFoundException $e) {
// The requested route does not exist in the Symfony2 application.
include __DIR__.'/index.php'; // Launch the legacy application.
return;
}
foreach ($attributes AS $key=>$attribute) {
$request->attributes->set($key, $attribute);
}
if (!isset($kernel)) {
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel($env, false);
$kernel->boot();
}
// Hack SwiftMailer (symfony1 vs Symfony2) > /vendor/swiftmailer/swiftmailer/lib/swift_required.php
$path = __DIR__.'/../vendor/swiftmailer/swiftmailer/lib';
require $path . '/classes/Swift.php';
if (!function_exists('_swiftmailer_init')) {
function _swiftmailer_init()
{
require $path . '/swift_init.php';
}
}
Swift::registerAutoload('_swiftmailer_init');
// end hack
$kernel->loadClassCache();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
<?php
use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
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. Check '.basename(__FILE__).' for more information.');
}
// Hack SwiftMailer (symfony1 vs Symfony2)
define('SWIFT_REQUIRED_LOADED', true);
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Debug::enable();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$env = 'dev';
$appUrlMatcher = sprintf(__DIR__.'/../cache/%s/app%sUrlMatcher.php', $env, ucfirst($env));
if (!file_exists($appUrlMatcher)) {
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel($env, true);
$kernel->boot();
$kernel->getContainer()->get('router')->getMatcher();
}
$classUrlMatcher = basename($appUrlMatcher, '.php');
require_once $appUrlMatcher;
$context = new RequestContext();
$context->fromRequest($request);
$matcher = new $classUrlMatcher($context);
try {
$attributes = $matcher->match($request->getPathInfo());
} catch (ResourceNotFoundException $e) {
// The requested route does not exist in the Symfony2 application.
include __DIR__.'/index.php'; // Launch the legacy application.
return;
}
if (!array_key_exists('token', $attributes)) {
foreach ($attributes AS $key=>$attribute) {
$request->attributes->set($key, $attribute);
}
}
if (!isset($kernel)) {
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel($env, true);
$kernel->boot();
}
// Hack SwiftMailer (symfony1 vs Symfony2) > /vendor/swiftmailer/swiftmailer/lib/swift_required.php
$path = __DIR__.'/../vendor/swiftmailer/swiftmailer/lib';
require $path . '/classes/Swift.php';
if (!function_exists('_swiftmailer_init')) {
function _swiftmailer_init()
{
require $path . '/swift_init.php';
}
}
Swift::registerAutoload('_swiftmailer_init');
// end hack
$kernel->loadClassCache();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment