Created
December 7, 2016 05:53
-
-
Save izayoi256/bfd89c94cbc4f25e008b74b6a727f3a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Plugin\HogePlugin; | |
use Eccube\Application; | |
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | |
class Event | |
{ | |
/** | |
* @var Application | |
*/ | |
protected $app; | |
public function __construct($app) | |
{ | |
$this->app = $app; | |
} | |
/** | |
* @param FilterResponseEvent $event | |
*/ | |
public function onAdminHogeResponse($event) | |
{ | |
$app = $this->app; | |
$request = $event->getRequest(); | |
$response = $event->getResponse(); | |
$html = $response->getContent(); | |
/* @var $builder \Symfony\Component\Form\FormBuilderInterface */ | |
$builder = $app['form.factory']->createBuilder('hoge_form'); | |
/* @var $form \Symfony\Component\Form\FormInterface */ | |
$form = $builder->getForm(); | |
$elements = $xpath->query('id("hoge")'); | |
if ($elements && $elements->item(0)) { | |
$element = $elements->item(0)->parentNode; | |
$twig = $app->renderView( | |
'HogePlugin/Resource/template/default/Hoge/birth.twig', | |
array( | |
'form' => $form->createView(), | |
) | |
); | |
$template->appendXml($twig); | |
$element->parentNode->insertBefore($node, $element->nextSibling); | |
} | |
$response->setContent(mb_convert_encoding($dom->saveHTML(), 'UTF-8', 'HTML-ENTITIES')); | |
$event->setResponse($response); | |
} | |
/** | |
* @param string $html | |
* @return array [dom=>\DOMDocument, node=>\DOMNode, template=>\DOMDocumentFragment, xpath=>\DOMXPath] | |
*/ | |
protected function initDomParser($html) | |
{ | |
libxml_use_internal_errors(true); | |
$dom = new \DOMDocument(); | |
$dom->loadHTML(mb_convert_encoding('<!DOCTYPE html>' . $html, 'HTML-ENTITIES', 'auto')); | |
$dom->encoding = 'UTF-8'; | |
$dom->formatOutput = true; | |
$template = $dom->createDocumentFragment(); | |
$node = $dom->importNode($template, true); | |
$xpath = new \DOMXPath($dom); | |
return compact('dom', 'node', 'template', 'xpath'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment