Skip to content

Commit

Permalink
feat(chore): refactor code to prevent breaking changes in ORM3 while …
Browse files Browse the repository at this point in the history
…maintaining ORM2 compatibility
  • Loading branch information
Zaruike committed Dec 5, 2024
1 parent 17fd413 commit 83a7265
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Entity/LibrariesHubCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity()]
#[ORM\Entity]
#[ORM\Table('h5p_libraries_hub_cache')]
class LibrariesHubCache
{
Expand Down
14 changes: 12 additions & 2 deletions Entity/LibrariesLanguagesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\NoResultException;
use Doctrine\Persistence\ManagerRegistry;
use Studit\H5PBundle\Service\DoctrineParser;

/**
* LibrariesLanguagesRepository
Expand All @@ -22,7 +23,12 @@ public function findForLibrary($machineName, $majorVersion, $minorVersion, $lang
->select('ll.languageJson')
->join('ll.library', 'l', 'WITH', 'l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion')
->where('ll.languageCode = :languageCode')
->setParameters(['majorVersion' => $majorVersion, 'machineName' => $machineName, 'minorVersion' => $minorVersion, 'languageCode' => $languageCode]);
->setParameters(DoctrineParser::buildParams([
'majorVersion' => $majorVersion,
'machineName' => $machineName,
'minorVersion' => $minorVersion,
'languageCode' => $languageCode
]));
try {
$result = $qb->getQuery()->getSingleResult();
} catch (NoResultException $e) {
Expand All @@ -35,7 +41,11 @@ public function findForLibraryAllLanguages($machineName, $majorVersion, $minorVe
$qb = $this->createQueryBuilder('ll')
->select('ll.languageCode')
->join('ll.library', 'l', 'WITH', 'l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion')
->setParameters(['majorVersion' => $majorVersion, 'machineName' => $machineName, 'minorVersion' => $minorVersion]);
->setParameters(DoctrineParser::buildParams([
'majorVersion' => $majorVersion,
'machineName' => $machineName,
'minorVersion' => $minorVersion
]));
try {
$results = $qb->getQuery()->getArrayResult();
} catch (NoResultException $e) {
Expand Down
2 changes: 1 addition & 1 deletion Entity/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Library

#[ORM\OneToMany(targetEntity: ContentLibraries::class, mappedBy: "library")]
/**
* @var ArrayCollection|Collection
* @var ArrayCollection|Collection $contentLibraries
*/
private ArrayCollection|Collection $contentLibraries;
/**
Expand Down
15 changes: 8 additions & 7 deletions Entity/LibraryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\NoResultException;
use Doctrine\Persistence\ManagerRegistry;
use Studit\H5PBundle\Service\DoctrineParser;

/**
* LibraryRepository
Expand Down Expand Up @@ -95,11 +96,11 @@ public function findHasSemantics($machineName, $majorVersion, $minorVersion)
$qb = $this->createQueryBuilder('l')
->select('l')
->where('l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion and l.semantics is not null')
->setParameters([
->setParameters(DoctrineParser::buildParams([
'machineName' => $machineName,
'majorVersion' => $majorVersion,
'minorVersion' => $minorVersion
]);
]));
try {
$library = $qb->getQuery()->getSingleResult();
} catch (NoResultException $e) {
Expand All @@ -123,19 +124,19 @@ public function findOneArrayBy($parameters)
{
$qb = $this->createQueryBuilder('l')
->where('l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion')
->setParameters($parameters);
->setParameters(DoctrineParser::buildParams($parameters));
return $qb->getQuery()->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY);
}
public function findIdBy($machineName, $majorVersion, $minorVersion)
{
$qb = $this->createQueryBuilder('l')
->select('l.id')
->where('l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion and l.semantics is not null')
->setParameters([
->setParameters(DoctrineParser::buildParams([
'machineName' => $machineName,
'majorVersion' => $majorVersion,
'minorVersion' => $minorVersion
]);
]));
try {
return $qb->getQuery()->getSingleScalarResult();
} catch (NoResultException $e) {
Expand All @@ -147,12 +148,12 @@ public function isPatched($library): bool
$qb = $this->createQueryBuilder('l')
->select('COUNT(l)')
->where('l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion and l.patchVersion < :patchVersion')
->setParameters([
->setParameters(DoctrineParser::buildParams([
'machineName' => $library['machineName'],
'majorVersion' => $library['majorVersion'],
'minorVersion' => $library['minorVersion'],
'patchVersion' => $library['patchVersion']
]);
]));
return $qb->getQuery()->getSingleScalarResult() > 0;
}
}
38 changes: 38 additions & 0 deletions Service/DoctrineParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Studit\H5PBundle\Service;

use Composer\InstalledVersions;
use Doctrine\Common\Collections\ArrayCollection;

/**
* This class exists to prevent breaking changes when working with different versions of Doctrine ORM.
* For example, in Doctrine ORM v3.x, certain parameters require an ArrayCollection.
* @author Joris Dugué
*/
class DoctrineParser
{
/**
* This method converts parameters to an ArrayCollection for ORM v3.
* If using ORM v2, it simply returns the received parameters as is.
*
* @param array $params The input parameters to process.
* @return ArrayCollection|array Returns an ArrayCollection for ORM v3 or the original parameters for ORM v2.
*/
public static function buildParams(array $params): ArrayCollection|array
{
$doctrineVersion = InstalledVersions::getVersion('doctrine/orm');
if ($doctrineVersion !== null && str_starts_with($doctrineVersion, '3')) {
// For Doctrine ORM v3, ensure the parameters are returned as an ArrayCollection
$paramsCollection = [];

foreach ($params as $k => $val){
$paramsCollection[] = new \Doctrine\ORM\Query\Parameter($k, $val);
}

return new ArrayCollection($paramsCollection);
}
// For Doctrine ORM v2, return the parameters as is
return $params;
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jorisdugue/h5p-bundle",
"version": "3.0.1",
"version": "3.0.2",
"type": "symfony-bundle",
"description": "H5P Bundle for Symfony 5, 6 and Symfony 7",
"keywords": [
Expand Down Expand Up @@ -29,6 +29,7 @@
],
"require": {
"php": ">= 8.1",
"composer-runtime-api": "^2",
"doctrine/orm": "~2.0|~3.0",
"guzzlehttp/guzzle": "^7.9",
"h5p/h5p-core": "1.27",
Expand Down

0 comments on commit 83a7265

Please sign in to comment.