Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

route loader #4

Closed
bnomei opened this issue Apr 20, 2022 · 0 comments
Closed

route loader #4

bnomei opened this issue Apr 20, 2022 · 0 comments
Assignees

Comments

@bnomei
Copy link
Owner

bnomei commented Apr 20, 2022

from @tobimori via discord

this is basically everything needed (i use the same finder component), you can optimise it for your code

    $finder = (new Finder())->files()
      ->name(static::PHP)
      ->in($dir);

    foreach ($finder as $file) {
      $pattern = strtolower($file->getRelativePathname());
      $pattern = preg_replace('~(.*)' . preg_quote('.php', '~') . '~', '$1' . '', $pattern, 1); // replace extension at end
      $pattern = preg_replace('~(.*)' . preg_quote('index', '~') . '~', '$1' . '', $pattern, 1); // replace index at end, for root of folder, but not in paths etc.

      $route = require $file->getRealPath();

      // check if return is actually an array (if additional stuff is specified, e.g. method or language) or returns a function
      if (is_array($route) || $route instanceof \Closure) {
        $this->registry[] = array_merge(
          [
            'pattern' => '/' . $pattern,
            'action' => $route instanceof \Closure ? $route : null
          ],
          is_array($route) ? $route : []
        );
      }
    }
@bnomei bnomei self-assigned this Apr 20, 2022
@bnomei bnomei closed this as completed Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant