diff --git a/app/config/eccube/services.yaml b/app/config/eccube/services.yaml index 8fabac62760..088488f8066 100644 --- a/app/config/eccube/services.yaml +++ b/app/config/eccube/services.yaml @@ -45,6 +45,9 @@ services: lazy: true public: true + Ecccube\Service\CartService: + lazy: true + Eccube\Service\SystemService: lazy: true public: true diff --git a/src/Eccube/Controller/Admin/Content/BlockController.php b/src/Eccube/Controller/Admin/Content/BlockController.php index c463dc71232..e596288eaf8 100644 --- a/src/Eccube/Controller/Admin/Content/BlockController.php +++ b/src/Eccube/Controller/Admin/Content/BlockController.php @@ -21,6 +21,7 @@ use Eccube\Form\Type\Admin\BlockType; use Eccube\Repository\BlockRepository; use Eccube\Repository\Master\DeviceTypeRepository; +use Eccube\Util\CacheUtil; use Eccube\Util\StringUtil; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\Filesystem\Filesystem; @@ -80,7 +81,7 @@ public function index(Request $request) * @Route("/%eccube_admin_route%/content/block/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_block_edit") * @Template("@admin/Content/block_edit.twig") */ - public function edit(Request $request, $id = null, Environment $twig, FileSystem $fs) + public function edit(Request $request, $id = null, Environment $twig, FileSystem $fs, CacheUtil $cacheUtil) { $DeviceType = $this->deviceTypeRepository ->find(DeviceType::DEVICE_TYPE_PC); @@ -153,9 +154,9 @@ public function edit(Request $request, $id = null, Environment $twig, FileSystem } } - // twigキャッシュの削除 - $cacheDir = $this->getParameter('kernel.cache_dir').'/twig'; - $fs->remove($cacheDir); + // キャッシュの削除 + $cacheUtil->clearTwigCache(); + $cacheUtil->clearDoctrineCache(); $event = new EventArgs( [ @@ -181,7 +182,7 @@ public function edit(Request $request, $id = null, Environment $twig, FileSystem /** * @Route("/%eccube_admin_route%/content/block/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_block_delete", methods={"DELETE"}) */ - public function delete(Request $request, Block $Block, Filesystem $fs) + public function delete(Request $request, Block $Block, Filesystem $fs, CacheUtil $cacheUtil) { $this->isTokenValid(); @@ -210,9 +211,9 @@ public function delete(Request $request, Block $Block, Filesystem $fs) $this->addSuccess('admin.common.delete_complete', 'admin'); - // twigキャッシュの削除 - $cacheDir = $this->getParameter('kernel.cache_dir').'/twig'; - $fs->remove($cacheDir); + // キャッシュの削除 + $cacheUtil->clearTwigCache(); + $cacheUtil->clearDoctrineCache(); } return $this->redirectToRoute('admin_content_block'); diff --git a/src/Eccube/Controller/Admin/Content/LayoutController.php b/src/Eccube/Controller/Admin/Content/LayoutController.php index 3b08b4376b9..31e0e11fe78 100644 --- a/src/Eccube/Controller/Admin/Content/LayoutController.php +++ b/src/Eccube/Controller/Admin/Content/LayoutController.php @@ -25,6 +25,7 @@ use Eccube\Repository\PageRepository; use Eccube\Repository\ProductRepository; use Eccube\Repository\Master\DeviceTypeRepository; +use Eccube\Util\CacheUtil; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -123,7 +124,7 @@ public function index() * * @return RedirectResponse */ - public function delete(Layout $Layout) + public function delete(Layout $Layout, CacheUtil $cacheUtil) { $this->isTokenValid(); @@ -139,6 +140,9 @@ public function delete(Layout $Layout) $this->addSuccess('admin.common.delete_complete', 'admin'); + // キャッシュの削除 + $cacheUtil->clearDoctrineCache(); + return $this->redirectToRoute('admin_content_layout'); } @@ -147,7 +151,7 @@ public function delete(Layout $Layout) * @Route("/%eccube_admin_route%/content/layout/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_layout_edit") * @Template("@admin/Content/layout.twig") */ - public function edit(Request $request, $id = null, $previewPageId = null) + public function edit(Request $request, $id = null, $previewPageId = null, CacheUtil $cacheUtil) { if (is_null($id)) { $Layout = new Layout(); @@ -190,6 +194,9 @@ public function edit(Request $request, $id = null, $previewPageId = null) $data = $request->request->all(); $this->blockPositionRepository->register($data, $Blocks, $UnusedBlocks, $Layout); + // キャッシュの削除 + $cacheUtil->clearDoctrineCache(); + // プレビューモード if ($this->isPreview) { // プレビューする画面を取得 @@ -266,11 +273,11 @@ public function viewBlock(Request $request, Twig $twig) /** * @Route("/%eccube_admin_route%/content/layout/{id}/preview", requirements={"id" = "\d+"}, name="admin_content_layout_preview") */ - public function preview(Request $request, $id) + public function preview(Request $request, $id, CacheUtil $cacheUtil) { $form = $request->get('admin_layout'); $this->isPreview = true; - return $this->edit($request, $id, $form['Page']); + return $this->edit($request, $id, $form['Page'], $cacheUtil); } } diff --git a/src/Eccube/Controller/Admin/Content/NewsController.php b/src/Eccube/Controller/Admin/Content/NewsController.php index fd2f0c84c5c..b6d9c7c0367 100644 --- a/src/Eccube/Controller/Admin/Content/NewsController.php +++ b/src/Eccube/Controller/Admin/Content/NewsController.php @@ -19,6 +19,7 @@ use Eccube\Event\EventArgs; use Eccube\Form\Type\Admin\NewsType; use Eccube\Repository\NewsRepository; +use Eccube\Util\CacheUtil; use Knp\Component\Pager\Paginator; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\Request; @@ -90,7 +91,7 @@ public function index(Request $request, $page_no = 1, Paginator $paginator) * * @return array|\Symfony\Component\HttpFoundation\RedirectResponse */ - public function edit(Request $request, $id = null) + public function edit(Request $request, $id = null, CacheUtil $cacheUtil) { if ($id) { $News = $this->newsRepository->find($id); @@ -134,6 +135,9 @@ public function edit(Request $request, $id = null) $this->addSuccess('admin.common.save_complete', 'admin'); + // キャッシュの削除 + $cacheUtil->clearDoctrineCache(); + return $this->redirectToRoute('admin_content_news_edit', ['id' => $News->getId()]); } @@ -153,7 +157,7 @@ public function edit(Request $request, $id = null) * * @return \Symfony\Component\HttpFoundation\RedirectResponse */ - public function delete(Request $request, News $News) + public function delete(Request $request, News $News, CacheUtil $cacheUtil) { $this->isTokenValid(); @@ -168,6 +172,10 @@ public function delete(Request $request, News $News) $this->addSuccess('admin.common.delete_complete', 'admin'); log_info('新着情報削除完了', [$News->getId()]); + + // キャッシュの削除 + $cacheUtil->clearDoctrineCache(); + } catch (\Exception $e) { $message = trans('admin.common.delete_error_foreign_key', ['%name%' => $News->getTitle()]); $this->addError($message, 'admin'); diff --git a/src/Eccube/Controller/Admin/Content/PageController.php b/src/Eccube/Controller/Admin/Content/PageController.php index 720f71ee870..9a09a360261 100644 --- a/src/Eccube/Controller/Admin/Content/PageController.php +++ b/src/Eccube/Controller/Admin/Content/PageController.php @@ -22,12 +22,14 @@ use Eccube\Repository\Master\DeviceTypeRepository; use Eccube\Repository\PageLayoutRepository; use Eccube\Repository\PageRepository; +use Eccube\Util\CacheUtil; use Eccube\Util\StringUtil; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Bundle\FrameworkBundle\Routing\Router; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Routing\RouterInterface; use Twig\Environment; class PageController extends AbstractController @@ -89,7 +91,7 @@ public function index(Request $request) * @Route("/%eccube_admin_route%/content/page/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_page_edit") * @Template("@admin/Content/page_edit.twig") */ - public function edit(Request $request, $id = null, Environment $twig, Router $router) + public function edit(Request $request, $id = null, Environment $twig, RouterInterface $router, CacheUtil $cacheUtil) { if (null === $id) { $Page = $this->pageRepository->newPage(); @@ -222,9 +224,9 @@ public function edit(Request $request, $id = null, Environment $twig, Router $ro $this->addSuccess('admin.common.save_complete', 'admin'); - // twig キャッシュの削除. - $cacheDir = $this->getParameter('kernel.cache_dir').'/twig'; - $fs->remove($cacheDir); + // キャッシュの削除 + $cacheUtil->clearTwigCache(); + $cacheUtil->clearDoctrineCache(); return $this->redirectToRoute('admin_content_page_edit', ['id' => $Page->getId()]); } @@ -251,7 +253,7 @@ public function edit(Request $request, $id = null, Environment $twig, Router $ro /** * @Route("/%eccube_admin_route%/content/page/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_page_delete", methods={"DELETE"}) */ - public function delete(Request $request, $id = null) + public function delete(Request $request, $id = null, CacheUtil $cacheUtil) { $this->isTokenValid(); @@ -286,6 +288,10 @@ public function delete(Request $request, $id = null) $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_DELETE_COMPLETE, $event); $this->addSuccess('admin.common.delete_complete', 'admin'); + + // キャッシュの削除 + $cacheUtil->clearTwigCache(); + $cacheUtil->clearDoctrineCache(); } return $this->redirectToRoute('admin_content_page'); diff --git a/src/Eccube/Controller/Admin/Product/CategoryController.php b/src/Eccube/Controller/Admin/Product/CategoryController.php index 0ae662be950..edbc439306c 100644 --- a/src/Eccube/Controller/Admin/Product/CategoryController.php +++ b/src/Eccube/Controller/Admin/Product/CategoryController.php @@ -21,6 +21,7 @@ use Eccube\Form\Type\Admin\CategoryType; use Eccube\Repository\CategoryRepository; use Eccube\Service\CsvExportService; +use Eccube\Util\CacheUtil; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -61,7 +62,7 @@ public function __construct( * @Route("/%eccube_admin_route%/product/category/{id}/edit", requirements={"id" = "\d+"}, name="admin_product_category_edit") * @Template("@admin/Product/category.twig") */ - public function index(Request $request, $parent_id = null, $id = null) + public function index(Request $request, $parent_id = null, $id = null, CacheUtil $cacheUtil) { if ($parent_id) { /** @var Category $Parent */ @@ -137,6 +138,9 @@ public function index(Request $request, $parent_id = null, $id = null) $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CATEGORY_INDEX_COMPLETE, $event); $this->addSuccess('admin.common.save_complete', 'admin'); + + $cacheUtil->clearDoctrineCache(); + if ($Parent) { return $this->redirectToRoute('admin_product_category_show', ['parent_id' => $Parent->getId()]); } else { @@ -162,6 +166,8 @@ public function index(Request $request, $parent_id = null, $id = null) $this->addSuccess('admin.common.save_complete', 'admin'); + $cacheUtil->clearDoctrineCache(); + if ($Parent) { return $this->redirectToRoute('admin_product_category_show', ['parent_id' => $Parent->getId()]); } else { @@ -198,7 +204,7 @@ public function index(Request $request, $parent_id = null, $id = null) /** * @Route("/%eccube_admin_route%/product/category/{id}/delete", requirements={"id" = "\d+"}, name="admin_product_category_delete", methods={"DELETE"}) */ - public function delete(Request $request, $id) + public function delete(Request $request, $id, CacheUtil $cacheUtil) { $this->isTokenValid(); @@ -226,6 +232,9 @@ public function delete(Request $request, $id) $this->addSuccess('admin.common.delete_complete', 'admin'); log_info('カテゴリ削除完了', [$id]); + + $cacheUtil->clearDoctrineCache(); + } catch (\Exception $e) { log_info('カテゴリ削除エラー', [$id, $e]); @@ -243,7 +252,7 @@ public function delete(Request $request, $id) /** * @Route("/%eccube_admin_route%/product/category/sort_no/move", name="admin_product_category_sort_no_move", methods={"POST"}) */ - public function moveSortNo(Request $request) + public function moveSortNo(Request $request, CacheUtil $cacheUtil) { if (!$request->isXmlHttpRequest()) { throw new BadRequestHttpException(); @@ -260,6 +269,8 @@ public function moveSortNo(Request $request) } $this->entityManager->flush(); + $cacheUtil->clearDoctrineCache(); + return new Response('Successful'); } } diff --git a/src/Eccube/Controller/Admin/Product/CsvImportController.php b/src/Eccube/Controller/Admin/Product/CsvImportController.php index 647094c6fb4..71779b41707 100644 --- a/src/Eccube/Controller/Admin/Product/CsvImportController.php +++ b/src/Eccube/Controller/Admin/Product/CsvImportController.php @@ -34,6 +34,7 @@ use Eccube\Repository\ProductRepository; use Eccube\Repository\TagRepository; use Eccube\Service\CsvImportService; +use Eccube\Util\CacheUtil; use Eccube\Util\StringUtil; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\Filesystem\Filesystem; @@ -538,7 +539,7 @@ public function csvProduct(Request $request) * @Route("/%eccube_admin_route%/product/category_csv_upload", name="admin_product_category_csv_import") * @Template("@admin/Product/csv_category.twig") */ - public function csvCategory(Request $request) + public function csvCategory(Request $request, CacheUtil $cacheUtil) { $form = $this->formFactory->createBuilder(CsvImportType::class)->getForm(); @@ -683,6 +684,8 @@ public function csvCategory(Request $request) log_info('カテゴリCSV登録完了'); $message = 'admin.common.csv_upload_complete'; $this->session->getFlashBag()->add('eccube.admin.success', $message); + + $cacheUtil->clearDoctrineCache(); } } } diff --git a/src/Eccube/Controller/Admin/Setting/Shop/ShopController.php b/src/Eccube/Controller/Admin/Setting/Shop/ShopController.php index 6a629fab658..0e11cd1e73c 100644 --- a/src/Eccube/Controller/Admin/Setting/Shop/ShopController.php +++ b/src/Eccube/Controller/Admin/Setting/Shop/ShopController.php @@ -96,7 +96,8 @@ public function index(Request $request, CacheUtil $cacheUtil) $event ); - $cacheUtil->clearCache(); + // キャッシュの削除 + $cacheUtil->clearDoctrineCache(); $this->addSuccess('admin.common.save_complete', 'admin'); diff --git a/src/Eccube/Entity/News.php b/src/Eccube/Entity/News.php index f9c9eb3afb3..2ab8ca5787f 100644 --- a/src/Eccube/Entity/News.php +++ b/src/Eccube/Entity/News.php @@ -24,6 +24,7 @@ * @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255) * @ORM\HasLifecycleCallbacks() * @ORM\Entity(repositoryClass="Eccube\Repository\NewsRepository") + * @ORM\Cache(usage="NONSTRICT_READ_WRITE") */ class News extends AbstractEntity { diff --git a/src/Eccube/EventListener/TwigInitializeListener.php b/src/Eccube/EventListener/TwigInitializeListener.php index c313225df0d..0b54e675b00 100644 --- a/src/Eccube/EventListener/TwigInitializeListener.php +++ b/src/Eccube/EventListener/TwigInitializeListener.php @@ -193,12 +193,7 @@ public function setFrontVariables(GetResponseEvent $event) // URLからPageを取得 /** @var Page $Page */ - $Page = $this->pageRepository->findOneBy(['url' => $route]); - - // 該当するPageがない場合は空のページをセット - if (!$Page) { - $Page = $this->pageRepository->newPage(); - } + $Page = $this->pageRepository->getPageByRoute($route); /** @var PageLayout[] $PageLayouts */ $PageLayouts = $Page->getPageLayouts(); @@ -227,22 +222,21 @@ public function setFrontVariables(GetResponseEvent $event) if ($request->get('preview')) { $is_admin = $request->getSession()->has('_security_admin'); if ($is_admin) { - $Page->getPageLayouts()->clear(); - $PageLayouts = $this->pageLayoutRepository->findBy(['layout_id' => 0]); - foreach ($PageLayouts as $PageLayout) { - $Page->addPageLayout($PageLayout); - } + $Layout = $this->layoutRepository->get(Layout::DEFAULT_LAYOUT_PREVIEW_PAGE); - $Layout->getBlockPositions()->clear(); - $BlockPositions = $this->blockPositionRepository->findBy(['layout_id' => 0]); - foreach ($BlockPositions as $BlockPosition) { - $Layout->addBlockPosition($BlockPosition); - } + $this->twig->addGlobal('Layout', $Layout); + $this->twig->addGlobal('Page', $Page); + $this->twig->addGlobal('title', $Page->getName()); + + return; } } - // Layoutのデータがない場合は空のLayoutをセット - if (!$Layout) { + if ($Layout) { + // lazy loadを制御するため, Layoutを取得しなおす. + $Layout = $this->layoutRepository->get($Layout->getId()); + } else { + // Layoutのデータがない場合は空のLayoutをセット $Layout = new Layout(); } diff --git a/src/Eccube/Repository/AbstractRepository.php b/src/Eccube/Repository/AbstractRepository.php index 1f135cf0e95..ff83dfe0826 100644 --- a/src/Eccube/Repository/AbstractRepository.php +++ b/src/Eccube/Repository/AbstractRepository.php @@ -46,8 +46,6 @@ public function save($entity) protected function getCacheLifetime() { - // $options = $this->eccubeConfig['doctrine_cache']; - // return $options['result_cache']['lifetime']; - return 0; // FIXME + return 3600; // TODO 設定に切り出すほどでもないので一旦固定で. } } diff --git a/src/Eccube/Repository/BaseInfoRepository.php b/src/Eccube/Repository/BaseInfoRepository.php index 9e3a651e6c8..488bdbe09f9 100644 --- a/src/Eccube/Repository/BaseInfoRepository.php +++ b/src/Eccube/Repository/BaseInfoRepository.php @@ -15,7 +15,6 @@ use Eccube\Entity\BaseInfo; use Symfony\Bridge\Doctrine\RegistryInterface; -use Symfony\Component\HttpKernel\KernelInterface; /** * BaseInfoRepository @@ -25,43 +24,29 @@ */ class BaseInfoRepository extends AbstractRepository { - /** - * @var KernelInterface - */ - protected $kernel; - /** * BaseInfoRepository constructor. * * @param RegistryInterface $registry - * @param KernelInterface $kernel */ - public function __construct(RegistryInterface $registry, KernelInterface $kernel) + public function __construct(RegistryInterface $registry) { parent::__construct($registry, BaseInfo::class); - $this->kernel = $kernel; } /** * @param int $id * * @return BaseInfo - * - * @throws \Doctrine\ORM\NoResultException - * @throws \Doctrine\ORM\NonUniqueResultException */ public function get($id = 1) { - $qb = $this->createQueryBuilder('e') - ->where('e.id = :id') - ->setParameter('id', $id); + $BaseInfo = $this->find($id); - if (!$this->kernel->isDebug()) { - $qb->setCacheable(true); + if (null === $BaseInfo) { + throw new \Exception('BaseInfo not found. id = '. $id); } - return $qb->getQuery() - ->useResultCache(true, $this->getCacheLifetime()) - ->getSingleResult(); + return $this->find($id); } } diff --git a/src/Eccube/Repository/CategoryRepository.php b/src/Eccube/Repository/CategoryRepository.php index a42a890f1d0..80bb5024960 100644 --- a/src/Eccube/Repository/CategoryRepository.php +++ b/src/Eccube/Repository/CategoryRepository.php @@ -72,10 +72,6 @@ public function getTotalCount() */ public function getList(Category $Parent = null, $flat = false) { - // TODO::doctrine_cache is not implement - // $options = $this->eccubeConfig['doctrine_cache']; - // $lifetime = $options['result_cache']['lifetime']; - $qb = $this->createQueryBuilder('c1') ->select('c1, c2, c3, c4, c5') ->leftJoin('c1.Children', 'c2') @@ -94,7 +90,7 @@ public function getList(Category $Parent = null, $flat = false) $qb->where('c1.Parent IS NULL'); } $Categories = $qb->getQuery() - // ->useResultCache(true, $lifetime) TODO::doctrine_cache is not implement + ->useResultCache(true, $this->getCacheLifetime()) ->getResult(); if ($flat) { diff --git a/src/Eccube/Repository/LayoutRepository.php b/src/Eccube/Repository/LayoutRepository.php index 23f42f9dbc9..184bdcb2b19 100644 --- a/src/Eccube/Repository/LayoutRepository.php +++ b/src/Eccube/Repository/LayoutRepository.php @@ -13,8 +13,8 @@ namespace Eccube\Repository; -use Eccube\Entity\Layout; use Doctrine\ORM\NoResultException; +use Eccube\Entity\Layout; use Symfony\Bridge\Doctrine\RegistryInterface; /** @@ -30,18 +30,20 @@ public function __construct(RegistryInterface $registry) parent::__construct($registry, Layout::class); } - public function get($layout_id) + public function get($id) { try { $Layout = $this->createQueryBuilder('l') ->select('l, bp, b') ->leftJoin('l.BlockPositions', 'bp') ->leftJoin('bp.Block', 'b') - ->where('l.id = :layout_id') + ->where('l.id = :id') ->orderBy('bp.block_row', 'ASC') - ->setParameter('layout_id', $layout_id) + ->setParameter('id', $id) ->getQuery() + ->useResultCache(true, $this->getCacheLifetime()) ->getSingleResult(); + } catch (NoResultException $e) { return null; } diff --git a/src/Eccube/Repository/NewsRepository.php b/src/Eccube/Repository/NewsRepository.php index 0efc3b0e03b..61f2fdc2d5c 100644 --- a/src/Eccube/Repository/NewsRepository.php +++ b/src/Eccube/Repository/NewsRepository.php @@ -13,6 +13,8 @@ namespace Eccube\Repository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Criteria; use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; use Eccube\Entity\News; @@ -71,17 +73,19 @@ public function getQueryBuilderAll() } /** - * @return mixed + * @return News[]|ArrayCollection */ public function getList() { - $qb = $this->createQueryBuilder('n'); - $qb->where('n.publish_date <= :date') - ->andWhere('n.visible = TRUE') - ->setParameter('date', new \DateTime()) - ->orderBy('n.publish_date', 'DESC') - ->addOrderBy('n.id', 'DESC'); + // second level cacheを効かせるためfindByで取得 + $Results = $this->findBy(['visible' => true], ['publish_date' => 'DESC', 'id' => 'DESC']); + + // 公開日時前のNewsをフィルター + $criteria = Criteria::create(); + $criteria->where(Criteria::expr()->lte('publish_date', new \DateTime())); + + $News = new ArrayCollection($Results); - return $qb->getQuery()->getResult(); + return $News->matching($criteria); } } diff --git a/src/Eccube/Repository/PageRepository.php b/src/Eccube/Repository/PageRepository.php index 0c0999b8720..2cbe2351d55 100644 --- a/src/Eccube/Repository/PageRepository.php +++ b/src/Eccube/Repository/PageRepository.php @@ -66,6 +66,31 @@ public function __construct(RegistryInterface $registry, EccubeConfig $eccubeCon $this->templateDefaultRealDir = $container->getParameter('eccube_theme_src_dir'); } + /** + * @param $route + * @return Page + */ + public function getPageByRoute($route) + { + $qb = $this->createQueryBuilder('p'); + + try { + $Page = $qb + ->select(['p', 'pl', 'l']) + ->leftJoin('p.PageLayouts', 'pl') + ->leftJoin('pl.Layout', 'l') + ->where('p.url = :url') + ->setParameter('url', $route) + ->getQuery() + ->useResultCache(true, $this->getCacheLifetime()) + ->getSingleResult(); + } catch (\Exception $e) { + return $this->newPage(); + } + + return $Page; + } + /** * @param string $url * diff --git a/src/Eccube/Resource/template/default/Block/cart.twig b/src/Eccube/Resource/template/default/Block/cart.twig index 98b02cca439..21f07936c63 100644 --- a/src/Eccube/Resource/template/default/Block/cart.twig +++ b/src/Eccube/Resource/template/default/Block/cart.twig @@ -8,6 +8,9 @@ http://www.lockon.co.jp/ For the full copyright and license information, please view the LICENSE file that was distributed with this source code. #} +{% set Carts = get_all_carts() %} +{% set totalPrice = get_carts_total_price() %} +{% set totalQuantity = get_carts_total_quantity() %}
diff --git a/src/Eccube/Resource/template/default/Block/header.twig b/src/Eccube/Resource/template/default/Block/header.twig index 9201751a4fb..bbffd3a74ab 100644 --- a/src/Eccube/Resource/template/default/Block/header.twig +++ b/src/Eccube/Resource/template/default/Block/header.twig @@ -14,7 +14,7 @@ file that was distributed with this source code. {{ render(path('block_search_product')) }}
- {{ render(url('block_cart_sp')) }} + {{ include('Block/nav_sp.twig') }}
@@ -22,7 +22,7 @@ file that was distributed with this source code. {{ include('Block/login.twig') }}
- {{ render(url('block_cart')) }} + {{ include('Block/cart.twig') }}
diff --git a/src/Eccube/Service/CartService.php b/src/Eccube/Service/CartService.php index 19375be6718..1d8d19e73cf 100644 --- a/src/Eccube/Service/CartService.php +++ b/src/Eccube/Service/CartService.php @@ -133,7 +133,7 @@ public function __construct( */ public function getCarts($empty_delete = false) { - if (!empty($this->carts)) { + if (null !== $this->carts) { if ($empty_delete) { $cartKeys = []; foreach (array_keys($this->carts) as $index) { @@ -181,6 +181,10 @@ public function getSessionCarts() { $cartKeys = $this->session->get('cart_keys', []); + if (empty($cartKeys)) { + return []; + } + return $this->cartRepository->findBy(['cart_key' => $cartKeys], ['id' => 'DESC']); } diff --git a/src/Eccube/Twig/Extension/CartServiceExtension.php b/src/Eccube/Twig/Extension/CartServiceExtension.php new file mode 100644 index 00000000000..66a790e7a57 --- /dev/null +++ b/src/Eccube/Twig/Extension/CartServiceExtension.php @@ -0,0 +1,65 @@ +cartService = $cartService; + } + + public function getFunctions() + { + return [ + new \Twig_Function('get_cart', [$this, 'get_cart'], ['is_safe' => ['all']]), + new \Twig_Function('get_all_carts', [$this, 'get_all_carts'], ['is_safe' => ['all']]), + new \Twig_Function('get_carts_total_price', [$this, 'get_carts_total_price'], ['is_safe' => ['all']]), + new \Twig_Function('get_carts_total_quantity', [$this, 'get_carts_total_quantity'], ['is_safe' => ['all']]), + ]; + } + + public function get_cart() + { + return $this->cartService->getCart(); + } + + public function get_all_carts() + { + return $this->cartService->getCarts(); + } + + public function get_carts_total_price() + { + $Carts = $this->cartService->getCarts(); + $totalPrice = array_reduce($Carts, function ($total, Cart $Cart) { + $total += $Cart->getTotalPrice(); + + return $total; + }, 0); + + return $totalPrice; + } + + public function get_carts_total_quantity() + { + $Carts = $this->cartService->getCarts(); + $totalQuantity = array_reduce($Carts, function ($total, Cart $Cart) { + $total += $Cart->getTotalQuantity(); + + return $total; + }, 0); + + return $totalQuantity; + } +} \ No newline at end of file diff --git a/src/Eccube/Util/CacheUtil.php b/src/Eccube/Util/CacheUtil.php index 41900b003c0..386f86e1674 100644 --- a/src/Eccube/Util/CacheUtil.php +++ b/src/Eccube/Util/CacheUtil.php @@ -85,6 +85,50 @@ public function forceClearCache(PostResponseEvent $event) return $output->fetch(); } + /** + * Doctrineのキャッシュを削除します. + * APP_ENV=prodの場合のみ実行されます. + * + * @param null $env + * @return string + * @throws \Exception + */ + public function clearDoctrineCache() + { + if ($this->kernel->getEnvironment() !== 'prod') { + return; + } + $console = new Application($this->kernel); + $console->setAutoExit(false); + + $command = [ + 'command' => 'cache:pool:clear', + 'pools' => ['doctrine.app_cache_pool'], + '--no-ansi' => true, + ]; + + $input = new ArrayInput($command); + + $output = new BufferedOutput( + OutputInterface::VERBOSITY_DEBUG, + true + ); + + $console->run($input, $output); + + return $output->fetch(); + } + + /** + * Twigキャッシュを削除します. + */ + public function clearTwigCache() + { + $cacheDir = $this->kernel->getCacheDir().'/twig'; + $fs = new Filesystem(); + $fs->remove($cacheDir); + } + /** * キャッシュを削除する. *