public function initRendering()
{
$this->register(new \Silex\Provider\TwigServiceProvider(), array('twig.form.templates' => array('Form/form_layout.twig')));
$this['twig'] = $this->share($this->extend('twig', function (\Twig_Environment $twig, \Silex\Application $app) {
$twig->addExtension(new \Eccube\Twig\Extension\EccubeExtension($app));
$twig->addExtension(new \Twig_Extension_StringLoader());
return $twig;
}));
$this->before(function (Request $request, \Silex\Application $app) {
$app['admin'] = false;
$app['front'] = false;
$pathinfo = rawurldecode($request->getPathInfo());
if (strpos($pathinfo, '/' . trim($app['config']['admin_route'], '/') . '/') === 0) {
$app['admin'] = true;
} else {
$app['front'] = true;
}
// フロント or 管理画面ごとにtwigの探索パスを切り替える.
$app['twig'] = $app->share($app->extend('twig', function (\Twig_Environment $twig, \Silex\Application $app) {
$paths = array();
// 互換性がないのでprofiler とproduction 時のcacheを分離する
if (isset($app['profiler'])) {
$cacheBaseDir = __DIR__ . '/../../app/cache/twig/profiler/';
} else {
$cacheBaseDir = __DIR__ . '/../../app/cache/twig/production/';
}
if ($app->isAdminRequest()) {
if (file_exists(__DIR__ . '/../../app/template/admin')) {
$paths[] = __DIR__ . '/../../app/template/admin';
}
$paths[] = $app['config']['template_admin_realdir'];
$paths[] = __DIR__ . '/../../app/Plugin';
$cache = $cacheBaseDir . 'admin';
} else {
if (file_exists($app['config']['template_realdir'])) {
$paths[] = $app['config']['template_realdir'];
}
$paths[] = $app['config']['template_default_realdir'];
$paths[] = __DIR__ . '/../../app/Plugin';
$cache = $cacheBaseDir . $app['config']['template_code'];
$app['front'] = true;
}
$twig->setCache($cache);
$app['twig.loader']->addLoader(new \Twig_Loader_Filesystem($paths));
return $twig;
}));
// 管理画面のIP制限チェック.
if ($app->isAdminRequest()) {
// IP制限チェック
$allowHost = $app['config']['admin_allow_host'];
if (count($allowHost) > 0) {
if (array_search($app['request']->getClientIp(), $allowHost) === false) {
throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException();
}
}
}
}, self::EARLY_EVENT);
// twigのグローバル変数を定義.
$app = $this;
$this->on(\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER, function (\Symfony\Component\HttpKernel\Event\FilterControllerEvent $event) use($app) {
// 未ログイン時にマイページや管理画面以下にアクセスするとSubRequestで実行されるため,
// $event->isMasterRequest()ではなく、グローバル変数が初期化済かどうかの判定を行う
if (isset($app['twig_global_initialized']) && $app['twig_global_initialized'] === true) {
return;
}
// ショップ基本情報
$BaseInfo = $app['eccube.repository.base_info']->get();
$app['twig']->addGlobal('BaseInfo', $BaseInfo);
if ($app->isAdminRequest()) {
// 管理画面
// 管理画面メニュー
$menus = array('', '', '');
$app['twig']->addGlobal('menus', $menus);
$Member = $app->user();
if (is_object($Member)) {
// ログインしていれば管理者のロールを取得
$AuthorityRoles = $app['eccube.repository.authority_role']->findBy(array('Authority' => $Member->getAuthority()));
$roles = array();
foreach ($AuthorityRoles as $AuthorityRole) {
// 管理画面でメニュー制御するため相対パス全てをセット
$roles[] = $app['request']->getBaseUrl() . '/' . $app['config']['admin_route'] . $AuthorityRole->getDenyUrl();
}
$app['twig']->addGlobal('AuthorityRoles', $roles);
}
} else {
// フロント画面
$request = $event->getRequest();
$route = $request->attributes->get('_route');
// ユーザ作成画面
if ($route === 'user_data') {
$params = $request->attributes->get('_route_params');
$route = $params['route'];
// プレビュー画面
} elseif ($request->get('preview')) {
$route = 'preview';
}
try {
$DeviceType = $app['eccube.repository.master.device_type']->find(\Eccube\Entity\Master\DeviceType::DEVICE_TYPE_PC);
$PageLayout = $app['eccube.repository.page_layout']->getByUrl($DeviceType, $route);
} catch (\Doctrine\ORM\NoResultException $e) {
$PageLayout = $app['eccube.repository.page_layout']->newPageLayout($DeviceType);
}
$app['twig']->addGlobal('PageLayout', $PageLayout);
$app['twig']->addGlobal('title', $PageLayout->getName());
}
$app['twig_global_initialized'] = true;
});
}