Skip to content

Commit

Permalink
issue #155 - better dependency management in composer.json file
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Jun 4, 2024
1 parent 9321481 commit 312fc0d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
17 changes: 13 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,35 @@
"prefer-stable": true,
"require": {
"php": ">=8.1",
"doctrine/doctrine-bundle": "^2.10.0",
"makinacorpus/query-builder": "^1.6.1",
"psr/log": "^3.0",
"symfony/config": "^6.0|^7.0",
"symfony/console": "^6.0|^7.0",
"symfony/dependency-injection": "^6.0|^7.0",
"symfony/filesystem": "^6.0|^7.0",
"symfony/finder": "^6.0|^7.0",
"symfony/options-resolver": "^6.0|^7.0",
"symfony/password-hasher": "^6.0|^7.0",
"symfony/process": "^6.0|^7.0",
"symfony/yaml": "^6.0|^7.0"
},
"require-dev": {
"doctrine/doctrine-bundle": "^2.10.0",
"doctrine/orm": "^2.15|^3.0",
"friendsofphp/php-cs-fixer": "^3.34",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.4",
"symfony/dependency-injection": "^6.0|^7.0",
"symfony/framework-bundle": "^6.0|^7.0",
"symfony/password-hasher": "^6.0|^7.0",
"symfony/validator": "^6.3|^7.0"
},
"suggest": {
"doctrine/doctrine-bundle": "For autoconfiguration in Symfony project context",
"symfony/console": "In order to use the standalone CLI tool or Symfony console commands",
"symfony/password-hasher": "In order to use the password hash anonymizer"
},
"conflict": {
"symfony/console": "<6.0|>=8.0",
"symfony/password-hasher": "<6.0|>=8.0"
},
"autoload": {
"psr-4": {
"MakinaCorpus\\DbToolsBundle\\" : "src/"
Expand Down
3 changes: 3 additions & 0 deletions src/Anonymization/Anonymizer/Core/PasswordAnonymizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AbstractAnonymizer;
use MakinaCorpus\DbToolsBundle\Attribute\AsAnonymizer;
use MakinaCorpus\DbToolsBundle\Error\MissingDependencyException;
use MakinaCorpus\QueryBuilder\Query\Update;
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;

Expand All @@ -22,6 +23,8 @@ class PasswordAnonymizer extends AbstractAnonymizer
#[\Override]
protected function validateOptions(): void
{
MissingDependencyException::check('symfony/password-hasher', PasswordHasherFactory::class);

$algorithm = $this->options->getString('algorithm', 'auto');
$this->options->getString('password', 'password');

Expand Down
32 changes: 32 additions & 0 deletions src/Error/MissingDependencyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare (strict_types=1);

namespace MakinaCorpus\DbToolsBundle\Error;

class MissingDependencyException extends \RuntimeException
{
/**
* Create new missing dependency exception.
*/
public static function create(string $package, ?string $className = null): self
{
if ($className) {
$message = \sprintf("'%s' dependency is missing in order to use '%s' class, please consider running 'composer require %s'", $package, $className, $package);
} else {
$message = \sprintf("'%s' dependency is missing, please consider running 'composer require %s'", $package, $package);
}

return new self($message);
}

/**
* Check class exists, raise missing dependency exception if not.
*/
public static function check(string $package, string $className): void
{
if (!\class_exists($className)) {
throw self::create($package, $className);
}
}
}

0 comments on commit 312fc0d

Please sign in to comment.