Skip to content

Commit

Permalink
Add compatibility with Haste 5 (#37)
Browse files Browse the repository at this point in the history
* Update to Haste v5 and PHP 8

* Fix the plugin class

* Fix the services definition

* Fix the services definition

* Fix the unit tests

* Use stable version of Haste
  • Loading branch information
qzminski authored Oct 26, 2022
1 parent 6a02ae3 commit 42c0a16
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 34 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
}
],
"require": {
"php": "^7.1 || ^8.0",
"php": "^8.0",
"ext-pdo": "*",
"contao/core-bundle": "^4.13 || ^5.0",
"codefog/contao-haste": "^4.24",
"codefog/contao-haste": "^5.0",
"doctrine/dbal": "^2.12 || ^3.0"
},
"require-dev": {
Expand Down
3 changes: 2 additions & 1 deletion src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Codefog\TagsBundle\ContaoManager;

use Codefog\HasteBundle\CodefogHasteBundle;
use Codefog\TagsBundle\CodefogTagsBundle;
use Contao\CoreBundle\ContaoCoreBundle;
use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
Expand All @@ -26,7 +27,7 @@ class Plugin implements BundlePluginInterface
public function getBundles(ParserInterface $parser)
{
return [
BundleConfig::create(CodefogTagsBundle::class)->setLoadAfter([ContaoCoreBundle::class, 'haste']),
BundleConfig::create(CodefogTagsBundle::class)->setLoadAfter([ContaoCoreBundle::class, CodefogHasteBundle::class]),
];
}
}
17 changes: 8 additions & 9 deletions src/EventListener/TagManagerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Codefog\TagsBundle\Manager\DcaAwareInterface;
use Codefog\TagsBundle\ManagerRegistry;
use Contao\DataContainer;
use Haste\Util\Debug;

class TagManagerListener
{
Expand Down Expand Up @@ -111,21 +110,21 @@ private function getManagerFromDca(DataContainer $dc): ?DcaAwareInterface
*/
private function addAssets(): void
{
$GLOBALS['TL_CSS'][] = Debug::uncompressedFile('bundles/codefogtags/selectize.min.css');
$GLOBALS['TL_CSS'][] = Debug::uncompressedFile('bundles/codefogtags/backend.min.css');
$GLOBALS['TL_CSS'][] = 'bundles/codefogtags/selectize.min.css';
$GLOBALS['TL_CSS'][] = 'bundles/codefogtags/backend.min.css';

// Add the jQuery
if (!isset($GLOBALS['TL_JAVASCRIPT']) || !preg_grep("/^assets\/jquery\/js\/jquery(\.min)?\.js$/", $GLOBALS['TL_JAVASCRIPT'])) {
$GLOBALS['TL_JAVASCRIPT'][] = Debug::uncompressedFile('assets/jquery/js/jquery.min.js');
$GLOBALS['TL_JAVASCRIPT'][] = 'assets/jquery/js/jquery.min.js';
}

// Add jQuery UI to make the widget sortable if needed
// @see https://jqueryui.com/download/#!version=1.12.1&themeParams=none&components=101000000100000010000000010000000000000000000000
$GLOBALS['TL_CSS'][] = Debug::uncompressedFile('bundles/codefogtags/jquery-ui.min.css');
$GLOBALS['TL_JAVASCRIPT'][] = Debug::uncompressedFile('bundles/codefogtags/jquery-ui.min.js');
$GLOBALS['TL_CSS'][] = 'bundles/codefogtags/jquery-ui.min.css';
$GLOBALS['TL_JAVASCRIPT'][] = 'bundles/codefogtags/jquery-ui.min.js';

$GLOBALS['TL_JAVASCRIPT'][] = Debug::uncompressedFile('bundles/codefogtags/selectize.min.js');
$GLOBALS['TL_JAVASCRIPT'][] = Debug::uncompressedFile('bundles/codefogtags/widget.min.js');
$GLOBALS['TL_JAVASCRIPT'][] = Debug::uncompressedFile('bundles/codefogtags/backend.min.js');
$GLOBALS['TL_JAVASCRIPT'][] = 'bundles/codefogtags/selectize.min.js';
$GLOBALS['TL_JAVASCRIPT'][] = 'bundles/codefogtags/widget.min.js';
$GLOBALS['TL_JAVASCRIPT'][] = 'bundles/codefogtags/backend.min.js';
}
}
26 changes: 10 additions & 16 deletions src/Finder/SourceFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,17 @@

namespace Codefog\TagsBundle\Finder;

use Codefog\HasteBundle\DcaRelationsManager;
use Codefog\HasteBundle\Model\DcaRelationsModel;
use Doctrine\DBAL\Connection;
use Haste\Model\Model;
use Haste\Model\Relations;

