Split browser.php to commands with routes.

This commit is contained in:
Krzysztof Płaczek
2024-10-13 20:44:14 +02:00
parent e13a0acce1
commit f2e6cba2f5
12 changed files with 281 additions and 40 deletions

View File

@@ -3,40 +3,45 @@
include_once 'vendor/autoload.php'; include_once 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Capsule\Manager as Capsule;
use Krzysiej\RyobiCrawler\Controller\CategoryController;
use Krzysiej\RyobiCrawler\Controller\IndexController;
use Krzysiej\RyobiCrawler\Controller\ProductController;
use Krzysiej\RyobiCrawler\Controller\SearchController;
use Krzysiej\RyobiCrawler\Models\Product; use Krzysiej\RyobiCrawler\Models\Product;
use Twig\{Environment, Loader\FilesystemLoader}; use Twig\{Environment, Loader\FilesystemLoader};
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
if (!file_exists('database.sqlite')) { if (!file_exists('database.sqlite')) {
exit('Database file <code>database.sqlite</code> missing. Run docker compose <blockquote>docker compose exec php-app php index.php app:migrate</blockquote> to create it.'); exit('Database file <code>database.sqlite</code> missing. Run docker compose <blockquote>docker compose exec php-app php index.php app:migrate</blockquote> to create it.');
} }
$productRoute = new Route('browser.php/product/{product_id}', ['_controller' => ProductController::class], ['product_id' => '\d+']);
$searchRoute = new Route('browser.php?search={search_term}', ['_controller' => SearchController::class]);
$categoryRoute = new Route('/browser.php/category/{category_name}', ['_controller' => CategoryController::class]);
$indexRoute = new Route('browser.php', ['_controller' => IndexController::class]);
$routes = new RouteCollection();
$capsule = new Capsule; $routes->add('product_show', $productRoute);
$capsule->addConnection(['driver' => 'sqlite', 'database' => __DIR__ . '/database.sqlite']); $routes->add('search_show', $searchRoute);
$capsule->setAsGlobal(); $routes->add('category_show', $categoryRoute);
$capsule->bootEloquent(); $routes->add('index_show', $indexRoute);
$loader = new FilesystemLoader(__DIR__ . '/src/templates');
$twig = new Environment($loader); $context = new RequestContext();
if (isset($_GET['product_id'])) { $matcher = new UrlMatcher($routes, $context);
$template = 'product.html.twig'; try {
$product = Product::with('price')->find($_GET['product_id']); $parameters = $matcher->match($_SERVER['REQUEST_URI']);
} catch (Exception $e) {
die($e->getMessage());
} }
if (isset($_GET['category'])) {
$template = 'productList.html.twig'; //dd($parameters['category_name']);
$products = Product::with('price')->selectRaw('products.*')->fromRaw('products, json_each(products.categories)')->whereRaw('json_each.value = ?', [$_GET['category']])
->orderByDesc('starred')->orderByDesc('created_by')->get(); match ($parameters['_controller']) {
} SearchController::class => (new $parameters['_controller']())($parameters['search_term']),
if (isset($_GET['search'])) { CategoryController::class => (new $parameters['_controller']())($parameters['category_name']),
$template = 'productList.html.twig'; ProductController::class => (new $parameters['_controller']())($parameters['product_id']),
$products = Product::with('price') IndexController::class => (new $parameters['_controller']())(),
->orWhere([['name', 'like', "%{$_GET['search']}%"]]) default => throw new Exception('Route not found')
->orWhere([['subTitle', 'like', "%{$_GET['search']}%"]])->get(); };
}
if (isset($_GET['star'])) {
Product::find($_GET['star'])->toggleStarred()->save();
header('Location: '.$_SERVER['HTTP_REFERER']);
}
if (empty($_GET)) {
$template = 'productList.html.twig';
$products = Product::with('price')->orderByDesc('starred')->orderByDesc('created_by')->get();
}
$twig->display($template, ['products' => $products ?? [], 'product' => $product ?? [], 'search' => $_GET['search'] ?? '']);

View File

@@ -5,7 +5,9 @@
"illuminate/database": "11.26.0.0", "illuminate/database": "11.26.0.0",
"ext-json": "*", "ext-json": "*",
"twig/twig": "3.14.0.0", "twig/twig": "3.14.0.0",
"symfony/console": "^7.0" "symfony/console": "^7.0",
"symfony/routing": "^7.1",
"laravel/serializable-closure": "^1.3"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

