Upgrade symfony from 7 to 8. Remove cache since it is now faster to open website. #62

Merged
krzysiej merged 1 commits from feature/-upgrade-symfony-remove-cache into master 2026-02-07 09:29:18 +01:00
17 changed files with 310 additions and 485 deletions

View File

@@ -1,18 +1,18 @@
{ {
"require": { "require": {
"guzzlehttp/guzzle": "^7.0", "guzzlehttp/guzzle": "^7",
"illuminate/database": "11.26.0.0", "illuminate/database": "11.26.0.0",
"ext-json": "*", "ext-json": "*",
"symfony/console": "^7.0", "symfony/console": "^8.0",
"symfony/routing": "^7.1", "symfony/routing": "^8.0",
"laravel/serializable-closure": "^1.3", "laravel/serializable-closure": "^1.3",
"symfony/http-kernel": "^7.1", "symfony/http-kernel": "^8.0",
"symfony/framework-bundle": "^7.1", "symfony/framework-bundle": "^8.0",
"symfony/twig-bundle": "^7.1", "symfony/twig-bundle": "^8.0",
"symfony/dotenv": "^7.1", "symfony/dotenv": "^8.0",
"twig/intl-extra": "^3.13", "twig/intl-extra": "^3.13",
"twig/extra-bundle": "^3.13", "twig/extra-bundle": "^3.13",
"symfony/cache": "^7.2", "symfony/cache": "^8.0",
"twig/cache-extra": "^3.21" "twig/cache-extra": "^3.21"
}, },
"autoload": { "autoload": {
@@ -21,6 +21,7 @@
} }
}, },
"require-dev": { "require-dev": {
"symfony/var-dumper": "^7.1" "symfony/var-dumper": "^8.0"
} },
"minimum-stability": "stable"
} }

623
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,84 +0,0 @@
<?php
declare(strict_types=1);
namespace Krzysiej\RyobiCrawler\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Database\Capsule\Manager as Capsule;
use Krzysiej\RyobiCrawler\Models\Product;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Environment;
#[AsCommand(name: 'app:cache:warm-twig', description: 'Warmup twig cache')]
class CacheWarmCommand extends Command
{
public function __construct(
private Environment $twig,
private RequestStack $requestStack,
private FilesystemAdapter $cache,
) {
parent::__construct();
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$this->cache->clear();
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'sqlite',
'database' => __DIR__ . '/../../database.sqlite',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
$progress = new ProgressBar($output);
$progress->start();
$products = Product::with([
'price' => fn($query) => $query->orderByDesc('created_at'),
'stock' => fn($query) => $query->orderByDesc('created_at'),
])->get();
$progress->setMaxSteps(count($products));
$request = Request::create('/cache-warm');
$this->requestStack->push($request);
foreach ($products as $product) {
$priceList = $product->price()->pluck('price');
$stockList = $product->stock()->pluck('stock');
$priceDates = $product->price()->pluck('created_at')->map(fn($date) => $date->format('Y-m-d'))->toArray();
$stockDates = $product->stock()->pluck('created_at')->map(fn($date) => $date->format('Y-m-d'))->toArray();
$this->twig->render('product.html.twig', [
'product' => $product,
'price_list' => $this->prepareChartData($priceDates, $priceList),
'stock_list' => $this->prepareChartData($stockDates, $stockList),
'price_dates' => implode("','", $priceDates),
]);
$progress->advance();
}
$progress->finish();
$output->writeln('');
$output->writeln('DONE');
return Command::SUCCESS;
}
private function prepareChartData($set1, $set2): string
{
$data = [];
foreach ($set1 as $key => $value) {
$data[] = ['x' => $value, 'y' => $set2[$key]];
}
$stringData = json_encode($data);
return str_replace(['"x"', '"y"'], ['x', 'y'], $stringData);
}
}

View File

