From 09e631c7cacd12e04b1bde6b991dedadfff4d747 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Thu, 7 Mar 2024 18:01:22 +0100 Subject: [PATCH 01/16] Rename pack bundle route alias --- Util/FacebookUtil.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Util/FacebookUtil.php b/Util/FacebookUtil.php index 8e5377f..91664d1 100644 --- a/Util/FacebookUtil.php +++ b/Util/FacebookUtil.php @@ -122,7 +122,7 @@ class FacebookUtil { //Return image data return [ - 'og:image' => $this->router->generate('rapsys_pack_facebook', ['mtime' => $mtime, 'path' => $pathInfo], UrlGeneratorInterface::ABSOLUTE_URL), + 'og:image' => $this->router->generate('rapsyspack_facebook', ['mtime' => $mtime, 'path' => $pathInfo], UrlGeneratorInterface::ABSOLUTE_URL), 'og:image:alt' => str_replace("\n", ' ', implode(' - ', array_keys($texts))), 'og:image:height' => $height, 'og:image:width' => $width @@ -348,7 +348,7 @@ class FacebookUtil { //Return image data return [ - 'og:image' => $this->router->generate('rapsys_pack_facebook', ['mtime' => stat($path)['mtime'], 'path' => $pathInfo], UrlGeneratorInterface::ABSOLUTE_URL), + 'og:image' => $this->router->generate('rapsyspack_facebook', ['mtime' => stat($path)['mtime'], 'path' => $pathInfo], UrlGeneratorInterface::ABSOLUTE_URL), 'og:image:alt' => str_replace("\n", ' ', implode(' - ', array_keys($texts))), 'og:image:height' => $height, 'og:image:width' => $width -- 2.41.0 From cc594246ad5d0bef91ff3b83ce9de7206acd61ec Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Thu, 7 Mar 2024 18:02:39 +0100 Subject: [PATCH 02/16] Rename pack bundle route alias --- Util/ImageUtil.php | 6 +++--- Util/MapUtil.php | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Util/ImageUtil.php b/Util/ImageUtil.php index b6646b2..871b99a 100644 --- a/Util/ImageUtil.php +++ b/Util/ImageUtil.php @@ -108,7 +108,7 @@ class ImageUtil { 'token' => $this->slugger->hash(strval($a * $b + $c)), 'value' => strval($a * $b + $c), 'equation' => str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation), - 'src' => $this->router->generate('rapsys_pack_captcha', ['hash' => $hash, 'updated' => $updated, 'equation' => $short, 'width' => $width, 'height' => $height]), + 'src' => $this->router->generate('rapsyspack_captcha', ['hash' => $hash, 'updated' => $updated, 'equation' => $short, 'width' => $width, 'height' => $height]), 'width' => $width, 'height' => $height ]; @@ -140,8 +140,8 @@ class ImageUtil { //Return array return [ 'caption' => $caption, - 'link' => $this->router->generate('rapsys_pack_thumb', ['hash' => $link, 'updated' => $updated, 'path' => $short, 'width' => $imageWidth, 'height' => $imageHeight]), - 'src' => $this->router->generate('rapsys_pack_thumb', ['hash' => $src, 'updated' => $updated, 'path' => $short, 'width' => $width, 'height' => $height]), + 'link' => $this->router->generate('rapsyspack_thumb', ['hash' => $link, 'updated' => $updated, 'path' => $short, 'width' => $imageWidth, 'height' => $imageHeight]), + 'src' => $this->router->generate('rapsyspack_thumb', ['hash' => $src, 'updated' => $updated, 'path' => $short, 'width' => $width, 'height' => $height]), 'width' => $width, 'height' => $height ]; diff --git a/Util/MapUtil.php b/Util/MapUtil.php index 407a649..b7d2232 100644 --- a/Util/MapUtil.php +++ b/Util/MapUtil.php @@ -209,8 +209,8 @@ class MapUtil { //Return array return [ 'caption' => $caption, - 'link' => $this->router->generate('rapsys_pack_map', ['hash' => $link, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'zoom' => $zoom + 1, 'width' => $width * 2, 'height' => $height * 2]), - 'src' => $this->router->generate('rapsys_pack_map', ['hash' => $src, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'zoom' => $zoom, 'width' => $width, 'height' => $height]), + 'link' => $this->router->generate('rapsyspack_map', ['hash' => $link, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'zoom' => $zoom + 1, 'width' => $width * 2, 'height' => $height * 2]), + 'src' => $this->router->generate('rapsyspack_map', ['hash' => $src, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'zoom' => $zoom, 'width' => $width, 'height' => $height]), 'width' => $width, 'height' => $height ]; @@ -263,8 +263,8 @@ class MapUtil { //Return array return [ 'caption' => $caption, - 'link' => $this->router->generate('rapsys_pack_multimap', ['hash' => $link, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'coordinates' => $coordinate, 'zoom' => $zoom + 1, 'width' => $width * 2, 'height' => $height * 2]), - 'src' => $this->router->generate('rapsys_pack_multimap', ['hash' => $src, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'coordinates' => $coordinate, 'zoom' => $zoom, 'width' => $width, 'height' => $height]), + 'link' => $this->router->generate('rapsyspack_multimap', ['hash' => $link, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'coordinates' => $coordinate, 'zoom' => $zoom + 1, 'width' => $width * 2, 'height' => $height * 2]), + 'src' => $this->router->generate('rapsyspack_multimap', ['hash' => $src, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'coordinates' => $coordinate, 'zoom' => $zoom, 'width' => $width, 'height' => $height]), 'width' => $width, 'height' => $height ]; -- 2.41.0 From ca42f395bd7fadae6aa2db553d88a5b9385238f6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Thu, 7 Mar 2024 18:03:13 +0100 Subject: [PATCH 03/16] Cleanup --- Util/IntlUtil.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Util/IntlUtil.php b/Util/IntlUtil.php index 9f897e1..eb7711f 100644 --- a/Util/IntlUtil.php +++ b/Util/IntlUtil.php @@ -22,6 +22,7 @@ class IntlUtil { * Format date */ public function date(Environment $env, \DateTime $date, string $dateFormat = 'medium', string $timeFormat = 'medium', ?string $locale = null, \IntlTimeZone|\DateTimeZone|string|null $timezone = null, ?string $calendar = null, ?string $pattern = null) { + //Get converted date $date = twig_date_converter($env, $date, $timezone); //Set date and time formatters @@ -33,6 +34,7 @@ class IntlUtil { 'full' => \IntlDateFormatter::FULL, ]; + //Get formatter $formatter = \IntlDateFormatter::create( $locale, $formatters[$dateFormat], @@ -42,6 +44,7 @@ class IntlUtil { $pattern ); + //Return formatted date return $formatter->format($date->getTimestamp()); } @@ -49,29 +52,35 @@ class IntlUtil { * Format number */ public function number(int|float $number, $style = 'decimal', $type = 'default', ?string $locale = null) { - static $typeValues = array( + //Set types + static $types = [ 'default' => NumberFormatter::TYPE_DEFAULT, 'int32' => NumberFormatter::TYPE_INT32, 'int64' => NumberFormatter::TYPE_INT64, 'double' => NumberFormatter::TYPE_DOUBLE, 'currency' => NumberFormatter::TYPE_CURRENCY, - ); + ]; + //Get formatter $formatter = $this->getNumberFormatter($locale, $style); - if (!isset($typeValues[$type])) { - throw new SyntaxError(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($typeValues)))); + //Without type + if (!isset($types[$type])) { + throw new SyntaxError(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($types)))); } - return $formatter->format($number, $typeValues[$type]); + //Return formatted number + return $formatter->format($number, $types[$type]); } /** * Format currency */ public function currency(int|float $number, string $currency, ?string $locale = null) { + //Get formatter $formatter = $this->getNumberFormatter($locale, 'currency'); + //Return formatted currency return $formatter->formatCurrency($number, $currency); } -- 2.41.0 From 177b09c72785e869d93602ac47524a31dd64a94b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Thu, 7 Mar 2024 18:51:57 +0100 Subject: [PATCH 04/16] Cleanup --- Command.php | 4 ++-- Command/RangeCommand.php | 8 ++++---- DependencyInjection/Configuration.php | 4 ++-- Extension/PackExtension.php | 4 ++-- Form/CaptchaType.php | 4 ++-- Package/PathPackage.php | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Command.php b/Command.php index 0832e2e..51fbb30 100644 --- a/Command.php +++ b/Command.php @@ -44,9 +44,9 @@ class Command extends BaseCommand { } /** - * Return the command name - * * {@inheritdoc} + * + * Return the command name */ public function getName(): string { //With namespace diff --git a/Command/RangeCommand.php b/Command/RangeCommand.php index f720493..65b4fe4 100644 --- a/Command/RangeCommand.php +++ b/Command/RangeCommand.php @@ -18,9 +18,9 @@ use Symfony\Component\Console\Output\OutputInterface; use Rapsys\PackBundle\Command; /** - * Shuffle printable character range - * * {@inheritdoc} + * + * Shuffle printable character range */ class RangeCommand extends Command { /** @@ -49,9 +49,9 @@ class RangeCommand extends Command { } /** - * Output a shuffled printable characters range - * * {@inheritdoc} + * + * Output a shuffled printable characters range */ protected function execute(InputInterface $input, OutputInterface $output): int { //Printable character range diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 5764724..95326bc 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -19,11 +19,11 @@ use Symfony\Component\Process\ExecutableFinder; use Rapsys\PackBundle\RapsysPackBundle; /** + * {@inheritdoc} + * * This is the class that validates and merges configuration from your app/config files. * * @link http://symfony.com/doc/current/cookbook/bundles/configuration.html - * - * {@inheritdoc} */ class Configuration implements ConfigurationInterface { /** diff --git a/Extension/PackExtension.php b/Extension/PackExtension.php index 97e3467..1234de5 100644 --- a/Extension/PackExtension.php +++ b/Extension/PackExtension.php @@ -26,9 +26,9 @@ use Rapsys\PackBundle\Util\SluggerUtil; */ class PackExtension extends AbstractExtension { /** - * @link https://twig.symfony.com/doc/2.x/advanced.html - * * {@inheritdoc} + * + * @link https://twig.symfony.com/doc/2.x/advanced.html */ public function __construct(protected IntlUtil $intl, protected FileLocator $locator, protected PackageInterface $package, protected SluggerUtil $slugger, protected array $parameters) { } diff --git a/Form/CaptchaType.php b/Form/CaptchaType.php index c0d5ae1..2ff22ba 100644 --- a/Form/CaptchaType.php +++ b/Form/CaptchaType.php @@ -40,9 +40,9 @@ class CaptchaType extends AbstractType { } /** - * Build form - * * {@inheritdoc} + * + * Build form */ public function buildForm(FormBuilderInterface $builder, array $options): void { //Set captcha diff --git a/Package/PathPackage.php b/Package/PathPackage.php index c202b19..22ed5ad 100644 --- a/Package/PathPackage.php +++ b/Package/PathPackage.php @@ -57,6 +57,8 @@ class PathPackage extends Package { } /** + * {@inheritdoc} + * * Returns an absolute or root-relative public path * * Transform @BundleBundle to bundle and remove /Resources/public fragment from path @@ -65,8 +67,6 @@ class PathPackage extends Package { * @link https://symfony.com/doc/current/bundles.html#overridding-the-bundle-directory-structure * @see vendor/symfony/framework-bundle/Command/AssetsInstallCommand.php +113 * @see vendor/symfony/framework-bundle/Command/AssetsInstallCommand.php +141 - * - * {@inheritdoc} */ public function getUrl(string $path): string { //Match url starting with a bundle name -- 2.41.0 From 47fb9009ea1dcd97ccbca1b787124407057d4c0a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Thu, 7 Mar 2024 20:43:55 +0100 Subject: [PATCH 05/16] Readd getAlias member function --- DependencyInjection/RapsysPackExtension.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/DependencyInjection/RapsysPackExtension.php b/DependencyInjection/RapsysPackExtension.php index ce77518..d6e3c80 100644 --- a/DependencyInjection/RapsysPackExtension.php +++ b/DependencyInjection/RapsysPackExtension.php @@ -55,4 +55,13 @@ class RapsysPackExtension extends Extension { //Set rapsyspack.version key $container->setParameter($alias.'.version', RapsysPackBundle::getVersion()); } + + /** + * {@inheritdoc} + * + * @xxx Required by kernel to load renamed alias configuration + */ + public function getAlias(): string { + return RapsysPackBundle::getAlias(); + } } -- 2.41.0 From f20e37360674d48c164ad95ce0105d7f5f5014ef Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Fri, 8 Mar 2024 02:08:30 +0100 Subject: [PATCH 06/16] Add getEastern member function --- Util/IntlUtil.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Util/IntlUtil.php b/Util/IntlUtil.php index eb7711f..b37169d 100644 --- a/Util/IntlUtil.php +++ b/Util/IntlUtil.php @@ -84,6 +84,46 @@ class IntlUtil { return $formatter->formatCurrency($number, $currency); } + /** + * Compute eastern for selected year + * + * @param string $year The eastern year + * + * @return DateTime The eastern date + */ + public function getEastern(string $year): \DateTime { + //Set static results + static $results = []; + + //Check if already computed + if (isset($results[$year])) { + //Return computed eastern + return $results[$year]; + } + + $d = (19 * ($year % 19) + 24) % 30; + + $e = (2 * ($year % 4) + 4 * ($year % 7) + 6 * $d + 5) % 7; + + $day = 22 + $d + $e; + + $month = 3; + + if ($day > 31) { + $day = $d + $e - 9; + $month = 4; + } elseif ($d == 29 && $e == 6) { + $day = 10; + $month = 4; + } elseif ($d == 28 && $e == 6) { + $day = 18; + $month = 4; + } + + //Store eastern in data + return ($results[$year] = new \DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day))); + } + /** * Gets number formatter instance matching locale and style. * -- 2.41.0 From fc8fb1449c29f4bc6056ed7d63b1fe904cf59051 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Fri, 8 Mar 2024 02:09:04 +0100 Subject: [PATCH 07/16] Fix pack bundle scheme env variable usage --- Parser/TokenParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parser/TokenParser.php b/Parser/TokenParser.php index 7c3ad41..0ca3e30 100644 --- a/Parser/TokenParser.php +++ b/Parser/TokenParser.php @@ -143,7 +143,7 @@ class TokenParser extends AbstractTokenParser { //Deal with generic url if (strpos($inputs[$k], '//') === 0) { //Fix url - $inputs[$k] = ($_ENV['RAPSYSPACK_SCHEME'] ?? 'https://').substr($inputs[$k], 2); + $inputs[$k] = ($_ENV['RAPSYSPACK_SCHEME'] ?? 'https').'://'.substr($inputs[$k], 2); //Deal with non url path } elseif (strpos($inputs[$k], '://') === false) { //Check if we have a bundle path -- 2.41.0 From 4b8ba7387936a2385a323d341847fa050c7846de Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Fri, 8 Mar 2024 02:09:34 +0100 Subject: [PATCH 08/16] Drop useless pack bundle hostname env variable Fix pack bundle scheme env variable --- Resources/config/packages/rapsyspack.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Resources/config/packages/rapsyspack.yaml b/Resources/config/packages/rapsyspack.yaml index c02e1ba..1bb195a 100644 --- a/Resources/config/packages/rapsyspack.yaml +++ b/Resources/config/packages/rapsyspack.yaml @@ -2,14 +2,12 @@ parameters: # User agent env(RAPSYSPACK_AGENT): "rapsyspack/Ch4ng3m3!" - # Hostname - env(RAPSYSPACK_HOSTNAME): "Ch4ng3m3!" # Shuffled printable character range env(RAPSYSPACK_RANGE): 'Ch4ng3m3!' # Redirect env(RAPSYSPACK_REDIRECT): 20 # Scheme - env(RAPSYSPACK_SCHEME): "https://" + env(RAPSYSPACK_SCHEME): "https" # Timeout env(RAPSYSPACK_TIMEOUT): 60 -- 2.41.0 From 738cb84ed85c3e542199919f683140cfa6a2da7e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Fri, 8 Mar 2024 16:57:01 +0100 Subject: [PATCH 09/16] Remove constructor range argument Protect constructor from unset range Cleanup --- Resources/config/packages/rapsyspack.yaml | 2 +- Util/SluggerUtil.php | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Resources/config/packages/rapsyspack.yaml b/Resources/config/packages/rapsyspack.yaml index 1bb195a..f57cf74 100644 --- a/Resources/config/packages/rapsyspack.yaml +++ b/Resources/config/packages/rapsyspack.yaml @@ -53,7 +53,7 @@ services: # Register slugger util service rapsyspack.slugger_util: class: 'Rapsys\PackBundle\Util\SluggerUtil' - arguments: [ '%env(string:RAPSYSPACK_RANGE)%', '%kernel.secret%' ] + arguments: [ '%kernel.secret%' ] public: true # Register range command Rapsys\PackBundle\Command\RangeCommand: diff --git a/Util/SluggerUtil.php b/Util/SluggerUtil.php index 3b1e6b5..eb6041e 100644 --- a/Util/SluggerUtil.php +++ b/Util/SluggerUtil.php @@ -38,20 +38,25 @@ class SluggerUtil { /** * Construct slugger util * - * @description Run "php bin/console rapsyspack:range" to generate RAPSYSPACK_RANGE="ayl[...]z9w" range in .env.local + * Run "bin/console rapsyspack:range" to generate RAPSYSPACK_RANGE="ayl[...]z9w" range in .env.local * * @todo Use Cache like in calendar controller through FilesystemAdapter ? * - * @param string $range The shuffled range string * @param string $secret The secret string */ - public function __construct(protected string $range, protected string $secret) { + public function __construct(protected string $secret) { + //Without range + if (empty($range = $_ENV['RAPSYSPACK_RANGE']) || $range === 'Ch4ng3m3!') { + //Protect member variable setup + return; + } + /** * Get pseuto-random alphabet by splitting range string * TODO: see required range by json_encode result and short input (0->255 ???) * XXX: The key count mismatch, count(alpha)>count(rev), resulted in a data corruption due to duplicate numeric values */ - $this->alpha = str_split($this->range); + $this->alpha = str_split($range); //Init rev array $this->count = count($rev = $this->rev = array_flip($this->alpha)); @@ -60,6 +65,7 @@ class SluggerUtil { $split = str_split($this->secret); //Set offset + //TODO: protect undefined index ? $this->offset = array_reduce($split, function ($res, $a) use ($rev) { return $res += $rev[$a]; }, count($split)) % $this->count; } -- 2.41.0 From 380623890b4880b1ea107945f7a3b9b59409081e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Fri, 8 Mar 2024 16:58:22 +0100 Subject: [PATCH 10/16] Cleanup --- DependencyInjection/Configuration.php | 4 ++-- README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 95326bc..e34da5b 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -82,8 +82,8 @@ class Configuration implements ConfigurationInterface { * @link http://symfony.com/doc/current/components/config/definition.html * @link https://github.com/symfony/assetic-bundle/blob/master/DependencyInjection/Configuration.php#L63 * - * @see php bin/console config:dump-reference rapsyspack to dump default config - * @see php bin/console debug:config rapsyspack to dump config + * @see bin/console config:dump-reference rapsyspack to dump default config + * @see bin/console debug:config rapsyspack to dump config */ $treeBuilder //Parameters diff --git a/README.md b/README.md index e51fc22..81cd07a 100644 --- a/README.md +++ b/README.md @@ -202,14 +202,14 @@ Open a command console, enter your project directory and execute the following command to see default bundle configuration: ```console -$ php bin/console config:dump-reference RapsysPackBundle +$ bin/console config:dump-reference RapsysPackBundle ``` Open a command console, enter your project directory and execute the following command to see current bundle configuration: ```console -$ php bin/console debug:config RapsysPackBundle +$ bin/console debug:config RapsysPackBundle ``` ### Step 4: Use the twig extension in your Template -- 2.41.0 From fedff27fa8b2f164d11e5fa218118f6bc10a57e4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Fri, 8 Mar 2024 18:32:01 +0100 Subject: [PATCH 11/16] Cleanup --- Command/RangeCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Command/RangeCommand.php b/Command/RangeCommand.php index 65b4fe4..b5586de 100644 --- a/Command/RangeCommand.php +++ b/Command/RangeCommand.php @@ -26,14 +26,14 @@ class RangeCommand extends Command { /** * Set description * - * @description Shown with bin/console list + * Shown with bin/console list */ protected string $description = 'Outputs a shuffled printable characters range'; /** * Set help * - * @description Shown with bin/console --help packbundle:range + * Shown with bin/console --help rapsyspack:range */ protected string $help = 'This command outputs a shuffled printable characters range'; -- 2.41.0 From f06acce2c1058d581657163417952723ff6d71cd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 26 Mar 2024 18:59:56 +0100 Subject: [PATCH 12/16] Set constructor parameters as optional Only add captcha with image, slugger and translator available Do not map captcha fields Cleanup --- Form/CaptchaType.php | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/Form/CaptchaType.php b/Form/CaptchaType.php index 2ff22ba..83f3b1b 100644 --- a/Form/CaptchaType.php +++ b/Form/CaptchaType.php @@ -14,14 +14,14 @@ namespace Rapsys\PackBundle\Form; use Rapsys\PackBundle\Util\ImageUtil; use Rapsys\PackBundle\Util\SluggerUtil; -use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\IntegerType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Form\FormEvents; -use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormError; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; +use Symfony\Contracts\Translation\TranslatorInterface; /** * Captcha Type class definition @@ -32,11 +32,11 @@ class CaptchaType extends AbstractType { /** * Constructor * - * @param ImageUtil $image - * @param SluggerUtil $slugger - * @param TranslatorInterface $translator The translator instance + * @param ?ImageUtil $image + * @param ?SluggerUtil $slugger + * @param ?TranslatorInterface $translator The translator instance */ - public function __construct(protected ImageUtil $image, protected SluggerUtil $slugger, protected TranslatorInterface $translator) { + public function __construct(protected ?ImageUtil $image = null, protected ?SluggerUtil $slugger = null, protected ?TranslatorInterface $translator = null) { } /** @@ -45,18 +45,21 @@ class CaptchaType extends AbstractType { * Build form */ public function buildForm(FormBuilderInterface $builder, array $options): void { - //Set captcha - $captcha = $this->image->getCaptcha((new \DateTime('-1 year'))->getTimestamp()); + //With image, slugger and translator + if ($this->image !== null && $this->slugger !== null && $this->translator !== null) { + //Set captcha + $captcha = $this->image->getCaptcha((new \DateTime('-1 year'))->getTimestamp()); - //Add captcha token - $builder->add('_captcha_token', HiddenType::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token']]); + //Add captcha token + $builder->add('_captcha_token', HiddenType::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token'], 'mapped' => false]); - //Add captcha - $builder->add('captcha', IntegerType::class, ['label_attr' => ['class' => 'captcha'], 'label' => ''.htmlentities($captcha['equation']).'', 'label_html' => true, 'translation_domain' => false]); + //Add captcha + $builder->add('captcha', IntegerType::class, ['label_attr' => ['class' => 'captcha'], 'label' => ''.htmlentities($captcha['equation']).'', 'label_html' => true, 'mapped' => false, 'translation_domain' => false]); - //Add event listener on captcha - $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'validateCaptcha']); - } + //Add event listener on captcha + $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'validateCaptcha']); + } + } /** * Validate captcha -- 2.41.0 From 0630ec4bdb74d50e2cc6353dca9d8c691fe22479 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 26 Mar 2024 19:01:54 +0100 Subject: [PATCH 13/16] Cleanup --- Command.php | 6 +++--- Command/RangeCommand.php | 4 ++-- Controller/ImageController.php | 8 ++++---- Controller/MapController.php | 8 ++++---- DependencyInjection/Configuration.php | 4 ++-- DependencyInjection/RapsysPackExtension.php | 4 ++-- Extension/PackExtension.php | 10 +++++----- Package/PathPackage.php | 4 ++-- Parser/TokenParser.php | 4 ++-- RapsysPackBundle.php | 4 ++-- 10 files changed, 28 insertions(+), 28 deletions(-) diff --git a/Command.php b/Command.php index 51fbb30..0097d12 100644 --- a/Command.php +++ b/Command.php @@ -11,11 +11,11 @@ namespace Rapsys\PackBundle; +use Rapsys\PackBundle\RapsysPackBundle; + use Symfony\Component\Console\Command\Command as BaseCommand; use Symfony\Component\DependencyInjection\Container; -use Rapsys\PackBundle\RapsysPackBundle; - /** * {@inheritdoc} */ @@ -23,7 +23,7 @@ class Command extends BaseCommand { /** * {@inheritdoc} */ - public function __construct(protected ?string $name = null) { + public function __construct(protected ?string $name = null) { //Fix name $this->name = $this->name ?? static::getName(); diff --git a/Command/RangeCommand.php b/Command/RangeCommand.php index b5586de..7c3270d 100644 --- a/Command/RangeCommand.php +++ b/Command/RangeCommand.php @@ -11,12 +11,12 @@ namespace Rapsys\PackBundle\Command; +use Rapsys\PackBundle\Command; + use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Rapsys\PackBundle\Command; - /** * {@inheritdoc} * diff --git a/Controller/ImageController.php b/Controller/ImageController.php index 36270ba..13865a7 100644 --- a/Controller/ImageController.php +++ b/Controller/ImageController.php @@ -11,22 +11,22 @@ namespace Rapsys\PackBundle\Controller; +use Rapsys\PackBundle\Util\ImageUtil; +use Rapsys\PackBundle\Util\SluggerUtil; + use Psr\Container\ContainerInterface; -use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Filesystem\Exception\IOExceptionInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\BinaryFileResponse; +use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Routing\RequestContext; use Symfony\Contracts\Service\ServiceSubscriberInterface; -use Rapsys\PackBundle\Util\ImageUtil; -use Rapsys\PackBundle\Util\SluggerUtil; - /** * {@inheritdoc} */ diff --git a/Controller/MapController.php b/Controller/MapController.php index 255c47b..8d38aa6 100644 --- a/Controller/MapController.php +++ b/Controller/MapController.php @@ -11,22 +11,22 @@ namespace Rapsys\PackBundle\Controller; +use Rapsys\PackBundle\Util\MapUtil; +use Rapsys\PackBundle\Util\SluggerUtil; + use Psr\Container\ContainerInterface; -use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Filesystem\Exception\IOExceptionInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\BinaryFileResponse; +use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Routing\RequestContext; use Symfony\Contracts\Service\ServiceSubscriberInterface; -use Rapsys\PackBundle\Util\MapUtil; -use Rapsys\PackBundle\Util\SluggerUtil; - /** * {@inheritdoc} */ diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index e34da5b..60b68b6 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -11,13 +11,13 @@ namespace Rapsys\PackBundle\DependencyInjection; +use Rapsys\PackBundle\RapsysPackBundle; + use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\Process\ExecutableFinder; -use Rapsys\PackBundle\RapsysPackBundle; - /** * {@inheritdoc} * diff --git a/DependencyInjection/RapsysPackExtension.php b/DependencyInjection/RapsysPackExtension.php index d6e3c80..fc9efce 100644 --- a/DependencyInjection/RapsysPackExtension.php +++ b/DependencyInjection/RapsysPackExtension.php @@ -11,11 +11,11 @@ namespace Rapsys\PackBundle\DependencyInjection; +use Rapsys\PackBundle\RapsysPackBundle; + use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; -use Rapsys\PackBundle\RapsysPackBundle; - /** * This is the class that loads and manages your bundle configuration. * diff --git a/Extension/PackExtension.php b/Extension/PackExtension.php index 1234de5..7107548 100644 --- a/Extension/PackExtension.php +++ b/Extension/PackExtension.php @@ -11,16 +11,16 @@ namespace Rapsys\PackBundle\Extension; -use Symfony\Component\Asset\PackageInterface; -use Symfony\Component\HttpKernel\Config\FileLocator; - -use Twig\Extension\AbstractExtension; - use Rapsys\PackBundle\Parser\TokenParser; use Rapsys\PackBundle\RapsysPackBundle; use Rapsys\PackBundle\Util\IntlUtil; use Rapsys\PackBundle\Util\SluggerUtil; +use Symfony\Component\Asset\PackageInterface; +use Symfony\Component\HttpKernel\Config\FileLocator; + +use Twig\Extension\AbstractExtension; + /** * {@inheritdoc} */ diff --git a/Package/PathPackage.php b/Package/PathPackage.php index 22ed5ad..c53b10f 100644 --- a/Package/PathPackage.php +++ b/Package/PathPackage.php @@ -11,12 +11,12 @@ namespace Rapsys\PackBundle\Package; +use Rapsys\PackBundle\Context\NullContext; + use Symfony\Component\Asset\Context\ContextInterface; use Symfony\Component\Asset\Package; use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface; -use Rapsys\PackBundle\Context\NullContext; - /** * {@inheritdoc} */ diff --git a/Parser/TokenParser.php b/Parser/TokenParser.php index 0ca3e30..67e87eb 100644 --- a/Parser/TokenParser.php +++ b/Parser/TokenParser.php @@ -11,6 +11,8 @@ namespace Rapsys\PackBundle\Parser; +use Rapsys\PackBundle\RapsysPackBundle; + use Symfony\Component\Asset\PackageInterface; use Symfony\Component\Filesystem\Exception\IOExceptionInterface; use Symfony\Component\Filesystem\Filesystem; @@ -25,8 +27,6 @@ use Twig\Source; use Twig\Token; use Twig\TokenParser\AbstractTokenParser; -use Rapsys\PackBundle\RapsysPackBundle; - /** * {@inheritdoc} */ diff --git a/RapsysPackBundle.php b/RapsysPackBundle.php index 4ad24d4..49dfdaf 100644 --- a/RapsysPackBundle.php +++ b/RapsysPackBundle.php @@ -11,11 +11,11 @@ namespace Rapsys\PackBundle; +use Rapsys\PackBundle\DependencyInjection\RapsysPackExtension; + use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; use Symfony\Component\HttpKernel\Bundle\Bundle; -use Rapsys\PackBundle\DependencyInjection\RapsysPackExtension; - /** * {@inheritdoc} */ -- 2.41.0 From 068e3ad94a51810a9d5ebdff3b2a60a746663d45 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 26 Mar 2024 19:03:35 +0100 Subject: [PATCH 14/16] Version 0.5.0 --- RapsysPackBundle.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RapsysPackBundle.php b/RapsysPackBundle.php index 49dfdaf..40e0563 100644 --- a/RapsysPackBundle.php +++ b/RapsysPackBundle.php @@ -64,6 +64,6 @@ class RapsysPackBundle extends Bundle { */ public static function getVersion(): string { //Return version - return '0.4.0'; + return '0.5.0'; } } -- 2.41.0 From 9b9263c9eef897f1e6919ed88df1308c19d3081b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 26 Mar 2024 19:17:33 +0100 Subject: [PATCH 15/16] Cleanup --- Form/CaptchaType.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Form/CaptchaType.php b/Form/CaptchaType.php index 83f3b1b..a2ecb87 100644 --- a/Form/CaptchaType.php +++ b/Form/CaptchaType.php @@ -32,8 +32,8 @@ class CaptchaType extends AbstractType { /** * Constructor * - * @param ?ImageUtil $image - * @param ?SluggerUtil $slugger + * @param ?ImageUtil $image The image instance + * @param ?SluggerUtil $slugger The slugger instance * @param ?TranslatorInterface $translator The translator instance */ public function __construct(protected ?ImageUtil $image = null, protected ?SluggerUtil $slugger = null, protected ?TranslatorInterface $translator = null) { -- 2.41.0 From 371738010baf06170c7b82495080884f9f782574 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 2 Apr 2024 05:42:10 +0200 Subject: [PATCH 16/16] Add captcha option --- Form/CaptchaType.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Form/CaptchaType.php b/Form/CaptchaType.php index a2ecb87..c3a5f69 100644 --- a/Form/CaptchaType.php +++ b/Form/CaptchaType.php @@ -21,6 +21,7 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; +use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Contracts\Translation\TranslatorInterface; /** @@ -46,7 +47,7 @@ class CaptchaType extends AbstractType { */ public function buildForm(FormBuilderInterface $builder, array $options): void { //With image, slugger and translator - if ($this->image !== null && $this->slugger !== null && $this->translator !== null) { + if (!empty($options['captcha']) && $this->image !== null && $this->slugger !== null && $this->translator !== null) { //Set captcha $captcha = $this->image->getCaptcha((new \DateTime('-1 year'))->getTimestamp()); @@ -61,6 +62,20 @@ class CaptchaType extends AbstractType { } } + /** + * {@inheritdoc} + */ + public function configureOptions(OptionsResolver $resolver): void { + //Call parent configure options + parent::configureOptions($resolver); + + //Set defaults + $resolver->setDefaults(['captcha' => false]); + + //Add extra captcha option + $resolver->setAllowedTypes('captcha', 'boolean'); + } + /** * Validate captcha * -- 2.41.0