leafo/lessphp — pull request #207 by jsdalton (2012-03-13 05:22:08+00:00)
Title: Fix imports for symlinked relative paths

This pull request provides a solution for the issue raised in #206.

The main change is to improve upon the regex pattern that is used to munge a folder structure containing relative paths prior to its being passed to `realpath()`.

Previously, the regex used was:

'/\w+\/\.\.\//'

The problem with this pattern is that it only matches folder names that contain word characters. It ignores the common hyphen character along with other. I replaced the pattern with the following:

'/([^.][^\/]*\/|\.[^.\/][^\/]*\/|\.\.[^\/]+\/)\.\.\//'

The main difference is that '\w+\/' is replaced by three alternative patterns. The three alternatives are (in the format (1|2|3) as follows):

1. `[^.][^\/]*\/` - This will match any phrase that does NOT start with a dot (.) and that is followed by zero or more characters (and terminated by a forward slash).

2. `\.[^.\/][^\/]*\/` - This will match any phrase that starts with one dot and that is followed by one character that is neither a dot nor a slash, and then zero or more non-slash characters.

3. `\.\.[^\/]+\/)\.\.\//` - This will match any phrase that starts with two dots and is followed by at least one non-slash characters.

The regex here is somewhat obtuse, but in simple terms it matches any phrase between forward slashes except for "." and "..".

Notes on other minor changes in this pull request:

* The replace step is set in a loop, which will continue munging the path until the pattern can no longer be matched.
* PHP will not be able to find the pile via it's original path, so the munged path is returned from fileExists (instead of a simple true/false). findImport() was adjusted to account for this.

You may wish to rearrange this a bit or add some tests, but hopefully this is a decent start in the right direction.