-
Notifications
You must be signed in to change notification settings - Fork 70
refactor getViewsReferences to read the cache if already computed #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace Victoire\Bundle\CoreBundle\CacheWarmer; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Victoire\Bundle\CoreBundle\Helper\ViewCacheHelper; | ||
use Victoire\Bundle\CoreBundle\Helper\ViewHelper; | ||
|
||
|
@@ -18,10 +19,11 @@ class ViewCacheWarmer | |
* @param ViewHelper $viewHelper @victoire_page.page_helper | ||
* @param ViewCacheHelper $viewCacheHelper @victoire_core.view_cache_helper | ||
*/ | ||
public function __construct(ViewHelper $viewHelper, ViewCacheHelper $viewCacheHelper) | ||
public function __construct(ViewHelper $viewHelper, ViewCacheHelper $viewCacheHelper, EntityManager $entityManager) | ||
{ | ||
$this->viewHelper = $viewHelper; | ||
$this->viewCacheHelper = $viewCacheHelper; | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
/** | ||
|
@@ -31,7 +33,8 @@ public function __construct(ViewHelper $viewHelper, ViewCacheHelper $viewCacheHe | |
public function warmUp($cacheDir) | ||
{ | ||
if (!$this->viewCacheHelper->fileExists()) { | ||
$viewsReferences = $this->viewHelper->getAllViewsReferences(); | ||
$views = $this->entityManager->createQuery("SELECT v FROM VictoireCoreBundle:View v")->getResult(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation ? |
||
$viewsReferences = $this->viewHelper->buildViewsReferences($views); | ||
$this->viewCacheHelper->write($viewsReferences); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,8 @@ public function configure() | |
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$viewsReferences = $this->getContainer()->get('victoire_core.view_helper')->getAllViewsReferences(); | ||
$views = $this->getContainer()->get('doctrine.orm.entity_manager')->createQuery("SELECT v FROM VictoireCoreBundle:View v")->getResult(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation |
||
$viewsReferences = $this->getContainer()->get('victoire_core.view_helper')->buildViewsReferences($views); | ||
$this->getContainer()->get('victoire_core.view_cache_helper')->write($viewsReferences); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ | |
|
||
namespace Victoire\Bundle\CoreBundle\Helper; | ||
|
||
use Symfony\Component\Config\Util\XmlUtils; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\DependencyInjection\SimpleXMLElement; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Victoire\Bundle\BusinessEntityPageBundle\Entity\BusinessEntityPage; | ||
use Victoire\Bundle\CoreBundle\Entity\View; | ||
|
@@ -60,6 +60,9 @@ public function buildItemNode($viewReference, $itemNode) | |
if (array_key_exists('locale', $viewReference)) { | ||
$itemNode->addAttribute('locale', $viewReference['locale']); | ||
} | ||
if (array_key_exists('name', $viewReference)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation |
||
$itemNode->addAttribute('name', $viewReference['name']); | ||
} | ||
} | ||
/** | ||
* Write given views references in a xml file | ||
|
@@ -68,7 +71,7 @@ public function buildItemNode($viewReference, $itemNode) | |
*/ | ||
public function write($viewsReferences) | ||
{ | ||
$rootNode = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8' ?><viewReferences></viewReferences>"); | ||
$rootNode = new \SimpleXMLElement("<?xml version='1.0' encoding='UTF-8' ?><viewReferences></viewReferences>"); | ||
|
||
foreach ($viewsReferences as $viewReference) { | ||
$itemNode = $rootNode->addChild('viewReference'); | ||
|
@@ -81,11 +84,36 @@ public function write($viewsReferences) | |
/** | ||
* get the content of the view cache file | ||
* | ||
* @return SimpleXMLElement | ||
* @return \SimpleXMLElement | ||
*/ | ||
public function readCache() | ||
{ | ||
return new SimpleXMLElement(file_get_contents($this->xmlFile)); | ||
return new \SimpleXMLElement(file_get_contents($this->xmlFile)); | ||
} | ||
|
||
public function convertXmlCacheToArray() | ||
{ | ||
$xml = $this->readCache(); | ||
|
||
$cachedArray = json_decode(json_encode((array) $xml), TRUE); | ||
$viewsReferences = []; | ||
|
||
|
||
foreach ($cachedArray['viewReference'] as $cachedViewReference) { | ||
$viewReference['id'] = !empty($cachedViewReference['@attributes']['id']) ? $cachedViewReference['@attributes']['id'] : null ; | ||
$viewReference['locale'] = !empty($cachedViewReference['@attributes']['locale']) ? $cachedViewReference['@attributes']['locale'] : null ; | ||
$viewReference['entityId'] = !empty($cachedViewReference['@attributes']['entityId']) ? $cachedViewReference['@attributes']['entityId'] : null ; | ||
$viewReference['entityNamespace'] = !empty($cachedViewReference['@attributes']['entityNamespace']) ? $cachedViewReference['@attributes']['entityNamespace'] : null ; | ||
$viewReference['url'] = !empty($cachedViewReference['@attributes']['url']) ? $cachedViewReference['@attributes']['url'] : null ; | ||
$viewReference['viewId'] = !empty($cachedViewReference['@attributes']['viewId']) ? $cachedViewReference['@attributes']['viewId'] : null ; | ||
$viewReference['viewNamespace'] = !empty($cachedViewReference['@attributes']['viewNamespace']) ? $cachedViewReference['@attributes']['viewNamespace'] : null ; | ||
$viewReference['patternId'] = !empty($cachedViewReference['@attributes']['patternId']) ? $cachedViewReference['@attributes']['patternId'] : null ; | ||
$viewReference['name'] = !empty($cachedViewReference['@attributes']['name']) ? $cachedViewReference['@attributes']['name'] : null ; | ||
|
||
$viewsReferences[] = $viewReference; | ||
} | ||
|
||
return $viewsReferences; | ||
} | ||
|
||
/** | ||
|
@@ -131,14 +159,15 @@ public function getReferenceByParameters($parameters) | |
$arguments = array_merge($arguments, $locale); | ||
|
||
if ($xmlReference = $this->readCache()->xpath("//viewReference[".implode(' and ', $arguments)."]")) { | ||
$viewReference['id'] = $xmlReference[0]->getAttributeAsPhp('id'); | ||
$viewReference['locale'] = $xmlReference[0]->getAttributeAsPhp('locale'); | ||
$viewReference['entityId'] = $xmlReference[0]->getAttributeAsPhp('entityId'); | ||
$viewReference['entityNamespace'] = $xmlReference[0]->getAttributeAsPhp('entityNamespace'); | ||
$viewReference['url'] = $xmlReference[0]->getAttributeAsPhp('url'); | ||
$viewReference['viewId'] = $xmlReference[0]->getAttributeAsPhp('viewId'); | ||
$viewReference['viewNamespace'] = $xmlReference[0]->getAttributeAsPhp('viewNamespace'); | ||
$viewReference['patternId'] = $xmlReference[0]->getAttributeAsPhp('patternId'); | ||
$viewReference['id'] = XmlUtils::phpize($xmlReference[0]['id']); | ||
$viewReference['locale'] = XmlUtils::phpize($xmlReference[0]['locale']); | ||
$viewReference['entityId'] = XmlUtils::phpize($xmlReference[0]['entityId']); | ||
$viewReference['entityNamespace'] = XmlUtils::phpize($xmlReference[0]['entityNamespace']); | ||
$viewReference['url'] = XmlUtils::phpize($xmlReference[0]['url']); | ||
$viewReference['viewId'] = XmlUtils::phpize($xmlReference[0]['viewId']); | ||
$viewReference['viewNamespace'] = XmlUtils::phpize($xmlReference[0]['viewNamespace']); | ||
$viewReference['patternId'] = XmlUtils::phpize($xmlReference[0]['patternId']); | ||
$viewReference['name'] = XmlUtils::phpize($xmlReference[0]['name']); | ||
} else { | ||
$viewReference = null; | ||
} | ||
|
@@ -162,12 +191,12 @@ public function getViewReferenceId(View $view, $entity = null) | |
} | ||
|
||
/** | ||
* write SimpleXMLElement in the cache file | ||
* @param SimpleXMLElement $rootNode | ||
* write \SimpleXMLElement in the cache file | ||
* @param \SimpleXMLElement $rootNode | ||
* | ||
* @return void | ||
*/ | ||
protected function writeFile(SimpleXMLElement $rootNode) | ||
protected function writeFile(\SimpleXMLElement $rootNode) | ||
{ | ||
if (!is_dir(dirname($this->xmlFile))) { | ||
mkdir(dirname($this->xmlFile), 0777, true); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation ?