diff --git a/browser.php b/browser.php
index d2d3897..e2fba90 100644
--- a/browser.php
+++ b/browser.php
@@ -3,78 +3,32 @@
include_once 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
-use Krzysiej\RyobiCrawler\Models\Price;
use Krzysiej\RyobiCrawler\Models\Product;
+use Twig\{Environment, Loader\FilesystemLoader};
$capsule = new Capsule;
-$capsule->addConnection([
- 'driver' => 'sqlite',
- 'database' => __DIR__ . '/database.sqlite',
- 'prefix' => '',
-]);
+$capsule->addConnection(['driver' => 'sqlite', 'database' => __DIR__ . '/database.sqlite']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
-echo '';
+$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']);
- echo "back
";
- echo "
-
-
- | price |
- lowest product price in 30 days |
- standard price |
-
- ";
- /** @var Price $price */
- foreach ($product->price as $price) {
- echo "
- | $price->price |
- $price->lowestProductPrice30Days |
- $price->productStandardPrice |
- $price->created_at |
-
";
- }
- echo "
";
}
if (isset($_GET['category'])) {
- echo "back";
+ $template = 'productList.html.twig';
$products = Product::with('price')->selectRaw('products.*')->fromRaw('products, json_each(products.categories)')->whereRaw('json_each.value = ?', [$_GET['category']])->get();
- echo "";
- foreach ($products as $product) {
- echo "
-  |
- $product->name |
- $product->subTitle | ";
- foreach ($product->categories as $category) {
- echo '- ' . $category . '
';
- }
- echo " | link |
- {$product->price->last()->price} |
-
";
- }
- echo "
";
}
-if (!isset($_GET['product_id']) && !isset($_GET['category'])) {
+if (isset($_GET['search'])) {
+ $template = 'productList.html.twig';
+ $products = Product::with('price')
+ ->orWhere([['name', 'like', "%{$_GET['search']}%"]])
+ ->orWhere([['subTitle', 'like', "%{$_GET['search']}%"]])->get();
+}
+if (empty($_GET)) {
+ $template = 'productList.html.twig';
$products = Product::with('price')->get();
- echo "";
- foreach ($products as $product) {
- echo "
-  |
- $product->name |
- $product->subTitle | ";
- foreach ($product->categories as $category) {
- echo '- ' . $category . '
';
- }
- echo " | link |
- {$product->price->last()->price} |
-
";
- }
- echo "
";
}
+
+$twig->display($template, ['products' => $products, 'product' => $product, 'search' => $_GET['search']]);
diff --git a/composer.json b/composer.json
index 5245765..8d57d8e 100644
--- a/composer.json
+++ b/composer.json
@@ -3,7 +3,8 @@
"guzzlehttp/guzzle": "^7.0",
"symfony/var-dumper": "^7.0",
"illuminate/database": "^11.0",
- "ext-json": "*"
+ "ext-json": "*",
+ "twig/twig": "^3.0"
},
"autoload": {
"psr-4": {
diff --git a/composer.lock b/composer.lock
index 447239b..d241cb5 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": "2de5a90e6e774457aaf4cab180a00f16",
+ "content-hash": "ce7d9a1457a9f208ddadf1630309fecc",
"packages": [
{
"name": "brick/math",
@@ -1542,6 +1542,85 @@
],
"time": "2023-05-23T14:45:45+00:00"
},
+ {
+ "name": "symfony/polyfill-ctype",
+ "version": "v1.29.0",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/symfony/polyfill-ctype.git",
+ "reference": "ef4d7e442ca910c4764bce785146269b30cb5fc4"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/ef4d7e442ca910c4764bce785146269b30cb5fc4",
+ "reference": "ef4d7e442ca910c4764bce785146269b30cb5fc4",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=7.1"
+ },
+ "provide": {
+ "ext-ctype": "*"
+ },
+ "suggest": {
+ "ext-ctype": "For best performance"
+ },
+ "type": "library",
+ "extra": {
+ "thanks": {
+ "name": "symfony/polyfill",
+ "url": "https://github.com/symfony/polyfill"
+ }
+ },
+ "autoload": {
+ "files": [
+ "bootstrap.php"
+ ],
+ "psr-4": {
+ "Symfony\\Polyfill\\Ctype\\": ""
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Gert de Pagter",
+ "email": "BackEndTea@gmail.com"
+ },
+ {
+ "name": "Symfony Community",
+ "homepage": "https://symfony.com/contributors"
+ }
+ ],
+ "description": "Symfony polyfill for ctype functions",
+ "homepage": "https://symfony.com",
+ "keywords": [
+ "compatibility",
+ "ctype",
+ "polyfill",
+ "portable"
+ ],
+ "support": {
+ "source": "https://github.com/symfony/polyfill-ctype/tree/v1.29.0"
+ },
+ "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-01-29T20:11:03+00:00"
+ },
{
"name": "symfony/polyfill-mbstring",
"version": "v1.29.0",
@@ -2034,6 +2113,78 @@
],
"time": "2024-02-15T11:33:06+00:00"
},
+ {
+ "name": "twig/twig",
+ "version": "v3.8.0",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/twigphp/Twig.git",
+ "reference": "9d15f0ac07f44dc4217883ec6ae02fd555c6f71d"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/twigphp/Twig/zipball/9d15f0ac07f44dc4217883ec6ae02fd555c6f71d",
+ "reference": "9d15f0ac07f44dc4217883ec6ae02fd555c6f71d",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=7.2.5",
+ "symfony/polyfill-ctype": "^1.8",
+ "symfony/polyfill-mbstring": "^1.3",
+ "symfony/polyfill-php80": "^1.22"
+ },
+ "require-dev": {
+ "psr/container": "^1.0|^2.0",
+ "symfony/phpunit-bridge": "^5.4.9|^6.3|^7.0"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "Twig\\": "src/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "authors": [
+ {
+ "name": "Fabien Potencier",
+ "email": "fabien@symfony.com",
+ "homepage": "http://fabien.potencier.org",
+ "role": "Lead Developer"
+ },
+ {
+ "name": "Twig Team",
+ "role": "Contributors"
+ },
+ {
+ "name": "Armin Ronacher",
+ "email": "armin.ronacher@active-4.com",
+ "role": "Project Founder"
+ }
+ ],
+ "description": "Twig, the flexible, fast, and secure template language for PHP",
+ "homepage": "https://twig.symfony.com",
+ "keywords": [
+ "templating"
+ ],
+ "support": {
+ "issues": "https://github.com/twigphp/Twig/issues",
+ "source": "https://github.com/twigphp/Twig/tree/v3.8.0"
+ },
+ "funding": [
+ {
+ "url": "https://github.com/fabpot",
+ "type": "github"
+ },
+ {
+ "url": "https://tidelift.com/funding/github/packagist/twig/twig",
+ "type": "tidelift"
+ }
+ ],
+ "time": "2023-11-21T18:54:41+00:00"
+ },
{
"name": "voku/portable-ascii",
"version": "2.0.1",
@@ -2115,7 +2266,9 @@
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
- "platform": [],
+ "platform": {
+ "ext-json": "*"
+ },
"platform-dev": [],
"plugin-api-version": "2.3.0"
}
diff --git a/src/templates/product.html.twig b/src/templates/product.html.twig
new file mode 100644
index 0000000..23c3ce1
--- /dev/null
+++ b/src/templates/product.html.twig
@@ -0,0 +1,42 @@
+{% extends "template.html.twig" %}
+
+{% block content %}
+
+
+  |
+ {{ product.name }} |
+ {{ product.subTitle }} |
+
+
+ {% for category in product.categories %}
+ - {{ category }}
+ {% endfor %}
+
+ |
+ link |
+
+
+
+
+
+
+
+ | price |
+ lowest product price in 30 days |
+ standard price |
+
+
+ {% for price in product.price %}
+
+ | {{ price.price }} |
+ {{ price.lowestProductPrice30Days }} |
+ {{ price.productStandardPrice }} |
+ {{ price.created_at }} |
+
+ {% endfor %}
+
+ |
+
+
+{% endblock %}
+
diff --git a/src/templates/productList.html.twig b/src/templates/productList.html.twig
new file mode 100644
index 0000000..fea02b6
--- /dev/null
+++ b/src/templates/productList.html.twig
@@ -0,0 +1,22 @@
+{% extends "template.html.twig" %}
+
+{% block content %}
+
+ {% for product in products %}
+
+  |
+ {{ product.name }} |
+ {{ product.subTitle }} |
+
+
+ {% for category in product.categories %}
+ - {{ category }}
+ {% endfor %}
+
+ |
+ link |
+ {{ product.price.last.price }} |
+
+ {% endfor %}
+
+{% endblock %}
\ No newline at end of file
diff --git a/src/templates/template.html.twig b/src/templates/template.html.twig
new file mode 100644
index 0000000..484a50f
--- /dev/null
+++ b/src/templates/template.html.twig
@@ -0,0 +1,24 @@
+
+
+
+
+
+
+ Ryobi crawler
+
+
+
+
+{% block content %}{% endblock %}
+
+
+