I recently found myself needing to pull in configuration information from a Laravel installation into Laravel Elixir (or more specifically the Gulpfile). The reason for this was so that i could configure the source paths of the sass and javascript “compilers” as i had a few dynamic paths too include, although there’s plenty of other reasons you may want to do this, especially if you find yourself copying generic configuration variables from your Laravel config files into the Elixir config.

Now there’s a plenty of ways you can achieve this. You could attempt to write a php array syntax parser in javascript (enjoying wasting a few days of your life getting that perfect) or you could put the burden on your application so you dump a JSON object of the configuration data you need too a cache file you can load up in your Gulpfile. The latter idea i did consider for a short while, but i wasn’t happy with burdening the application with build/development tasks and the management code i’d have to write to manage that cache file (it would be running in production after all, so it would have to be perfectly stable).

The answer, is actually really simple. Just look at how Laravel’s config loader actually works, and it just require’s the config file and require is a built-in PHP statement so we can simply use the PHP-CLI to load it for us.


How it’s done

Here’s a function you can just drop into your Gulpfile

Note that the first line (highlighted) would normally go at the top of your Gulpfile with the other require statements. The rest of the function is really simple, first we invoke the PHP-CLI and get it too JSON encode the contents of our config file which stores a string representation of the JSON inside contents, then we parse that string into a an object.

Using it is pretty simple, let’s say you want to know the production app URL (just trying to find a generic example, can’t really see why you’d need that, but let’s continue).

With this basic structure of loading data from your PHP source into Gulp, you can modify it too do all sorts of wonderful things. You could also create an artisan command at gulp:config and have it output all your required data instead of loading the php config files that you need.


Is this “production ready”?

Sure! If by that you mean “is this OK to run on my development machine”. Sure it’s not perfect, there’s no doubt a shell injection exploit in there and the hardcoded config path could be dynamic, but remember it’s a development tool. It’s meant for your access only and unless you plan on hacking your own application there’s no real problem. Error handling’s a bit basic too but that’s not an issue either, you’re a developer, if it doesn’t work, fix it. The important thing is we’ve kept the impact of this development tool inside the development environment, it works perfectly fine and you haven’t had to waste time over-engineering some complex, all singing and dancing solution.


Does this only work with Laravel & Elixir?

The code i provided above does rely on the Laravel config structure too work (i.e. the path too the file is hardcoded and requires the file just returns an array), but the basic idea is universal. In fact nothing in the code i provided apart from the function name, is Laravel specific at all. As long as you can write a CLI script that returns a JSON string representation of the data you want, you can use this method.