class SourceFinder
{
/**
* @var Connection
*/
private $db;

/**
* DefaultFinder constructor.
*/
public function __construct(Connection $db)
public function __construct(
private Connection $connection,
private DcaRelationsManager $dcaRelationsManager,
)
{
$this->db = $db;
}

/**
Expand All @@ -51,7 +45,7 @@ public function findMultiple(SourceCriteria $criteria): array
$ids[] = $tag->getValue();
}

$values = Model::getReferenceValues($criteria->getSourceTable(), $criteria->getSourceField(), $ids);
$values = DcaRelationsModel::getReferenceValues($criteria->getSourceTable(), $criteria->getSourceField(), $ids);
$values = array_values(array_unique($values));

return array_map('intval', $values);
Expand All @@ -68,11 +62,11 @@ public function findRelatedSourceRecords(SourceCriteria $criteria, int $limit =
throw new \RuntimeException('No IDs have been provided');
}

if (false === ($relation = Relations::getRelation($criteria->getSourceTable(), $criteria->getSourceField()))) {
if (false === ($relation = $this->dcaRelationsManager->getRelation($criteria->getSourceTable(), $criteria->getSourceField()))) {
throw new \RuntimeException(sprintf('The field %s.%s is not related', $criteria->getSourceTable(), $criteria->getSourceField()));
}

$tagIds = Model::getRelatedValues($criteria->getSourceTable(), $criteria->getSourceField(), $criteria->getIds());
$tagIds = DcaRelationsModel::getRelatedValues($criteria->getSourceTable(), $criteria->getSourceField(), $criteria->getIds());
$tagIds = array_values(array_unique($tagIds));
$tagIds = array_map('intval', $tagIds);

Expand All @@ -97,7 +91,7 @@ public function findRelatedSourceRecords(SourceCriteria $criteria, int $limit =
}

$related = [];
$records = $this->db->fetchAllAssociative($query);
$records = $this->connection->fetchAllAssociative($query);

// Generate the related records
foreach ($records as $record) {
Expand Down
8 changes: 4 additions & 4 deletions src/Finder/TagFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

namespace Codefog\TagsBundle\Finder;

use Codefog\HasteBundle\Model\DcaRelationsModel;
use Codefog\TagsBundle\Exception\NoTagsException;
use Codefog\TagsBundle\Model\TagModel;
use Codefog\TagsBundle\Tag;
use Haste\Model\Model;

class TagFinder
{
Expand Down Expand Up @@ -114,7 +114,7 @@ public function getTopTags(TagCriteria $criteria, int $limit = null, bool $withC
public function getTopTagIds(TagCriteria $criteria, int $limit = null, bool $withCount = false): array
{
// No array_unique() here!
$tagIds = Model::getRelatedValues($criteria->getSourceTable(), $criteria->getSourceField(), $criteria->getSourceIds());
$tagIds = DcaRelationsModel::getRelatedValues($criteria->getSourceTable(), $criteria->getSourceField(), $criteria->getSourceIds());
$tagIds = array_map('intval', $tagIds);

if (0 === \count($tagIds)) {
Expand Down Expand Up @@ -184,7 +184,7 @@ protected function parseCriteria(TagCriteria $criteria): array

// Find by source IDs
if (\count($sourceIds = $criteria->getSourceIds()) > 0) {
$ids = Model::getRelatedValues($criteria->getSourceTable(), $criteria->getSourceField(), $sourceIds);
$ids = DcaRelationsModel::getRelatedValues($criteria->getSourceTable(), $criteria->getSourceField(), $sourceIds);
$ids = array_values(array_unique($ids));
$ids = array_map('intval', $ids);

Expand All @@ -200,7 +200,7 @@ protected function parseCriteria(TagCriteria $criteria): array

// Find only the used tags
if ($criteria->isUsedOnly()) {
$ids = Model::getRelatedValues($criteria->getSourceTable(), $criteria->getSourceField());
$ids = DcaRelationsModel::getRelatedValues($criteria->getSourceTable(), $criteria->getSourceField());
$ids = array_values(array_unique($ids));
$ids = array_map('intval', $ids);

Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ services:
codefog_tags.source_finder:
class: Codefog\TagsBundle\Finder\SourceFinder
arguments:
- "@database_connection"
- '@database_connection'
- '@Codefog\HasteBundle\DcaRelationsManager'

codefog_tags.manager_registry:
class: Codefog\TagsBundle\ManagerRegistry
Expand Down
3 changes: 2 additions & 1 deletion tests/ContaoManager/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Codefog\TagsBundle\Test\ContaoManager;

use Codefog\HasteBundle\CodefogHasteBundle;
use Codefog\TagsBundle\CodefogTagsBundle;
use Codefog\TagsBundle\ContaoManager\Plugin;
use Contao\CoreBundle\ContaoCoreBundle;
Expand All @@ -22,6 +23,6 @@ public function testGetBundles()
$this->assertCount(1, $bundles);
$this->assertInstanceOf(BundleConfig::class, $config);
$this->assertEquals(CodefogTagsBundle::class, $config->getName());
$this->assertEquals([ContaoCoreBundle::class, 'haste'], $config->getLoadAfter());
$this->assertEquals([ContaoCoreBundle::class, CodefogHasteBundle::class], $config->getLoadAfter());
}
}

0 comments on commit 42c0a16

Please sign in to comment.