Skip to content

Commit

Permalink
Merge pull request #32 from lchampel/master
Browse files Browse the repository at this point in the history
correction pour initialisation kernel symfony en mode prod sans le debug
  • Loading branch information
juliens committed Mar 21, 2016
2 parents c9c9d90 + 31fd683 commit d0563e2
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions sources/wordpress/sf2/sf2plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Sf2Plugin {
class Sf2Plugin
{
private $container = null;
private $kernel = null;
private $in_sf2 = true;

private function isValidSymfonyPath($dir)
{
return (file_exists($dir.'var/bootstrap.php.cache'));
return (file_exists($dir . 'var/bootstrap.php.cache'));
}

private function calculatePath()
{
$dir = __DIR__;
if (($pos = strpos($dir, 'vendor'))!==false) {
if (($pos = strpos($dir, 'vendor')) !== false) {
//Lien symbolique dans les vendor

$dir = substr($dir, 0, $pos);
if (!$this->isValidSymfonyPath($dir)) {
return null;
}
return $dir;
} elseif (($pos=strpos($dir, 'wordpress/wp-content'))!==false) {
} elseif (($pos = strpos($dir, 'wordpress/wp-content')) !== false) {
$dir = substr($dir, 0, $pos);
if (!$this->isValidSymfonyPath($dir)) {
return null;
Expand All @@ -40,14 +41,13 @@ private function calculatePath()

private function calculateUrl()
{
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if (($pos = strpos($url, 'wordpress/wp-admin')) != false) {
$url = substr($url, 0, $pos).'web/app_dev.php';
$url = substr($url, 0, $pos) . 'web/app_dev.php';
}
return $url;
}


private function loadSf2()
{
global $kernel;
Expand All @@ -58,24 +58,27 @@ private function loadSf2()
$url = (get_option('symfony2_url'));

if (!$this->isValidSymfonyPath($path)) {
add_action( 'admin_footer', array( $this, 'symfony2_warning' ) );
add_action('admin_footer', array($this, 'symfony2_warning'));
return;
}


if ($kernel==null) {
$loader = require_once $path.'var/bootstrap.php.cache';
$autoload= require_once $path.'app/autoload.php';
require_once $path.'app/AppKernel.php';
$kernel = new AppKernel($env, true);
if ($kernel == null) {
$loader = require_once $path . 'var/bootstrap.php.cache';
$autoload = require_once $path . 'app/autoload.php';
require_once $path . 'app/AppKernel.php';
$debug = true;
if ($env == 'prod') {
$debug = false;
}
$kernel = new AppKernel($env, $debug);
$kernel->loadClassCache();
$kernel->boot();
$this->kernel = $kernel;
$this->container = $kernel->getContainer();
if ($this->container->get('session')->isStarted() == false) {
$this->container->get('session')->start();
}
if ($url!=null) {
if ($url != null) {
$this->overloadUrlContext($url);
}
} else {
Expand All @@ -84,7 +87,7 @@ private function loadSf2()
}
$wp_loader = $this->container->get('wordpress.loader');
$wp_loader->load();

$request = Request::createFromGlobals();
$response = new Response();
$kernel->terminate($request, $response);
Expand All @@ -94,13 +97,13 @@ private function loadSf2()
private function overloadUrlContext($url)
{
preg_match('%([^:]*):\/\/([^\/]*)(\/?.*)%', $url, $matches);
if (count($matches)==4) {
if (count($matches) == 4) {
$context = $this->container->get('router')->getContext();
$context->setHost($matches[2]);
$context->setScheme($matches[1]);
$context->setBaseUrl($matches[3]);
} else {
add_action( 'admin_footer', array( $this, 'symfony2_url_warning' ) );
add_action('admin_footer', array($this, 'symfony2_url_warning'));

}
}
Expand All @@ -126,7 +129,7 @@ public function __construct()
{
//register_activation_hook( __FILE__, array($this, 'activate'));
add_action('activate_sf2/sf2plugin.php', array($this, 'activate'));
add_action('admin_menu', array ($this, 'menu_params') );
add_action('admin_menu', array($this, 'menu_params'));
add_action('admin_init', array($this, 'admin_init'));

$this->loadSf2();
Expand All @@ -135,13 +138,13 @@ public function __construct()

public function activate()
{
if (get_option('symfony2_path')==null) {
if (get_option('symfony2_path') == null) {
update_option('symfony2_path', $this->calculatePath());
}
if (get_option('symfony2_env')==null) {
if (get_option('symfony2_env') == null) {
update_option('symfony2_env', 'dev');
}
if (get_option('symfony2_url')==null) {
if (get_option('symfony2_url') == null) {
update_option('symfony2_url', $this->calculateUrl());
}

Expand All @@ -150,10 +153,10 @@ public function activate()
public function admin_init()
{
//add_management_page( 'Custom Permalinks', 'Custom Permalinks', 'manage_options', 'my-unique-identifier', 'custom_permalinks_options_page' );
if ($this->container!=null) {
if ($this->container != null) {
$shortcodes = $this->getContainer()->get('wordpress.loader')->getShortcodes();
foreach ($shortcodes as $shortcode) {
register_setting('wp_symfony_settings', 'shortcode_'.$shortcode->getName());
register_setting('wp_symfony_settings', 'shortcode_' . $shortcode->getName());
}
}
register_setting('wp_symfony_settings', 'symfony2_path');
Expand All @@ -163,21 +166,21 @@ public function admin_init()

public function menu_params()
{
add_options_page( 'Symfony2 configuration','Symfony2','manage_options','options_symfony2', array( $this, 'settings_symfony2' ) );
add_options_page('Symfony2 configuration', 'Symfony2', 'manage_options', 'options_symfony2', array($this, 'settings_symfony2'));
//add_menu_page('Symfony2 configuration', 'Symfony2', 'manage_options', 'symfony2_options', array($this, 'menu_params_page'), plugins_url( 'sf2/images/icon.png' ), 6 );
}

public function settings_symfony2()
{
include(dirname(__FILE__).'/settings.php');
include dirname(__FILE__) . '/settings.php';
}

public function menu_params_page()
{
if ($this->container!=null) {
if ($this->container != null) {
$shortcodes = $this->getContainer()->get('wordpress.loader')->getShortcodes();
}
include(dirname(__FILE__).'/settings.php');
include dirname(__FILE__) . '/settings.php';
}

public function get($id)
Expand All @@ -186,6 +189,4 @@ public function get($id)
}
}



$sf2plugin = new Sf2Plugin();

0 comments on commit d0563e2

Please sign in to comment.