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

[Bug]: siteInformation function from custom driver never gets called #1198

Open
menno-ll opened this issue Dec 16, 2024 · 2 comments
Open

[Bug]: siteInformation function from custom driver never gets called #1198

menno-ll opened this issue Dec 16, 2024 · 2 comments
Labels

Comments

@menno-ll
Copy link

menno-ll commented Dec 16, 2024

Platform

macOS

Operating system version

macOS Sequoia 15.1.1

System architecture

ARM64 (M1, M2, etc)

Herd Version

1.13.0 (Build: 37)

PHP Version

PHP 8.2.26

Bug description

I wanted to use a custom WordPress multisite driver.
I've copied mine over from my Valet installation, and this worked flawlessly.

Then, after reading the documentation I've tried to add the siteInformation function as documented at https://herd.laravel.com/docs/1/extending-herd/custom-drivers#customize-herds-behaviour.
However it seems like this function siteInformation is never called (used XDebug to check).

Steps to reproduce

Add a custom driver, using the following code:

<?php

namespace Valet\Drivers\Custom;

use Valet\Drivers\BasicValetDriver;

class WordPressMultisiteValetDriver extends BasicValetDriver {

    /**
     * @var string The public web directory, if deeper under the root directory
     */
    protected $public_dir = '';

     /**
     * Determine if the driver serves the request.
     */
    public function serves(string $sitePath, string $siteName, string $uri): bool
    {
        return file_exists($sitePath.'/wp-config.php') || file_exists($sitePath.'/wp-config-sample.php');
    }

    /**
     * Determine if the incoming request is for a static file.
     *
     * @param  string $sitePath
     * @param  string $siteName
     * @param  string $uri
     * @return string|false
     */
    public function isStaticFile(string $sitePath, string $siteName, string $uri) {
        $uri = $this->rewriteMultisite($sitePath, $uri);
        $sitePath = $this->realSitePath($sitePath);

        if ($this->isActualFile($staticFilePath = $sitePath . $uri)) {
            return $staticFilePath;
        }

        return false;
    }

    /**
     * Get the fully resolved path to the application's front controller.
     *
     * @param  string $sitePath
     * @param  string $siteName
     * @param  string $uri
     * @return string
     */
    public function frontControllerPath(string $sitePath, string $siteName, string $uri): string {
        $this->forceTrailingSlash($uri);
        
        $uri = $this->rewriteMultisite($sitePath, $uri);
        $sitePath = $this->realSitePath($sitePath);

        return parent::frontControllerPath(
            $sitePath,
            $siteName,
            $this->forceTrailingSlash($uri)
        );
    }

    /**
     * Translate the site path to the actual public directory
     *
     * @param $sitePath
     * @return string
     */
    protected function realSitePath(string $sitePath): string {
        if ($this->public_dir) {
            $sitePath .= $this->public_dir;
        }

        return $sitePath;
    }

    /**
     * Imitate the rewrite rules for a multisite .htaccess
     *
     * @param $sitePath
     * @param $uri
     * @return string
     */
    protected function rewriteMultisite( string $sitePath, string $uri ): string {
        if ( ! file_exists( $sitePath . '/wp-config.php' ) ) {
            return $uri;
        }

        if ( strpos( file_get_contents($sitePath . '/wp-config.php'), 'MULTISITE') !== false ) {
            if (preg_match('/^(.*)?(\/wp-(content|admin|includes).*)/', $uri, $matches)) {
                //RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
                $uri = $matches[2];
            } elseif (preg_match('/^(.*)?(\/.*\.php)$/', $uri, $matches)) {
                //RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
                $uri = $matches[2];
            }
        }
        return $uri;
    }

    /**
     * Redirect to uri with trailing slash.
     */
    private function forceTrailingSlash($uri): ?string
    {
        if (substr($uri, -1 * strlen('/wp-admin')) == '/wp-admin') {
            header('Location: '.$uri.'/');
            exit;
        }

        return $uri;
    }

    public function siteInformation(string $sitePath, string $phpBinary): array
    {

        echo 'hi';

        return [
            "Overview" => [
                "Site Name" => "Laravel Airport",
                "Runway operational" => true,
            ],
            "Flights" => [
                "Today" => 10,
                "Yesterday" => 5,
                "This week" => 22,
            ],
        ];
    }
}

Make a new project directory. And in there, install WordPress.

Relevant log output

No response

@issuelabeler issuelabeler bot added the macOS label Dec 16, 2024
@sschlein
Copy link
Member

What happens if you run herd site-information in this project? That should return a JSON string and is probabaly broken with your echo statement.

@sschlein sschlein added the needs more information Further information is requested label Dec 17, 2024
@menno-ll
Copy link
Author

menno-ll commented Dec 19, 2024

The output is the PHP array, but encoded to json.
And it seems valid.

image

Raw:

{"Overview":{"Site Name":"Laravel Airport","Runway operational":true},"Flights":{"Today":10,"Yesterday":5,"This week":22}}

@github-actions github-actions bot removed the needs more information Further information is requested label Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants