-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from wmde/doctrine-update-2024
Update Doctrine ORM and DBAL
- Loading branch information
Showing
22 changed files
with
245 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
use Doctrine\DBAL\DriverManager; | ||
use Doctrine\DBAL\Tools\DsnParser; | ||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\ORMSetup; | ||
use Doctrine\ORM\Tools\Console\ConsoleRunner; | ||
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
use WMDE\Fundraising\AddressChangeContext\AddressChangeContextFactory; | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|
||
$dotenv = new Dotenv(); | ||
$dotenv->load( __DIR__ . '/../.env' ); | ||
|
||
function createEntityManager(): EntityManager { | ||
if (empty( $_ENV['DB_DSN'] ) ) { | ||
echo "You must set the database connection string in 'DB_DSN'\n"; | ||
exit(1); | ||
} | ||
$dsnParser = new DsnParser(['mysql' => 'pdo_mysql']); | ||
$connectionParams = $dsnParser | ||
->parse( $_ENV['DB_DSN'] ); | ||
$connection = DriverManager::getConnection( $connectionParams ); | ||
|
||
$contextFactory = new AddressChangeContextFactory(); | ||
$contextFactory->registerCustomTypes( $connection ); | ||
$doctrineConfig = ORMSetup::createXMLMetadataConfiguration( | ||
$contextFactory->getDoctrineMappingPaths(), | ||
true | ||
); | ||
|
||
return new EntityManager( $connection, $doctrineConfig ); | ||
} | ||
|
||
|
||
ConsoleRunner::run( | ||
new SingleManagerProvider(createEntityManager()), | ||
[] | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace WMDE\Fundraising\AddressChangeContext\DataAccess\DoctrineTypes; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\Type; | ||
use WMDE\Fundraising\AddressChangeContext\Domain\Model\AddressType as DomainAddressType; | ||
|
||
class AddressType extends Type { | ||
public function getSQLDeclaration( array $column, AbstractPlatform $platform ): string { | ||
return 'VARCHAR(10)'; | ||
} | ||
|
||
public function convertToPHPValue( mixed $value, AbstractPlatform $platform ): DomainAddressType { | ||
return match ( $value ) { | ||
'person' => DomainAddressType::Person, | ||
'company' => DomainAddressType::Company, | ||
default => throw new \InvalidArgumentException( | ||
"Could not convert address type string ({$value}) to enum" | ||
), | ||
}; | ||
} | ||
|
||
public function convertToDatabaseValue( mixed $value, AbstractPlatform $platform ): string { | ||
return match ( $value ) { | ||
DomainAddressType::Person => 'person', | ||
DomainAddressType::Company => 'company', | ||
default => throw new \InvalidArgumentException( | ||
"Could not convert address type enum ({$value}) to string" | ||
), | ||
}; | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
* @return string | ||
*/ | ||
public function getName(): string { | ||
return 'AddressType'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
declare( strict_types=1 ); | ||
|
||
namespace WMDE\Fundraising\AddressChangeContext\Domain\Model; | ||
|
||
enum AddressType { | ||
case Person; | ||
case Company; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
declare( strict_types=1 ); | ||
|
||
namespace WMDE\Fundraising\AddressChangeContext; | ||
|
||
/** | ||
* This class converts "mixed" values from libraries like Doctrine or Symfony into scalar values, without tripping up | ||
* PHPStan, which disallows calling "strval" and "intval" on variables with "mixed" type, because calling them | ||
* with objects or arrays will generate a warning. | ||
* | ||
* DO NOT USE THIS IN LOOPS! (E.g. iterating over database results). | ||
* The constant type checking will slow down the application, use PHPStan-specific comments to ignore this error instead: | ||
* https://phpstan.org/user-guide/ignoring-errors | ||
* | ||
* Hopefully, in the future libraries will return fewer "mixed" types. Please check from time to time if this class is still needed. | ||
* Check the usage of the class methods to detect libraries that return mixed. | ||
*/ | ||
class ScalarTypeConverter { | ||
public static function toInt( mixed $value ): int { | ||
return intval( self::assertScalarType( $value ) ); | ||
} | ||
|
||
public static function toString( mixed $value ): string { | ||
return strval( self::assertScalarType( $value ) ); | ||
} | ||
|
||
private static function assertScalarType( mixed $value ): int|string|bool|float { | ||
if ( is_scalar( $value ) ) { | ||
return $value; | ||
} | ||
throw new \InvalidArgumentException( "Given value is not a scalar type" ); | ||
} | ||
} |
Oops, something went wrong.