From 48ee68f71a695b6c9434bf529f23dad5d0ec10d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Sun, 17 Nov 2024 12:33:40 +0100 Subject: [PATCH 1/2] Add twig extra bundle to use currency formatter filter. Add custom twig method to display number of active promos. Remove old index.php file. --- composer.json | 4 +- composer.lock | 226 ++++++++++++++++++++++++++- index.php | 5 +- index_old.php | 48 ------ src/Controller/ProductController.php | 4 +- src/Twig/AppExtension.php | 23 +++ templates/product.html.twig | 8 +- templates/productList.html.twig | 4 +- templates/template.html.twig | 2 +- 9 files changed, 264 insertions(+), 60 deletions(-) delete mode 100644 index_old.php create mode 100644 src/Twig/AppExtension.php diff --git a/composer.json b/composer.json index 4600aee..299ddad 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,9 @@ "symfony/http-kernel": "^7.1", "symfony/framework-bundle": "^7.1", "symfony/twig-bundle": "^7.1", - "symfony/dotenv": "^7.1" + "symfony/dotenv": "^7.1", + "twig/intl-extra": "^3.13", + "twig/extra-bundle": "^3.13" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 629c927..184cebc 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f2dfc64ddf11dc6a94b37f7eac9ffc0f", + "content-hash": "4eef5feb5e2635d7fb7c69c8ed01a84c", "packages": [ { "name": "brick/math", @@ -2949,6 +2949,92 @@ ], "time": "2024-09-21T06:09:21+00:00" }, + { + "name": "symfony/intl", + "version": "v7.1.8", + "source": { + "type": "git", + "url": "https://github.com/symfony/intl.git", + "reference": "e56b243fc0afa5a12bd11dace4002ada5a7d99f8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/intl/zipball/e56b243fc0afa5a12bd11dace4002ada5a7d99f8", + "reference": "e56b243fc0afa5a12bd11dace4002ada5a7d99f8", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "symfony/deprecation-contracts": "^2.5|^3" + }, + "conflict": { + "symfony/string": "<7.1" + }, + "require-dev": { + "symfony/filesystem": "^6.4|^7.0", + "symfony/var-exporter": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Intl\\": "" + }, + "exclude-from-classmap": [ + "/Tests/", + "/Resources/data/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + }, + { + "name": "Eriksen Costa", + "email": "eriksen.costa@infranology.com.br" + }, + { + "name": "Igor Wiedler", + "email": "igor@wiedler.ch" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides access to the localization data of the ICU library", + "homepage": "https://symfony.com", + "keywords": [ + "i18n", + "icu", + "internationalization", + "intl", + "l10n", + "localization" + ], + "support": { + "source": "https://github.com/symfony/intl/tree/v7.1.8" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-11-08T15:46:42+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.31.0", @@ -4194,6 +4280,144 @@ ], "time": "2024-06-28T08:00:31+00:00" }, + { + "name": "twig/extra-bundle", + "version": "v3.13.0", + "source": { + "type": "git", + "url": "https://github.com/twigphp/twig-extra-bundle.git", + "reference": "21a9a7aa9f79d4493bb6fed4eb2794339f9551f5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/twig-extra-bundle/zipball/21a9a7aa9f79d4493bb6fed4eb2794339f9551f5", + "reference": "21a9a7aa9f79d4493bb6fed4eb2794339f9551f5", + "shasum": "" + }, + "require": { + "php": ">=8.0.2", + "symfony/framework-bundle": "^5.4|^6.4|^7.0", + "symfony/twig-bundle": "^5.4|^6.4|^7.0", + "twig/twig": "^3.0|^4.0" + }, + "require-dev": { + "league/commonmark": "^1.0|^2.0", + "symfony/phpunit-bridge": "^6.4|^7.0", + "twig/cache-extra": "^3.0", + "twig/cssinliner-extra": "^3.0", + "twig/html-extra": "^3.0", + "twig/inky-extra": "^3.0", + "twig/intl-extra": "^3.0", + "twig/markdown-extra": "^3.0", + "twig/string-extra": "^3.0" + }, + "type": "symfony-bundle", + "autoload": { + "psr-4": { + "Twig\\Extra\\TwigExtraBundle\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + } + ], + "description": "A Symfony bundle for extra Twig extensions", + "homepage": "https://twig.symfony.com", + "keywords": [ + "bundle", + "extra", + "twig" + ], + "support": { + "source": "https://github.com/twigphp/twig-extra-bundle/tree/v3.13.0" + }, + "funding": [ + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/twig/twig", + "type": "tidelift" + } + ], + "time": "2024-09-01T20:39:12+00:00" + }, + { + "name": "twig/intl-extra", + "version": "v3.13.0", + "source": { + "type": "git", + "url": "https://github.com/twigphp/intl-extra.git", + "reference": "1b8d78c5db08bdc61015fd55009d2e84b3aa7e38" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/intl-extra/zipball/1b8d78c5db08bdc61015fd55009d2e84b3aa7e38", + "reference": "1b8d78c5db08bdc61015fd55009d2e84b3aa7e38", + "shasum": "" + }, + "require": { + "php": ">=8.0.2", + "symfony/intl": "^5.4|^6.4|^7.0", + "twig/twig": "^3.13|^4.0" + }, + "require-dev": { + "symfony/phpunit-bridge": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Twig\\Extra\\Intl\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + } + ], + "description": "A Twig extension for Intl", + "homepage": "https://twig.symfony.com", + "keywords": [ + "intl", + "twig" + ], + "support": { + "source": "https://github.com/twigphp/intl-extra/tree/v3.13.0" + }, + "funding": [ + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/twig/twig", + "type": "tidelift" + } + ], + "time": "2024-09-03T13:08:40+00:00" + }, { "name": "twig/twig", "version": "v3.14.0", diff --git a/index.php b/index.php index 005d203..38ab369 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,6 @@ autowire() ->autoconfigure() ; + $container->services()->set(AppExtension::class)->tag('twig.extension'); } protected function configureRoutes(RoutingConfigurator $routes): void diff --git a/index_old.php b/index_old.php deleted file mode 100644 index 785c415..0000000 --- a/index_old.php +++ /dev/null @@ -1,48 +0,0 @@ -database.sqlite missing. Run docker compose
docker compose exec php-app php index.php app:migrate
to create it.'); -} -$productRoute = new Route('/product/{product_id<\d+>}', ['_controller' => ProductController::class]); -$searchRoute = new Route('/?search={search_term}', ['_controller' => SearchController::class]); -$categoryRoute = new Route('/category/{category_name}', ['_controller' => CategoryController::class]); -$starRoute = new Route('/star/{product_id}', ['_controller' => StarController::class]); -$promosRoute = new Route('/promos', ['_controller' => PromosController::class]); -$indexRoute = new Route('/', ['_controller' => IndexController::class]); -$routes = new RouteCollection(); - -$routes->add('product_show', $productRoute); -$routes->add('search_show', $searchRoute); -$routes->add('category_show', $categoryRoute); -$routes->add('start_show', $starRoute); -$routes->add('promos_show', $promosRoute); -$routes->add('index_show', $indexRoute); - -$context = new RequestContext(); -$matcher = new UrlMatcher($routes, $context); -try { - $parameters = $matcher->match($_SERVER['REQUEST_URI']); -} catch (Exception $e) { - die($e->getMessage()); -} - -match ($parameters['_controller']) { - SearchController::class => (new $parameters['_controller']())($parameters['search_term']), - CategoryController::class => (new $parameters['_controller']())($parameters['category_name']), - ProductController::class, StarController::class => (new $parameters['_controller']())($parameters['product_id']), - PromosController::class, IndexController::class => (new $parameters['_controller']())(), - default => throw new Exception('Route not found') -}; diff --git a/src/Controller/ProductController.php b/src/Controller/ProductController.php index b15e938..042cd89 100644 --- a/src/Controller/ProductController.php +++ b/src/Controller/ProductController.php @@ -2,7 +2,6 @@ namespace Krzysiej\RyobiCrawler\Controller; -use Illuminate\Support\ItemNotFoundException; use Krzysiej\RyobiCrawler\Models\Product; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; @@ -21,7 +20,8 @@ final class ProductController extends BaseController public function __invoke(int $productId): Response { $product = Product::with([ - 'price' => fn($query) => $query->orderBy('created_at', 'desc') + 'price' => fn($query) => $query->orderBy('created_at', 'desc'), + 'stock' => fn($query) => $query->orderBy('created_at', 'desc'), ])->find($productId); if(null === $product) { diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php new file mode 100644 index 0000000..fc6216f --- /dev/null +++ b/src/Twig/AppExtension.php @@ -0,0 +1,23 @@ + $query->whereColumn('price', '<', 'productStandardPrice'))->with(['currentPrice'])->count(); + } +} \ No newline at end of file diff --git a/templates/product.html.twig b/templates/product.html.twig index 6e77d72..3bfd197 100644 --- a/templates/product.html.twig +++ b/templates/product.html.twig @@ -6,7 +6,7 @@ {% if product.starred %}★{% else %} ☆ {% endif %} {{ product.name }} - {{ product.name }} + {{ product.name }} {{ product.subTitle }} link - {{ product.price.last.price }} - {% if product.price.last.price != product.price.last.productStandardPrice %}{{ product.price.last.productStandardPrice }} {{ ((1 - product.price.last.price / product.price.last.productStandardPrice)*100)|number_format(0) }}%{% else %}{% endif %} + {{ product.price.last.price | format_currency('PLN', {}, 'pl') }} + {% if product.price.last.price != product.price.last.productStandardPrice %}{{ product.price.last.productStandardPrice | format_currency('PLN', {}, 'pl') }} {{ ((1 - product.price.last.price / product.price.last.productStandardPrice)*100)|number_format(0) }}%{% else %}{% endif %} {% endfor %} diff --git a/templates/template.html.twig b/templates/template.html.twig index 0761055..bc05a88 100644 --- a/templates/template.html.twig +++ b/templates/template.html.twig @@ -19,7 +19,7 @@