Jump to content
bernhard

RockLESS - PHP LESS parsing helper module.

Recommended Posts

Where did you get the Less compiler part from? I noticed some warnings from php regarding the use of "continue" rather than "break" in some switch statements (functionally identical but continue generates a warning). I saw they were from the less.php/Less.php file and was about to patch them, however I was curious as to whether this was imported from an external library or you rolled your own. If external then I would imagine it more useful to fix it upstream first. I did a brief search for "less.php" but didn't find anything that seemed to match your project file's structure.

Share this post


Link to post
Share on other sites

Hi @porl sorry for the delay!

It's this one: https://github.com/oyejorge/less.php I see that the repository is archived/read-only. Maybe you want to fork it and I can include the fixed version?

Or we can take one of the 186 available forks 🙈 https://github.com/oyejorge/less.php/network/members; maybe https://github.com/wikimedia/less.php ?

Share this post


Link to post
Share on other sites

@bernhard I definitely think a more active fork makes sense. If one of use were to fork it for this one-off I think a similar problem would just happen again in future.

Share this post


Link to post
Share on other sites

@porl could you please try if the fork linked above resolves your problem? It has a very active commit log and 72 contributors so that should be a solid choice.

Share this post


Link to post
Share on other sites

@bernhard I'll be happy to when I get a chance. Unfortunately I'm in the middle of a project I have severely underestimated time-wise so I'm not sure when I'll get to it sorry. If you don't get anything useful from me in the near future make sure to ping me again and I'll try to get back to it!

Share this post


Link to post
Share on other sites

@bernhard I have started a quick test and so far so good.

The wikimedia fork is structured differently but I copied the "lib/Less" directory into the RockLESS one (so there is /RockLESS/Less) and modified the RockLESS.module.php file as follows:

diff --git a/site/modules/RockLESS/RockLESS.module.php b/site/modules/RockLESS/RockLESS.module.php
index a711627..fd455da 100644
--- a/site/modules/RockLESS/RockLESS.module.php
+++ b/site/modules/RockLESS/RockLESS.module.php
@@ -23,7 +23,10 @@ class RockLESS extends WireData implements Module {
   public function init() {
     // load less.php if it is not already loaded
     // a simple require_once does not work properly
-    if(!class_exists('Less_Parser')) require_once(__DIR__ . "/less.php/Less.php");
+    if(!class_exists('Less_Parser')) {
+      require_once(__DIR__ . "/Less/Autoloader.php");
+      \Less_Autoloader::register();
+    }
   }
 
   /**

A quick check and the RockSkinUIKit module loads and executes with no more errors.

I'm not using the RockLESS module beyond that in this project but I assume that is a sign that it should work as expected.

  • Thanks 1

Share this post


Link to post
Share on other sites

v0.0.3 fixes a bug where cssUrl returned the path and not the relative url due to windows directory separator. Not sure why this was no issue until now... And another reason why I hope that we get $files->url() and $files->path() soon 😐 

  • Like 1

Share this post


Link to post
Share on other sites

v0.0.5 adds the link to the used parser to the readme and a parse() method to parse a string of less (not a less file)

  • Like 1

Share this post


Link to post
Share on other sites

It looks like this module could allow for the introduction of field content prior to compiling - is that accurate? Is there anything special that needs to be done to prioritize it at runtime before ProCache? I'm looking to be able to push field values from SettingsFactory into the LESS file preproc.

Share this post


Link to post
Share on other sites

Yes, RockLESS can send variables from PHP to LESS:

$less = $modules->get('RockLESS');
$less->vars = [
  'foo' => 'bar',
];
$css = $less->getCSS($config->paths->templates . 'less/theme.less')->cssUrl;
echo "<link rel='stylesheet' type='text/css' href='$css'>";

How you combine that with ProCache is up to you. You can either send the compiled CSS to procache or I think you could also generate a LESS file via plain PHP listing your variables and include that file via @include in your LESS and then parse that LESS via ProCache.

  • Like 1

Share this post


Link to post
Share on other sites

Just added the feature to parse multiple LESS files into one single CSS (file or string), bumped the version to 1.0.0 and updated the readme (proper docs) 😎

  • Like 1

Share this post


Link to post
Share on other sites

This worked really nicely, but I have run into an interesting problem.

I'm trying to use this in coordination with Settings Factory, and the issue I guess is that there is no real way for RockLESS to detect that the variables going into the CSS call have changed - so I can change the options all day long but the css won't regenrate because RockLess hasn't detected any changes in the less file it is calling - right?

Would it make sense to hook the settings factory so that when the settings are saved the compiled file is deleted? Or would it be safer to just touch the less file the less file RockLess is trying to load?

I'm curious if any others are trying to do something similar.

Share this post


Link to post
Share on other sites
5 hours ago, gornycreative said:

Would it make sense to hook the settings factory so that when the settings are saved the compiled file is deleted?

Yep. Perfect solution 🙂 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...