Skip to content

Commit

Permalink
Adding all files to git via Yireo Command
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Oct 25, 2024
0 parents commit 4127ce8
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Controller/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Yireo\MagewireRouter\Controller;

use Magento\Framework\App\Request\Http;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\RouterInterface;
use Magewirephp\Magewire\Controller\Post\LivewireFactory as ControllerFactory;

class Router implements RouterInterface
{
public function __construct(
private ControllerFactory $controllerFactory,
) {
}

public function match(RequestInterface $request)
{
/** @var Http $request */
if (false === strstr($request->getRequestUri(), 'magewire/post/livewire')) {
return false;
}

$request->setControllerModule('magewire');
$request->setModuleName('post');
$request->setActionName('livewire');
$request->setDispatched(true);

return $this->controllerFactory->create();
}
}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Yireo MagewireRouter

**This Magento 2 module adds a router specifically for Magewire requests, shaving off various milliseconds off every request.**

## Reasoning
By default, when a request to `magewire/post/livewire` is made, it loops through routers before hitting the `standard` router which maps the URL path to the right controller. This means that with every request, the `securitytxt` router, the `robots` router and the `urlrewrite` router are used. And the `urlrewrite` router adds an additional database query. This module adds a new router with ordering 1, preceeding all others.

## Benchmarks
Simple benchmarking with `time` and `curl` was used to determine the request time of Magewire with this module enabled and with this module disabled:
```bash
time curl -s -H 'Content-Type: application/json' -d '{}' -XPOST -S http://magento.local/default/magewire/post/livewire
```

In my case, without the module, request times are between 165ms and 200ms. With the module enabled, request times are between 155ms and 180ms.
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "yireo/module-magewire-router",
"version": "1.0.0",
"description": "N/A",
"type": "magento2-module",
"require": {
"magento/framework": "*"
},
"license": [
"Open Software License (OSL)"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Yireo\\MagewireRouter\\": ""
}
}
}
14 changes: 14 additions & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\App\RouterList">
<arguments>
<argument name="routerList" xsi:type="array">
<item name="magewire" xsi:type="array">
<item name="class" xsi:type="string">Yireo\MagewireRouter\Controller\Router</item>
<item name="disable" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="string">1</item>
</item>
</argument>
</arguments>
</type>
</config>
8 changes: 8 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Yireo_MagewireRouter">
<sequence>
<module name="Magewirephp_Magewire"/>
</sequence>
</module>
</config>
6 changes: 6 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Yireo_MagewireRouter', __DIR__);

0 comments on commit 4127ce8

Please sign in to comment.