Rareloop / lumberjack

Lumberjack is a powerful MVC framework for the modern WordPress developer. Write better, more expressive and easier to maintain code.
https://lumberjack.rareloop.com
MIT License
371 stars 34 forks source link

Cant get a setup to work with Laravel Herd on Windows (even with a custom driver) #36

Open kyloknight opened 3 weeks ago

kyloknight commented 3 weeks ago

I am using Laravel Herd on Windows 11 Pro to host all my local sites. Laravel, default WordPress as well as Themosis Framework installs work fine. Its only Lumberjack that I cant seem to configure correctly. I think this will be useful for a lot of users, as Laravel Herd is growing in popularity (everyone at our company uses it, as a lot of developers even on codeable.io).

I tried creating a custom Valet driver:

<?php 

namespace Valet\Drivers\Custom;

use Valet\Drivers\LaravelValetDriver;

class LumberjackValetDriver extends LaravelValetDriver
{
    public function serves(string $sitePath, string $siteName, string $uri): bool
    {
        return file_exists($sitePath.'/web/wp/wp-load.php');
    }

    public function isStaticFile(string $sitePath, string $siteName, string $uri)
    {
        // Define potential static file paths
        $staticFilePaths = [
            $sitePath.'/web'.$uri,
            $sitePath.'/web/app'.$uri,
            $sitePath.'/web/app/themes'.$uri,
            $sitePath.'/web/app/uploads'.$uri
        ];

        // Check if the static file exists in any of the defined paths
        foreach ($staticFilePaths as $staticFilePath) {
            if ($this->isActualFile($staticFilePath)) {
                return $staticFilePath;
            }
        }

        return false;
    }

    public function frontControllerPath(string $sitePath, string $siteName, string $uri): string
    {
        $_SERVER['PHP_SELF'] = $uri;
        $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];

        // Check if the request is for a wp-admin page or a static file
        if (strpos($uri, '/wp/') === 0 || $this->isStaticFile($sitePath, $siteName, $uri)) {
            if (is_dir($sitePath.'/web'.$uri)) {
                $uri = $this->forceTrailingSlash($uri);
                return $sitePath.'/web'.$uri.'/index.php';
            }
            return $sitePath.'/web'.$uri;
        }

        // Default to the main index.php for other requests
        return $sitePath.'/web/index.php';
    }

    private function forceTrailingSlash(string $uri): string
    {
        if (substr($uri, -1 * strlen('/wp/wp-admin')) == '/wp/wp-admin') {
            header('Location: '.$uri.'/');
            die;
        }

        return $uri;
    }
}

/wp/wp-admin works just fine. I can access the dashboard and all admin functionality without any issues. The problem is the front-end, images, js, css etc is never loaded (seems not static files load).

Any help would be appreciated, as we have quite a few sites using Lumberjack, and if we cant get this working we have to move them all over to Themosis Framework which would cost a lot of time, and not be ideal, as they are all working really great using lumberjack.

I did not put down version information for PHP or Lumberjack, as its not relevant to the issue.

adamtomat commented 3 weeks ago

We tend to use Docker so I've not used Heard yet, but back when we used Laravel Valet we had to use this Valet Driver for Bedrock/Trellis. May be useful?

https://github.com/danielroe/trellis-valet-driver/blob/master/TrellisValetDriver.php

kyloknight commented 3 weeks ago

@adamtomat, thank for such a quick response, really appreciated! I am just going to try a bit longer to figure it out. I adapted what you sent and it looks like this:

<?php

namespace Valet\Drivers\Custom;

use Valet\Drivers\LaravelValetDriver;

class LumberjackValetDriver extends LaravelValetDriver
{
    /**
     * Determine if the driver serves the request.
     *
     * @param string $sitePath
     * @param string $siteName
     * @param string $uri
     * @return bool
     */
    public function serves(string $sitePath, string $siteName, string $uri): bool
    {
        return file_exists($sitePath . '/web/app/mu-plugins/bedrock-autoloader.php') ||
            (is_dir($sitePath . '/web/app/') &&
                file_exists($sitePath . '/web/wp-config.php') &&
                file_exists($sitePath . '/config/application.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)
    {
        $staticFilePath = $sitePath . '/web' . $uri;
        if ($this->isActualFile($staticFilePath)) {
            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
    {
        $_SERVER['PHP_SELF'] = $uri;
        $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];

        if (strpos($uri, '/wp/') === 0) {
            return is_dir($sitePath . '/web' . $uri)
                ? $sitePath . '/web' . $this->forceTrailingSlash($uri) . '/index.php'
                : $sitePath . '/web' . $uri;
        }
        return $sitePath . '/web/index.php';
    }

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

wp admin is fine still as before, but front facing pages still don't fetch any assets for some odd reason. I just dont have the time to go through all of lumberjacks core code to figure this out. I will have to work on a staging server for now rather than local. Thanks again for that super quick response <3

adamtomat commented 3 weeks ago

My gut feel is that this is less of a specific Lumberjack issue and more about Heard + Bedrock. Lumberjack is just the theme, so I'd be surprised if anything LJ related is causing issues especially as we don't do anything to asset loading etc.

I'd recommend seeing if there are any other folks experiencing this issue with Heard/Valet and Bedrock/Trellis as that directory structure is common regardless of the theme being used.

Also if this is blocking you, you can also spin up a local PHP server, e.g:

php -S 127.0.0.1:8080

This uses the installed PHP version on your machine instead of Heard, but may be useful - I've defo done this a bunch in the past 😅

kyloknight commented 3 weeks ago

Thanks @adamtomat. When I figure it out I will come back and post it here for other users :)