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')
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user