Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ All notable changes are documented here. The format is based on [Keep a Changelo
### Changed

- Faster item storage, lookup, removal and JSON serialization in `Phalcon\Support\Collection`. [#17683](https://github.com/phalcon/cphalcon/issues/17683)
- Faster route index build in `Phalcon\Mvc\Router` for routes with no host name. [#17693](https://github.com/phalcon/cphalcon/issues/17693)

### Added

Expand Down
57 changes: 32 additions & 25 deletions ext/phalcon/mvc/router/route.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 7 additions & 18 deletions ext/phalcon/support/registry.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions phalcon/Mvc/Router/Route.zep
Original file line number Diff line number Diff line change
Expand Up @@ -584,15 +584,22 @@ class Route implements RouteInterface
* Returns the data that the router needs for its method index, with
* one call: the HTTP methods, the compiled pattern, the host name, the
* compiled host name, the beforeMatch callback and the route id, in
* this order.
* this order. A route with no host name has no compiled host name, so
* there is no getCompiledHostName() call for it.
*/
public function getIndexData() -> array
{
var compiledHostName = null;

if this->hostname !== null {
let compiledHostName = this->getCompiledHostName();
}

return [
this->methods,
this->compiledPattern,
this->hostname,
this->getCompiledHostName(),
compiledHostName,
this->beforeMatch,
this->routeId
];
Expand Down
25 changes: 7 additions & 18 deletions phalcon/Support/Registry.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use Traversable;
* available throughout your application.
*
*```php
* $registry = new \Phalcon\Registry();
* $registry = new \Phalcon\Support\Registry();
*
* // Set value
* $registry->something = "something";
Expand All @@ -41,24 +41,13 @@ use Traversable;
* unset($registry["something"]);
*```
*
* In addition to ArrayAccess, Phalcon\Registry also implements Countable
* (count($registry) will return the number of elements in the registry),
* Serializable and Iterator (you can iterate over the registry using a foreach
* loop) interfaces. For PHP 5.4 and higher, JsonSerializable interface is
* implemented.
* Phalcon\Support\Registry extends Phalcon\Support\Collection. It implements
* ArrayAccess, Countable, IteratorAggregate and JsonSerializable. Use
* count($registry) to get the number of elements, and use foreach to iterate
* over the registry.
*
* Phalcon\Registry is very fast (it is typically faster than any userspace
* implementation of the registry); however, this comes at a price:
* Phalcon\Registry is a final class and cannot be inherited from.
*
* Though Phalcon\Registry exposes methods like __get(), offsetGet(), count() etc,
* it is not recommended to invoke them manually (these methods exist mainly to
* match the interfaces the registry implements): $registry->__get("property")
* is several times slower than $registry->property.
*
* Internally all the magic methods (and interfaces except JsonSerializable)
* are implemented using object handlers or similar techniques: this allows to
* bypass relatively slow method calls.
* The keys are not case-sensitive. Phalcon\Support\Registry is a final class.
* You cannot extend it.
*
* @extends Collection<mixed>
*/
Expand Down
79 changes: 79 additions & 0 deletions tests/unit/Mvc/Router/Route/GetIndexDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,28 @@

namespace Phalcon\Tests\Unit\Mvc\Router\Route;

use Phalcon\Events\Manager as EventsManager;
use Phalcon\Mvc\Router\Route;
use Phalcon\Talon\PHPUnit\AbstractUnitTestCase;
use Phalcon\Tests\Unit\Mvc\Fake\RouterTrait;
use PHPUnit\Framework\Attributes\BackupGlobals;
use PHPUnit\Framework\Attributes\DataProvider;

final class GetIndexDataTest extends AbstractUnitTestCase
{
use RouterTrait;

/**
* @return array<string, array{0: bool}>
*/
public static function getEventsManagerCases(): array
{
return [
'no events manager' => [false],
'events manager' => [true],
];
}

/**
* getIndexData() returns the values of the six getters, in order.
*
Expand All @@ -43,6 +60,50 @@ public function testMvcRouterRouteGetIndexData(): void
);
}

/**
* getIndexData() does not call getCompiledHostName() for a route with no
* host name. A subclass that overrides getCompiledHostName() gets null.
*
* @author Phalcon Team <team@phalcon.io>
* @since 2026-10-26
*/
public function testMvcRouterRouteGetIndexDataNoHostnameNoCompiledHostNameCall(): void
{
$route = $this->getNoHostnameRoute();

$actual = $route->getIndexData();

$this->assertNull($actual[3]);
$this->assertSame(0, $route->calls);
}

/**
* The router does not read the compiled host name of a route with no
* host name: the route matches with any host, with and without an
* events manager.
*
* @author Phalcon Team <team@phalcon.io>
* @since 2026-10-26
*/
#[BackupGlobals(true)]
#[DataProvider('getEventsManagerCases')]
public function testMvcRouterRouteGetIndexDataNoHostnameRoutingUnchanged(bool $events): void
{
$route = $this->getNoHostnameRoute();
$router = $this->getRouter(false);
if ($events) {
$router->setEventsManager(new EventsManager());
}
$router->attach($route);

$_SERVER['HTTP_HOST'] = 'www.other.com';
$_SERVER['REQUEST_METHOD'] = 'GET';
$router->handle('/users');

$this->assertTrue($router->wasMatched());
$this->assertSame($route, $router->getMatchedRoute());
}

/**
* getIndexData() returns the compiled host name and the beforeMatch
* callback, and the compiled host name stays in the route.
Expand Down Expand Up @@ -72,4 +133,22 @@ public function testMvcRouterRouteGetIndexDataWithHostnameAndBeforeMatch(): void
);
$this->assertSame($actual[3], $route->getCompiledHostName());
}

/**
* A route with no host name. Its getCompiledHostName() counts the calls
* and returns a host regex.
*/
private function getNoHostnameRoute(): Route
{
return new class ('/users', ['controller' => 'users']) extends Route {
public int $calls = 0;

public function getCompiledHostName(): ?string
{
$this->calls++;

return '#^api\.example\.com$#i';
}
};
}
}
Loading