146
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "e9489916095593f696613b706467239b", "content-hash": "0857f170e12b9885e20a632ba9c5d530",
"packages": [ "packages": [
{ {
"name": "brick/math", "name": "brick/math",
@@ -942,6 +942,67 @@
}, },
"time": "2024-10-08T18:54:07+00:00" "time": "2024-10-08T18:54:07+00:00"
}, },
{
"name": "laravel/serializable-closure",
"version": "v1.3.5",
"source": {
"type": "git",
"url": "https://github.com/laravel/serializable-closure.git",
"reference": "1dc4a3dbfa2b7628a3114e43e32120cce7cdda9c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/serializable-closure/zipball/1dc4a3dbfa2b7628a3114e43e32120cce7cdda9c",
"reference": "1dc4a3dbfa2b7628a3114e43e32120cce7cdda9c",
"shasum": ""
},
"require": {
"php": "^7.3|^8.0"
},
"require-dev": {
"illuminate/support": "^8.0|^9.0|^10.0|^11.0",
"nesbot/carbon": "^2.61|^3.0",
"pestphp/pest": "^1.21.3",
"phpstan/phpstan": "^1.8.2",
"symfony/var-dumper": "^5.4.11|^6.2.0|^7.0.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Laravel\\SerializableClosure\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
},
{
"name": "Nuno Maduro",
"email": "nuno@laravel.com"
}
],
"description": "Laravel Serializable Closure provides an easy and secure way to serialize closures in PHP.",
"keywords": [
"closure",
"laravel",
"serializable"
],
"support": {
"issues": "https://github.com/laravel/serializable-closure/issues",
"source": "https://github.com/laravel/serializable-closure"
},
"time": "2024-09-23T13:33:08+00:00"
},
{ {
"name": "nesbot/carbon", "name": "nesbot/carbon",
"version": "3.8.0", "version": "3.8.0",
@@ -2108,6 +2169,87 @@
], ],
"time": "2024-09-09T11:45:10+00:00" "time": "2024-09-09T11:45:10+00:00"
}, },
{
"name": "symfony/routing",
"version": "v7.1.4",
"source": {
"type": "git",
"url": "https://github.com/symfony/routing.git",
"reference": "1500aee0094a3ce1c92626ed8cf3c2037e86f5a7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/routing/zipball/1500aee0094a3ce1c92626ed8cf3c2037e86f5a7",
"reference": "1500aee0094a3ce1c92626ed8cf3c2037e86f5a7",
"shasum": ""
},
"require": {
"php": ">=8.2",
"symfony/deprecation-contracts": "^2.5|^3"
},
"conflict": {
"symfony/config": "<6.4",
"symfony/dependency-injection": "<6.4",
"symfony/yaml": "<6.4"
},
"require-dev": {
"psr/log": "^1|^2|^3",
"symfony/config": "^6.4|^7.0",
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/expression-language": "^6.4|^7.0",
"symfony/http-foundation": "^6.4|^7.0",
"symfony/yaml": "^6.4|^7.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Symfony\\Component\\Routing\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Maps an HTTP request to a set of configuration variables",
"homepage": "https://symfony.com",
"keywords": [
"router",
"routing",
"uri",
"url"
],
"support": {
"source": "https://github.com/symfony/routing/tree/v7.1.4"
},
"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-08-29T08:16:25+00:00"
},
{ {
"name": "symfony/service-contracts", "name": "symfony/service-contracts",
"version": "v3.5.0", "version": "v3.5.0",
@@ -2697,5 +2839,5 @@
"ext-json": "*" "ext-json": "*"
}, },
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.3.0" "plugin-api-version": "2.6.0"
} }

View File

