{"id":15068634,"url":"https://github.com/jerowork/route-attribute-provider","last_synced_at":"2025-04-10T17:36:08.701Z","repository":{"id":43326402,"uuid":"319396503","full_name":"jerowork/route-attribute-provider","owner":"jerowork","description":"Define routes by PHP8 attributes.","archived":false,"fork":false,"pushed_at":"2024-07-31T17:59:36.000Z","size":71,"stargazers_count":5,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-20T14:53:10.456Z","etag":null,"topics":["attributes","php8","router"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jerowork.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-07T17:37:50.000Z","updated_at":"2025-01-16T13:10:23.000Z","dependencies_parsed_at":"2024-05-28T15:28:48.669Z","dependency_job_id":"e88387dc-c4b2-4e64-9564-b3e15ec84fcb","html_url":"https://github.com/jerowork/route-attribute-provider","commit_stats":{"total_commits":52,"total_committers":3,"mean_commits":"17.333333333333332","dds":0.07692307692307687,"last_synced_commit":"d2fd204024b3e62cf5935f42fbdaf259ffa949f4"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jerowork%2Froute-attribute-provider","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jerowork%2Froute-attribute-provider/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jerowork%2Froute-attribute-provider/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jerowork%2Froute-attribute-provider/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jerowork","download_url":"https://codeload.github.com/jerowork/route-attribute-provider/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248262032,"owners_count":21074230,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attributes","php8","router"],"created_at":"2024-09-25T01:38:40.031Z","updated_at":"2025-04-10T17:36:08.682Z","avatar_url":"https://github.com/jerowork.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# route-attribute-provider\n[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fjerowork%2Froute-attribute-provider%2Fbadge%3Fref%3Dmain\u0026style=flat-square)](https://github.com/jerowork/route-attribute-provider/actions)\n[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/jerowork/route-attribute-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/jerowork/route-attribute-provider/code-structure)\n[![Quality Score](https://img.shields.io/scrutinizer/g/jerowork/route-attribute-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/jerowork/route-attribute-provider)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)\n[![Packagist Version](https://img.shields.io/packagist/v/jerowork/route-attribute-provider.svg?style=flat-square\u0026include_prereleases)](https://packagist.org/packages/jerowork/route-attribute-provider)\n[![PHP Version](https://img.shields.io/badge/php-%5E8.1+-8892BF.svg?style=flat-square)](http://www.php.net)\n\nDefine routes by PHP8 [attributes]((https://stitcher.io/blog/attributes-in-php-8)).\n\n## Installation\nInstall via [Composer](https://getcomposer.org):\n```bash\n$ composer require jerowork/route-attribute-provider\n```\n\n## Configuration\nIn order to use route attributes, pick any of the [existing framework implementations](#existing-implementations) or create a custom one.\n\nInstantiate `RouteAttributeConfigurator` somewhere close to the construction of your application,\ne.g. in your front controller (or ideally register in your PSR-11 container).\n\nBasic configuration:\n```php\nuse Jerowork\\RouteAttributeProvider\\RouteAttributeConfigurator;\n\n// ...\n\n$routeConfigurator = new RouteAttributeConfigurator(\n    new CustomRouteProvider($router)  // Implementation of your choice\n);\n\n$routeConfigurator\n    -\u003eaddDirectory(sprintf('%s/src/Infrastructure/Api/Http/Action', __DIR__))\n    -\u003econfigure();\n\n// ...\n```\n\nExtended configuration:\n```php\nuse Jerowork\\FileClassReflector\\FileFinder\\RegexIterator\\RegexIteratorFileFinder;\nuse Jerowork\\FileClassReflector\\NikicParser\\NikicParserClassReflectorFactory;\nuse Jerowork\\RouteAttributeProvider\\RouteAttributeConfigurator;\nuse Jerowork\\RouteAttributeProvider\\RouteLoader\\ClassReflector\\ClassReflectorRouteLoader;\nuse PhpParser\\NodeTraverser;\nuse PhpParser\\ParserFactory;\n// ...\n\n// All parts of the configurator can be replaced with a custom implementation\n$routeConfigurator = new RouteAttributeConfigurator(\n    new CustomRouteProvider($router), // Implementation of your choice\n    new ClassReflectorRouteLoader(\n        new NikicParserClassReflectorFactory(\n            new RegexIteratorFileFinder(),\n            (new ParserFactory())-\u003ecreate(ParserFactory::PREFER_PHP7),\n            new NodeTraverser()\n        )\n    )\n);\n\n// Multiple directories can be defined\n$routeConfigurator\n    -\u003eaddDirectory(\n        sprintf('%s/src/Infrastructure/Api/Http/Action', __DIR__),\n        sprintf('%s/src/Other/Controller', __DIR__)\n    )\n    -\u003econfigure();\n\n// ...\n```\n\n### Existing implementations\n- [jerowork/slim-route-attribute-provider](https://github.com/jerowork/slim-route-attribute-provider) for [Slim](https://www.slimframework.com)\n- [brenoroosevelt/league-route-attribute-provider](https://github.com/brenoroosevelt/league-route-attribute-provider) for [League/Route](https://github.com/thephpleague/route)\n\nOr check [packagist](https://packagist.org/providers/jerowork/route-attribute-provider-implementation) for any other implementations.\n\n### Custom implementation\nCreate a custom implementation by using `RouteAttributeProviderInterface`.\n\nA (fictive) custom implementation:\n```php\nuse Jerowork\\RouteAttributeProvider\\Api\\Route;\nuse Jerowork\\RouteAttributeProvider\\RouteAttributeProviderInterface;\n\nfinal class CustomRouteProvider implements RouteAttributeProviderInterface\n{\n    private SomeRouter $router;\n    \n    public function __construct(SomeRouter $router)\n    {\n        $this-\u003erouter = $router;\n    }\n\n    public function configure(string $className,string $methodName, Route $route) : void\n    {\n        // Register rule at your router\n        $rule = $this-\u003erouter-\u003eaddRule(\n            $route-\u003egetMethods(),\n            $route-\u003egetPattern(),\n            $className.':'.$methodName,\n            $route-\u003egetName()\n        );\n\n        // Register optional middleware\n        foreach ($route-\u003egetMiddleware() as $middleware) {\n            $rule-\u003eaddMiddleware($middleware);\n        }\n    }\n}\n```\n\n### Cache\nBy default, caching of route attributes is disabled. This is fine for a development environment.\n\nHowever, for a production environment you should use a more efficient way of route attribute loading.\nTherefore you can use any [PSR-16](https://www.php-fig.org/psr/psr-16) cache implementation.\n\n```php\nuse Symfony\\Component\\Cache\\Adapter\\ApcuAdapter;\nuse Symfony\\Component\\Cache\\Psr16Cache;\n\n// ...\n\n// Enable route attribute caching with any PSR-16 implementation (e.g. symfony/cache)\n$routeConfigurator-\u003eenableCache(new Psr16Cache(new ApcuAdapter()));\n\n// ...\n```\n\n**Note:** Any PSR-6 cache implementation can be used too, by using Symfony's [PSR-6 to PSR-16 adapter](https://symfony.com/doc/current/components/cache/psr6_psr16_adapters.html).\n\n## Usage\nA route can be defined via PHP8 attributes.\n\nMinimalist example:\n\n```php\nuse Jerowork\\RouteAttributeProvider\\Api\\Route;\nuse Psr\\Http\\Message\\ResponseInterface as ServerRequest;\nuse Psr\\Http\\Message\\ServerRequestInterface as Response;\n\nfinal class RootAction\n{\n    #[Route('/root')]\n    public function __invoke(ServerRequest $request, Response $response) : Response\n    {\n        return $response;\n    }\n}\n```\n\nExtended example:\n\n```php\nuse Jerowork\\RouteAttributeProvider\\Api\\RequestMethod;\nuse Jerowork\\RouteAttributeProvider\\Api\\Route;\nuse Psr\\Http\\Message\\ResponseInterface as ServerRequest;\nuse Psr\\Http\\Message\\ServerRequestInterface as Response;\n\nfinal class RootAction\n{\n    #[Route('/root', method: RequestMethod::GET, name: 'root', middleware: SomeMiddleware::class)]\n    public function __invoke(ServerRequest $request, Response $response) : Response\n    {\n        return $response;\n    }\n}\n```\n\nFull-fledged example:\n\n```php\nuse Jerowork\\RouteAttributeProvider\\Api\\RequestMethod;\nuse Jerowork\\RouteAttributeProvider\\Api\\Route;\nuse Psr\\Http\\Message\\ResponseInterface as ServerRequest;\nuse Psr\\Http\\Message\\ServerRequestInterface as Response;\n\nfinal class RootAction\n{\n    #[Route('/root',\n        method: [RequestMethod::GET, RequestMethod::POST],\n        name: 'root',\n        middleware: [\n            SomeMiddleware::class,\n            AnotherMiddleware::class,\n        ],\n        host: 'localhost',\n        schemes: ['http', 'https'],\n        httpPort: 8888,\n        httpsPort: 9999,\n        options: ['strategy' =\u003e 'something']\n    )]\n    #[Route('/second-route',\n        method: RequestMethod::DELETE,\n        name: 'second-route'\n    )]\n    public function __invoke(ServerRequest $request, Response $response) : Response\n    {\n        return $response;\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjerowork%2Froute-attribute-provider","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjerowork%2Froute-attribute-provider","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjerowork%2Froute-attribute-provider/lists"}