phug-php / phug

Phug - The Pug Template Engine for PHP
https://phug.selfbuild.fr
MIT License
63 stars 3 forks source link

cacheDirectory function issues #103

Closed Lacni135 closed 1 year ago

Lacni135 commented 1 year ago

Hello,

I encountered an issue using the cacheDirectory() function.

I'm using this script to cache all pug file in my application (for production).

<?php

use Phug\Phug;

include('vendor/autoload.php');

const ROOT_DIR = __DIR__;

$files = glob('/var/cache/pug/*'); // get all file names
foreach($files as $file){ // iterate files
    if(is_file($file)) {
        unlink($file); // delete file
    }
}

Phug::setOptions([
    'cache_dir' => '/var/cache/pug',
    'paths'     => realpath(ROOT_DIR . '/public'),
    'debug' => false
]);

Phug::cacheDirectory(ROOT_DIR . '/app/Views');

Some of the errors thrown. PHP Fatal error: Uncaught TypeError: Phug\Renderer\Adapter\FileAdapter::locate(): Argument #2 ($locations) must be of type array, string given PHP Fatal error: Uncaught TypeError: Phug\Compiler\Locator\FileLocator::locate(): Argument #2 ($locations) must be of type array, string given

Passing an array instead of a string to cacheDirectory() also gives typing errors.

To note, every pug files successfully compile and caches when the normal render runs with up_to_date_check = true

Am I missing something?

Thanks!

kylekatarnls commented 1 year ago

Hello, 👋

paths is an array option:

Phug::setOptions([
    'cache_dir' => '/var/cache/pug',
    'paths'     => [realpath(ROOT_DIR . '/public')],
    'debug' => false
]);

Passing string to 'paths' is not intended to work, please retry with the code above.

If it's still giving you an error please re-open this issue and post it here with the complete stack trace. 🙏

Lacni135 commented 1 year ago

Thank you for the quick response :) The array for paths seems to have indeed fixed the problem. I have another kind of problem related to the cacheDirectory function (i can open another issue if you prefer).

Now when executing the function i get this error: PHP Fatal error: Nesting level too deep - recursive dependency? in /var/www/pulsar-template/vendor/phug/phug/src/Phug/Event/EventManagerTrait.php on line 53

Changing the in_array() to add the $strict bool to true seems to fix the issue.

kylekatarnls commented 1 year ago

If this is sensitive to strictness of the in_array, then some of the listener is likely not an object, maybe some false or null value sliced in there 🤔 Fishy. But anyway, I don't see any reason not to be strict for any of the in_array in our project, I will strictify them all.

Lacni135 commented 1 year ago

Thanks alot!