@@ -51,9 +51,7 @@ class ScrapeWebsite extends Command
$progress->advance(); $progress->advance();
} }
$progress->finish(); $progress->finish();
$output->writeln(''); $output->writeln("\nScrape products - DONE\n");
$output->writeln('Scrape products - DONE');
$output->writeln('');
} }
$output->writeln('Update prices'); $output->writeln('Update prices');
@@ -92,7 +90,7 @@ class ScrapeWebsite extends Command
'form_params' => [ 'form_params' => [
"includePreviousPages" => false, "includePreviousPages" => false,
"pageIndex" => $page, "pageIndex" => $page,
"pageSize" => 1000, "pageSize" => 100,
"cultureCode" => $country->cultureCode, "cultureCode" => $country->cultureCode,
] ]
]); ]);

View File

@@ -11,7 +11,7 @@ class BaseController extends AbstractController
{ {
protected Environment $twig; protected Environment $twig;
public function __construct(protected FilesystemAdapter $cache, protected Capsule $database) public function __construct( protected Capsule $database)
{ {
} }
} }

View File

@@ -11,9 +11,6 @@ final class CategoryController extends BaseController
#[Route('/category/{category?}', name: 'app_category')] #[Route('/category/{category?}', name: 'app_category')]
public function __invoke(?string $category): Response public function __invoke(?string $category): Response
{ {
if($this->cache->getItem('list_category_'.$category)->isHit()) {
return $this->render('productList.html.twig', ['listType' => 'category_'.$category]);
}
/** @var Product[] $products */ /** @var Product[] $products */
$products = Product::with(['price', 'lowestPrice']) $products = Product::with(['price', 'lowestPrice'])

View File

@@ -13,9 +13,6 @@ final class DiscontinuedController extends BaseController
#[Route('/discontinued', name: 'app_discontinued')] #[Route('/discontinued', name: 'app_discontinued')]
public function __invoke(): Response public function __invoke(): Response
{ {
if($this->cache->getItem('list_discontinued')->isHit()) {
return $this->render('productList.html.twig', ['listType' => 'discontinued']);
}
$products = Product::where('lastSeen', '<', now()->format('Y-m-d')) $products = Product::where('lastSeen', '<', now()->format('Y-m-d'))
->orderByDesc('starred') ->orderByDesc('starred')

View File

@@ -11,9 +11,6 @@ final class IndexController extends BaseController
#[Route('/', name: 'app_home')] #[Route('/', name: 'app_home')]
public function __invoke(): Response public function __invoke(): Response
{ {
if ($this->cache->getItem('list_all')->isHit()) {
return $this->render('productList.html.twig', ['listType' => 'all']);
}
$products = Product::orderByDesc('starred') $products = Product::orderByDesc('starred')
->orderByDesc('created_by') ->orderByDesc('created_by')
->get(); ->get();

View File

@@ -13,9 +13,6 @@ final class LowestPriceController extends BaseController
public function __invoke(): Response public function __invoke(): Response
{ {
$listType = 'lowest_price'; $listType = 'lowest_price';
if($this->cache->getItem('lowest_price')->isHit()) {
return $this->render('productList.html.twig', ['listType' => $listType]);
}
$products = Product::whereRaw('priceCurrent = priceLowest') $products = Product::whereRaw('priceCurrent = priceLowest')
->whereRaw('lastSeen = "'.now()->format('Y-m-d').'"') ->whereRaw('lastSeen = "'.now()->format('Y-m-d').'"')

View File

@@ -13,9 +13,6 @@ final class NewController extends BaseController
#[Route('/new', name: 'app_new')] #[Route('/new', name: 'app_new')]
public function __invoke(): Response public function __invoke(): Response
{ {
if($this->cache->getItem('list_new')->isHit()) {
return $this->render('productList.html.twig', ['listType' => 'new']);
}
$date = now()->modify('-30 days')->format('Y-m-d'); $date = now()->modify('-30 days')->format('Y-m-d');
$products = Product::where('created_at', '>', $date) $products = Product::where('created_at', '>', $date)
->with(['country', 'stock']) ->with(['country', 'stock'])

View File

@@ -19,10 +19,6 @@ final class ProductController extends BaseController
#[Route('/product/{productId<\d+>}', name: 'app_product')] #[Route('/product/{productId<\d+>}', name: 'app_product')]
public function __invoke(int $productId): Response public function __invoke(int $productId): Response
{ {
if ($this->cache->getItem('product' . $productId)->isHit()) {
return $this->render('product.html.twig', ['product' => ['id' => $productId]]);
}
$product = Product::with([ $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'), 'stock' => fn($query) => $query->orderBy('created_at', 'desc'),

View File

@@ -11,10 +11,6 @@ final class PromosController extends BaseController
#[Route('/promos/{promo?}', name: 'app_promos')] #[Route('/promos/{promo?}', name: 'app_promos')]
public function __invoke(?string $promo): Response public function __invoke(?string $promo): Response
{ {
if ($this->cache->getItem('list_promos')->isHit()) {
return $this->render('productList.html.twig', ['listType' => 'promos' . $promo]);
}
$products = Product::when(is_null($promo), fn($q) => $q->whereRaw('priceCurrent < productStandardPrice')) $products = Product::when(is_null($promo), fn($q) => $q->whereRaw('priceCurrent < productStandardPrice'))
->orderByDesc('starred') ->orderByDesc('starred')
->orderByDesc('created_by') ->orderByDesc('created_by')

View File

@@ -12,15 +12,12 @@ final class StarController extends BaseController
#[Route('/star/{productId<\d+>}', name: 'app_star')] #[Route('/star/{productId<\d+>}', name: 'app_star')]
public function __invoke(int $productId, Request $request): Response public function __invoke(int $productId, Request $request): Response
{ {
$this->cache->deleteItems(['list_all', 'list_promos', 'list_new', 'list_discontinued']);
$referer = $request->headers->get('referer'); $referer = $request->headers->get('referer');
if (str_contains($referer, '/category/')) { if (str_contains($referer, '/category/')) {
preg_match('#/category/(.*)#i', $referer, $matches); preg_match('#/category/(.*)#i', $referer, $matches);
$this->cache->deleteItem('list_category_'.urldecode($matches[1]));
} }
if (str_contains($referer, '/search?search=')) { if (str_contains($referer, '/search?search=')) {
preg_match('#/search\?search=(.*)#i', $referer, $matches); preg_match('#/search\?search=(.*)#i', $referer, $matches);
$this->cache->deleteItem('list_search_'.urldecode($matches[1]));
} }
Product::find($productId)->toggleStarred()->save(); Product::find($productId)->toggleStarred()->save();

View File

@@ -38,12 +38,7 @@ class Kernel extends BaseKernel
$services->set(Manager::class)->configurator([DatabaseFactory::class, 'create']); $services->set(Manager::class)->configurator([DatabaseFactory::class, 'create']);
$services->load('Krzysiej\\RyobiCrawler\\', __DIR__ ) $services->load('Krzysiej\\RyobiCrawler\\', __DIR__ )
->exclude('../src/{Models,Twig,DatabaseFactory.php,Kernel.php}'); ->exclude('../src/{Models,Twig,DatabaseFactory.php,Kernel.php}');
$services->set('twig.extension.cache', AppExtension::class)->tag('twig.extension'); $services->set('twig.extension', AppExtension::class)->tag('twig.extension');
$services->set(CacheExtension::class)->tag('twig.extension');
$services->set(FilesystemAdapter::class)->args([
'$directory' => __DIR__ . '/../var/cache/twig_blocks'
]);
$services->set('twig.runtime.cache', CacheRuntime::class)->args([new Reference(FilesystemAdapter::class)])->tag('twig.runtime');
} }
protected function configureRoutes(RoutingConfigurator $routes): void protected function configureRoutes(RoutingConfigurator $routes): void

View File

@@ -1,24 +1,30 @@
{% extends "template.html.twig" %} {% extends "template.html.twig" %}
{% block content %} {% block content %}
{% cache 'product' ~ product.id %}
<div class="table-responsive"> <div class="table-responsive">
<table class='table table-hover'> <table class='table table-hover'>
<tr> <tr>
<td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" <td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none"
href="{{ path('app_star', {'productId': product.id}) }}">{% if product.starred %}{% else %}{% endif %}</a></td> href="{{ path('app_star', {'productId': product.id}) }}">{% if product.starred %}{% else %}{% endif %}</a>
</td>
<td><img src='{{ product.image }}&width=150' class='border rounded p-1' alt='{{ product.name }}'/></td> <td><img src='{{ product.image }}&width=150' class='border rounded p-1' alt='{{ product.name }}'/></td>
<td> <td>
<a href='{{ path('app_product', {'productId': product.id}) }}' class="text-decoration-none">{{ product.name }}</a> <a href='{{ path('app_product', {'productId': product.id}) }}'
<span class="badge text-bg-light"><a href="{{ path('app_search', {'search': product.subTitle}) }}" class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span> class="text-decoration-none">{{ product.name }}</a>
{% if product.promotions is not null and product.promotions.hasPromotion %}<a href="{{ path('app_promos', {'promo': product.promotions.slug}) }}"><span class="badge bg-info">PROMO: {{ product.promotions.tag }}</span></a>{% endif %} <span class="badge text-bg-light"><a href="{{ path('app_search', {'search': product.subTitle}) }}"
class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span>
{% if product.promotions is not null and product.promotions.hasPromotion %}<a
href="{{ path('app_promos', {'promo': product.promotions.slug}) }}"><span class="badge bg-info">PROMO: {{ product.promotions.tag }}</span>
</a>{% endif %}
</td> </td>
<td> <td>
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';"> <nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';">
<ol class="breadcrumb"> <ol class="breadcrumb">
{% for category in product.categories %} {% for category in product.categories %}
<li class="breadcrumb-item" aria-current="page"><a class="breadcrumb-item text-decoration-none" <li class="breadcrumb-item" aria-current="page"><a
href="{{ path('app_category', {'category': category}) }}">{{ category }}</a></li> class="breadcrumb-item text-decoration-none"
href="{{ path('app_category', {'category': category}) }}">{{ category }}</a>
</li>
{% endfor %} {% endfor %}
</ol> </ol>
</nav> </nav>
@@ -133,6 +139,5 @@
} }
}); });
</script> </script>
{% endcache %}
{% endblock %} {% endblock %}

View File

@@ -1,8 +1,6 @@
{% extends "template.html.twig" %} {% extends "template.html.twig" %}
{% block content %} {% block content %}
{% cache 'list_' ~ listType %}
{% if listType starts with 'category_' %} {% if listType starts with 'category_' %}
{{ renderCategoryTree(categoryTree, category) | raw }} {{ renderCategoryTree(categoryTree, category) | raw }}
{% endif %} {% endif %}
@@ -93,5 +91,4 @@
</table> </table>
</div> </div>
{% endif %} {% endif %}
{% endcache %}
{% endblock %} {% endblock %}

View File

@@ -18,7 +18,6 @@
</button> </button>
<div class="collapse navbar-collapse" id="navbarNav"> <div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav me-auto mb-2 mb-lg-0"> <ul class="navbar-nav me-auto mb-2 mb-lg-0">
{% cache "menu_count" ~ listType|default('') %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link {% if app.request.pathinfo == path('app_home') %}active shadow-sm bg-body rounded{% endif %}" aria-current="page" href="{{ path('app_home') }}">All products <span class="badge text-bg-secondary">{{ allCount() }}</span></a> <a class="nav-link {% if app.request.pathinfo == path('app_home') %}active shadow-sm bg-body rounded{% endif %}" aria-current="page" href="{{ path('app_home') }}">All products <span class="badge text-bg-secondary">{{ allCount() }}</span></a>
</li> </li>
@@ -34,7 +33,6 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link {% if app.request.pathinfo == path('app_discontinued') %}active shadow-sm bg-body rounded{% endif %}" aria-current="page" href="{{ path('app_discontinued') }}">Discontinued <span class="badge text-bg-secondary">{{ discontinuedCount() }}</span></a> <a class="nav-link {% if app.request.pathinfo == path('app_discontinued') %}active shadow-sm bg-body rounded{% endif %}" aria-current="page" href="{{ path('app_discontinued') }}">Discontinued <span class="badge text-bg-secondary">{{ discontinuedCount() }}</span></a>
</li> </li>
{% endcache %}
</ul> </ul>
<form class="form-floating d-flex col-lg-6 col-sm-8" role="search" action="{{ path('app_search') }}"> <form class="form-floating d-flex col-lg-6 col-sm-8" role="search" action="{{ path('app_search') }}">