Files
Passkeys/PluginManager.php
2025-10-06 21:45:33 +09:00

186 lines
5.2 KiB
PHP

<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\Passkeys;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Layout;
use Eccube\Entity\Page;
use Eccube\Entity\PageLayout;
use Eccube\Plugin\AbstractPluginManager;
use Plugin\Passkeys\Entity\PasskeysAuthConfig;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
/**
* Class PluginManager.
*/
class PluginManager extends AbstractPluginManager
{
private $pages = [
['plg_customer_passkey_page', 'パスキー認証', 'Passkeys/Resource/template/default/passkey'],
];
/**
* @param array $meta
* @param ContainerInterface $container
*/
public function enable(array $meta, ContainerInterface $container)
{
$em = $container->get('doctrine')->getManager();
$this->createConfig($em);
// twigファイルを追加
$this->copyTwigFiles($container);
// ページ登録
$this->createPages($em);
}
/**
* 設定の登録.
*
* @param EntityManagerInterface $em
*/
protected function createConfig(EntityManagerInterface $em)
{
$PasskeysAuthConfig = $em->find(PasskeysAuthConfig::class, 1);
if ($PasskeysAuthConfig) {
return;
}
// 初期値を保存
$PasskeysAuthConfig = new PasskeysAuthConfig();
$em->persist($PasskeysAuthConfig);
$em->flush();
}
/**
* Twigファイルの登録
*
* @param ContainerInterface $container
*/
protected function copyTwigFiles(ContainerInterface $container)
{
// テンプレートファイルコピー
$templatePath = $container->getParameter('eccube_theme_front_dir')
. '/Passkeys/Resource/template/default';
$fs = new Filesystem();
if ($fs->exists($templatePath)) {
return;
}
$fs->mkdir($templatePath);
$fs->mirror(__DIR__ . '/Resource/template/default', $templatePath);
}
/**
* ページ情報の登録
*
* @param EntityManagerInterface $em
*/
protected function createPages(EntityManagerInterface $em)
{
foreach ($this->pages as $p) {
log_info('pk:createPages:'.$p[0]);
$hasPage = $em->getRepository(Page::class)->count(['url' => $p[0]]) > 0;
if (!$hasPage) {
/** @var Page $Page */
$Page = $em->getRepository(Page::class)->newPage();
$Page->setEditType(Page::EDIT_TYPE_DEFAULT);
$Page->setUrl($p[0]);
$Page->setName($p[1]);
$Page->setFileName($p[2]);
$Page->setMetaRobots('noindex');
$em->persist($Page);
$em->flush();
$Layout = $em->getRepository(Layout::class)->find(Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE);
$PageLayout = new PageLayout();
$PageLayout->setPage($Page)
->setPageId($Page->getId())
->setLayout($Layout)
->setLayoutId($Layout->getId())
->setSortNo(0);
$em->persist($PageLayout);
$em->flush();
}
}
}
/**
* @param array $meta
* @param ContainerInterface $container
*/
public function disable(array $meta, ContainerInterface $container)
{
$em = $container->get('doctrine')->getManager();
// twigファイルを削除
$this->removeTwigFiles($container);
// ページ削除
$this->removePages($em);
}
/**
* Twigファイルの削除
*
* @param ContainerInterface $container
*/
protected function removeTwigFiles(ContainerInterface $container)
{
$templatePath = $container->getParameter('eccube_theme_front_dir')
. '/Passkeys';
$fs = new Filesystem();
$fs->remove($templatePath);
}
/**
* ページ情報の削除
*
* @param EntityManagerInterface $em
*/
protected function removePages(EntityManagerInterface $em)
{
foreach ($this->pages as $p) {
$Page = $em->getRepository(Page::class)->findOneBy(['url' => $p[0]]);
if ($Page !== null) {
$Layout = $em->getRepository(Layout::class)->find(Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE);
$PageLayout = $em->getRepository(PageLayout::class)->findOneBy(['Page' => $Page, 'Layout' => $Layout]);
$em->remove($PageLayout);
$em->remove($Page);
$em->flush();
}
}
}
/**
* @param array $meta
* @param ContainerInterface $container
*/
public function uninstall(array $meta, ContainerInterface $container)
{
$em = $container->get('doctrine')->getManager();
// twigファイルを削除
$this->removeTwigFiles($container);
// ページ削除
$this->removePages($em);
}
}