Split browser.php to commands with routes.
This commit is contained in:
63
browser.php
63
browser.php
@@ -3,40 +3,45 @@
|
||||
include_once 'vendor/autoload.php';
|
||||
|
||||
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 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')) {
|
||||
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;
|
||||
$capsule->addConnection(['driver' => 'sqlite', 'database' => __DIR__ . '/database.sqlite']);
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$loader = new FilesystemLoader(__DIR__ . '/src/templates');
|
||||
$twig = new Environment($loader);
|
||||
if (isset($_GET['product_id'])) {
|
||||
$template = 'product.html.twig';
|
||||
$product = Product::with('price')->find($_GET['product_id']);
|
||||
$routes->add('product_show', $productRoute);
|
||||
$routes->add('search_show', $searchRoute);
|
||||
$routes->add('category_show', $categoryRoute);
|
||||
$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());
|
||||
}
|
||||
if (isset($_GET['category'])) {
|
||||
$template = 'productList.html.twig';
|
||||
$products = Product::with('price')->selectRaw('products.*')->fromRaw('products, json_each(products.categories)')->whereRaw('json_each.value = ?', [$_GET['category']])
|
||||
->orderByDesc('starred')->orderByDesc('created_by')->get();
|
||||
}
|
||||
if (isset($_GET['search'])) {
|
||||
$template = 'productList.html.twig';
|
||||
$products = Product::with('price')
|
||||
->orWhere([['name', 'like', "%{$_GET['search']}%"]])
|
||||
->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'] ?? '']);
|
||||
|
||||
//dd($parameters['category_name']);
|
||||
|
||||
match ($parameters['_controller']) {
|
||||
SearchController::class => (new $parameters['_controller']())($parameters['search_term']),
|
||||
CategoryController::class => (new $parameters['_controller']())($parameters['category_name']),
|
||||
ProductController::class => (new $parameters['_controller']())($parameters['product_id']),
|
||||
IndexController::class => (new $parameters['_controller']())(),
|
||||
default => throw new Exception('Route not found')
|
||||
};
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
"illuminate/database": "11.26.0.0",
|
||||
"ext-json": "*",
|
||||
"twig/twig": "3.14.0.0",
|
||||
"symfony/console": "^7.0"
|
||||
"symfony/console": "^7.0",
|
||||
"symfony/routing": "^7.1",
|
||||
"laravel/serializable-closure": "^1.3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
||||
146
composer.lock
generated
146
composer.lock
generated
@@ -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": "e9489916095593f696613b706467239b",
|
||||
"content-hash": "0857f170e12b9885e20a632ba9c5d530",
|
||||
"packages": [
|
||||
{
|
||||
"name": "brick/math",
|
||||
@@ -942,6 +942,67 @@
|
||||
},
|
||||
"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",
|
||||
"version": "3.8.0",
|
||||
@@ -2108,6 +2169,87 @@
|
||||
],
|
||||
"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",
|
||||
"version": "v3.5.0",
|
||||
@@ -2697,5 +2839,5 @@
|
||||
"ext-json": "*"
|
||||
},
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.3.0"
|
||||
"plugin-api-version": "2.6.0"
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ class ScrapeWebsite extends Command
|
||||
$products = $this->getProducts();
|
||||
$progress->setMaxSteps(count($products));
|
||||
foreach ($products as $product) {
|
||||
//dd($product);
|
||||
$this->saveProduct($product);
|
||||
$progress->advance();
|
||||
}
|
||||
@@ -80,11 +81,13 @@ class ScrapeWebsite extends Command
|
||||
{
|
||||
/** @var Product $productModel */
|
||||
$productModel = Product::firstOrNew(['skuID' => $product->skuID]);
|
||||
//dd($productModel);
|
||||
|
||||
$productModel->skuID = $product->skuID;
|
||||
$productModel->name = $product->name;
|
||||
$productModel->availableQuantity = $product->availableQuantity;
|
||||
$productModel->stock = $product->stock;
|
||||
$productModel->categories = json_encode($product->categories);
|
||||
$productModel->categories = $product->categories;
|
||||
$productModel->image = $product->image;
|
||||
$productModel->subTitle = $product->subTitle;
|
||||
$productModel->variantCode = $product->variantCode;
|
||||
@@ -92,7 +95,8 @@ class ScrapeWebsite extends Command
|
||||
$productModel->url = $product->url;
|
||||
$productModel->save();
|
||||
$priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
||||
if (!$priceExists) {
|
||||
|
||||
if (false === $priceExists) {
|
||||
$price = new Price();
|
||||
$price->price = $product->productPrice;
|
||||
$price->productStandardPrice = $product->productStandardPrice;
|
||||
@@ -100,7 +104,7 @@ class ScrapeWebsite extends Command
|
||||
$productModel->price()->save($price);
|
||||
}
|
||||
$stockExist = $productModel->stock()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
||||
if (!$stockExist) {
|
||||
if (false === $stockExist) {
|
||||
$stock = new Stock();
|
||||
$stock->availableQuantity = $product->availableQuantity;
|
||||
$stock->stock = $product->stock;
|
||||
|
||||
22
src/Controller/BaseController.php
Normal file
22
src/Controller/BaseController.php
Normal 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);
|
||||
}
|
||||
}
|
||||
20
src/Controller/CategoryController.php
Normal file
20
src/Controller/CategoryController.php
Normal 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]);
|
||||
}
|
||||
}
|
||||
14
src/Controller/IndexController.php
Normal file
14
src/Controller/IndexController.php
Normal 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]);
|
||||
}
|
||||
}
|
||||
14
src/Controller/ProductController.php
Normal file
14
src/Controller/ProductController.php
Normal 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 ?? []]);
|
||||
}
|
||||
}
|
||||
17
src/Controller/SearchController.php
Normal file
17
src/Controller/SearchController.php
Normal 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]);
|
||||
}
|
||||
}
|
||||
@@ -39,10 +39,11 @@ class Product extends Model
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function categories(): Attribute
|
||||
public function categories(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn(string $value) => array_reverse(json_decode($value, 1)),
|
||||
set: fn(array $value) => json_encode($value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
<table class='table table-hover'>
|
||||
<tr>
|
||||
<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>
|
||||
<ul class='nav'>
|
||||
{% 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 %}
|
||||
</ul>
|
||||
</td>
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
<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><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">
|
||||
<ul class='nav'>
|
||||
{% 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 %}
|
||||
</ul>
|
||||
</td>
|
||||
|
||||
Reference in New Issue
Block a user