@@ -40,6 +40,7 @@ class ScrapeWebsite extends Command
$products = $this->getProducts(); $products = $this->getProducts();
$progress->setMaxSteps(count($products)); $progress->setMaxSteps(count($products));
foreach ($products as $product) { foreach ($products as $product) {
//dd($product);
$this->saveProduct($product); $this->saveProduct($product);
$progress->advance(); $progress->advance();
} }
@@ -80,11 +81,13 @@ class ScrapeWebsite extends Command
{ {
/** @var Product $productModel */ /** @var Product $productModel */
$productModel = Product::firstOrNew(['skuID' => $product->skuID]); $productModel = Product::firstOrNew(['skuID' => $product->skuID]);
//dd($productModel);
$productModel->skuID = $product->skuID; $productModel->skuID = $product->skuID;
$productModel->name = $product->name; $productModel->name = $product->name;
$productModel->availableQuantity = $product->availableQuantity; $productModel->availableQuantity = $product->availableQuantity;
$productModel->stock = $product->stock; $productModel->stock = $product->stock;
$productModel->categories = json_encode($product->categories); $productModel->categories = $product->categories;
$productModel->image = $product->image; $productModel->image = $product->image;
$productModel->subTitle = $product->subTitle; $productModel->subTitle = $product->subTitle;
$productModel->variantCode = $product->variantCode; $productModel->variantCode = $product->variantCode;
@@ -92,7 +95,8 @@ class ScrapeWebsite extends Command
$productModel->url = $product->url; $productModel->url = $product->url;
$productModel->save(); $productModel->save();
$priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists(); $priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
if (!$priceExists) {
if (false === $priceExists) {
$price = new Price(); $price = new Price();
$price->price = $product->productPrice; $price->price = $product->productPrice;
$price->productStandardPrice = $product->productStandardPrice; $price->productStandardPrice = $product->productStandardPrice;
@@ -100,7 +104,7 @@ class ScrapeWebsite extends Command
$productModel->price()->save($price); $productModel->price()->save($price);
} }
$stockExist = $productModel->stock()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists(); $stockExist = $productModel->stock()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
if (!$stockExist) { if (false === $stockExist) {
$stock = new Stock(); $stock = new Stock();
$stock->availableQuantity = $product->availableQuantity; $stock->availableQuantity = $product->availableQuantity;
$stock->stock = $product->stock; $stock->stock = $product->stock;

View File

@@ -0,0 +1,22 @@
<?php
namespace Krzysiej\RyobiCrawler\Controller;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Illuminate\Database\Capsule\Manager as Capsule;
class BaseController
{
protected Environment $twig;
public function __construct()
{
$capsule = new Capsule;
$capsule->addConnection(['driver' => 'sqlite', 'database' => __DIR__ . '/../../database.sqlite']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
$loader = new FilesystemLoader( __DIR__ . '/../../src/templates');
$this->twig = new Environment($loader);
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Krzysiej\RyobiCrawler\Controller;
use Krzysiej\RyobiCrawler\Models\Product;
final class CategoryController extends BaseController
{
public function __invoke(string $category): void
{
$products = Product::with('price')
->selectRaw('products.*')
->fromRaw('products, json_each(products.categories)')
->whereRaw('json_each.value = ?', [$category])
->orderByDesc('starred')
->orderByDesc('created_by')
->get();
$this->twig->display('productList.html.twig', ['products' => $products]);
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Krzysiej\RyobiCrawler\Controller;
use Krzysiej\RyobiCrawler\Models\Product;
final class IndexController extends BaseController
{
public function __invoke(): void
{
$products = Product::with('price')->orderByDesc('starred')->orderByDesc('created_by')->get();
$this->twig->display('productList.html.twig', ['products' => $products]);
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Krzysiej\RyobiCrawler\Controller;
use Krzysiej\RyobiCrawler\Models\Product;
final class ProductController extends BaseController
{
public function __invoke(int $productId): void
{
$product = Product::with('price')->find($productId);
$this->twig->display('product.html.twig', ['product' => $product ?? []]);
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Krzysiej\RyobiCrawler\Controller;
use Krzysiej\RyobiCrawler\Models\Product;
final class SearchController extends BaseController
{
public function __invoke(string $search): void
{
$products = Product::with('price')
->orWhere([['name', 'like', "%$search%"]])
->orWhere([['subTitle', 'like', "%$search%"]])->get();
$this->twig->display('productList.html.twig', ['products' => $products ?? [], 'search' => $search]);
}
}

View File

@@ -39,10 +39,11 @@ class Product extends Model
return $this; return $this;
} }
protected function categories(): Attribute public function categories(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn(string $value) => array_reverse(json_decode($value, 1)), get: fn(string $value) => array_reverse(json_decode($value, 1)),
set: fn(array $value) => json_encode($value),
); );
} }
} }

View File

@@ -4,12 +4,12 @@
<table class='table table-hover'> <table class='table table-hover'>
<tr> <tr>
<td><img src='{{ product.image }}&width=150' class='img-fluid' alt='{{ product.name }}'/></td> <td><img src='{{ product.image }}&width=150' class='img-fluid' alt='{{ product.name }}'/></td>
<td><a href='?product_id={{ product.id }}'>{{ product.name }}</a></td> <td><a href='/browser.php/product/{{ product.id }}'>{{ product.name }}</a></td>
<td>{{ product.subTitle }}</td> <td>{{ product.subTitle }}</td>
<td> <td>
<ul class='nav'> <ul class='nav'>
{% for category in product.categories %} {% for category in product.categories %}
<li class="nav-item"><a class="nav-link" href="?category={{ category }}"> {{ category }} </a></li> <li class="nav-item"><a class="nav-link" href="/browser.php/category/{{ category }}"> {{ category }} </a></li>
{% endfor %} {% endfor %}
</ul> </ul>
</td> </td>

View File

@@ -6,12 +6,12 @@
<tr> <tr>
<td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" href="?star={{ product.id }}">{% if product.starred %}{% else %}{% endif %}</a></td> <td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" href="?star={{ product.id }}">{% if product.starred %}{% else %}{% endif %}</a></td>
<td><img src='{{ product.image }}&width=70' class='img-fluid' alt='{{ product.name }}'/></td> <td><img src='{{ product.image }}&width=70' class='img-fluid' alt='{{ product.name }}'/></td>
<td class="align-middle"><a href='?product_id={{ product.id }}'>{{ product.name }}</a></td> <td class="align-middle"><a href='/browser.php/product/{{ product.id }}'>{{ product.name }}</a></td>
<td class="align-middle">{{ product.subTitle }}</td> <td class="align-middle">{{ product.subTitle }}</td>
<td class="align-middle"> <td class="align-middle">
<ul class='nav'> <ul class='nav'>
{% for category in product.categories %} {% for category in product.categories %}
<li class="nav-item"><a class="nav-link" href="?category={{ category }}"> {{ category }} </a></li> <li class="nav-item"><a class="nav-link" href="/browser.php/category/{{ category }}"> {{ category }} </a></li>
{% endfor %} {% endfor %}
</ul> </ul>
</td> </td>