Skip to content
This repository has been archived by the owner on Aug 18, 2022. It is now read-only.

Commit

Permalink
update Hooks to Symfony 5 (#4593)
Browse files Browse the repository at this point in the history
update Hooks to Symfony 5
  • Loading branch information
craigh authored Feb 24, 2021
1 parent 46d4212 commit ec4b0a5
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 0 deletions.
99 changes: 99 additions & 0 deletions Bridge/HookBundle/HookBundleInstaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\Bundle\CoreInstallerBundle\Bridge\HookBundle;

use Doctrine\ORM\EntityManagerInterface;
use Zikula\Bundle\CoreBundle\Doctrine\Helper\SchemaHelper;
use Zikula\Bundle\HookBundle\Entity\Connection;
use Zikula\Bundle\HookBundle\Entity\HookBindingEntity;
use Zikula\Bundle\HookBundle\Entity\HookRuntimeEntity;
use Zikula\ExtensionsModule\Installer\InstallerInterface;

class HookBundleInstaller implements InstallerInterface
{
/**
* @var SchemaHelper
*/
private $schemaTool;

/**
* @var EntityManagerInterface
*/
private $em;

private static $entities = [
HookBindingEntity::class, // @deprecated remove this line at Core 4.0.0
HookRuntimeEntity::class, // @deprecated remove this line at Core 4.0.0
Connection::class
];

public function __construct(
SchemaHelper $schemaTool,
EntityManagerInterface $entityManager
) {
$this->schemaTool = $schemaTool;
$this->em = $entityManager;
}

public function install(): bool
{
$this->schemaTool->create(self::$entities);

return true;
}

public function uninstall(): bool
{
return false;
}

public function upgrade(string $currentCoreVersion): bool
{
// special note, the $currentCoreVersion var will contain the version of the CORE (not this bundle)

if (version_compare($currentCoreVersion, '2.0.0', '<')) {
// remove all old hook-related tables
$oldTables = [
'hook_area',
'hook_provider',
'hook_subscriber',
'hook_binding',
'hook_runtime'
];
foreach ($oldTables as $table) {
$sql = "DROP TABLE ${table};";
$connection = $this->em->getConnection();
$stmt = $connection->prepare($sql);
$stmt->execute();
$stmt->closeCursor();
}
$this->schemaTool->create(self::$entities); // create new versions of the tables for Core-2.0.0
}
switch ($currentCoreVersion) {
case '2.0.0':
$this->schemaTool->update([
HookRuntimeEntity::class
]);
// no break
case '2.0.1':
// nothing
case '3.1.0'://current version
// allow old tables to remain on upgrade, just add the new one
$this->schemaTool->create(self::$entities);
}

// Update successful
return true;
}
}
57 changes: 57 additions & 0 deletions Command/SchemaToolInstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\Bundle\CoreInstallerBundle\Command;

use InvalidArgumentException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Zikula\Bundle\CoreBundle\Doctrine\Helper\SchemaHelper;

class SchemaToolInstallCommand extends Command
{
protected static $defaultName = 'zikula:schema:create';

/**
* @var SchemaHelper
*/
private $schemaHelper;

public function __construct(SchemaHelper $schemaHelper)
{
parent::__construct();
$this->schemaHelper = $schemaHelper;
}

protected function configure()
{
$this
->setDefinition([
new InputArgument('entityClass', InputArgument::REQUIRED, 'The FqCN of the Entity Class'),
])
->setDescription('Create a new table in the DB for the provided entity class.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$entityClass = $input->getArgument('entityClass');
if (!class_exists($entityClass)) {
throw new InvalidArgumentException('The entity class does not exist.');
}
$this->schemaHelper->create([$entityClass]);

return Command::SUCCESS;
}
}
51 changes: 51 additions & 0 deletions EventListener/HookBundleInstallerListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\Bundle\CoreInstallerBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Zikula\Bundle\CoreInstallerBundle\Bridge\HookBundle\HookBundleInstaller;
use Zikula\Bundle\CoreInstallerBundle\Event\CoreInstallationPreExtensionInstallation;
use Zikula\Bundle\CoreInstallerBundle\Event\CoreUpgradePreExtensionUpgrade;

class HookBundleInstallerListener implements EventSubscriberInterface
{
private $hookBundleInstaller;

public function __construct(HookBundleInstaller $hookBundleInstaller)
{
$this->hookBundleInstaller = $hookBundleInstaller;
}

public static function getSubscribedEvents()
{
return [
CoreInstallationPreExtensionInstallation::class => 'installHookBundle',
CoreUpgradePreExtensionUpgrade::class => 'upgradeHookBundle'
];
}

public function installHookBundle(CoreInstallationPreExtensionInstallation $event): void
{
if (!$this->hookBundleInstaller->install()) {
$event->stopPropagation();
}
}

public function upgradeHookBundle(CoreUpgradePreExtensionUpgrade $event): void
{
if (!$this->hookBundleInstaller->upgrade($event->getCurrentCoreVersion())) {
$event->stopPropagation();
}
}
}

0 comments on commit ec4b0a5

Please sign in to comment.