Permalink
Browse files

LaravelHaml

  • Loading branch information...
weotch committed Nov 15, 2013
0 parents commit e2c69c8e03e2d3018fcea301763f2227ef79f1ed
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
@@ -0,0 +1,12 @@
language: php
php:
- 5.3
- 5.4
- 5.5
before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev
script: phpunit
20 LICENSE
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2013 BKWLD
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
# Auto Publish
A small package that exposes a Laravel facade to compile HAML files using [MtHaml](https://github.com/arnaud-lb/MtHaml).
## Installation
1. Add it to your composer.json (`"bkwld/laravel-haml": "~1.0"`) and do a composer install.
2. Add the service provider to your app.php config file providers: `'Bkwld\LaravelHaml\ServiceProvider',`
3. Create an alias to the facade: `'HAML' => 'Bkwld\LaravelHaml\Facade',`
## Usage
Manually invoke the LaravelHaml compiler before you render your view. For instance, here is a Laravel controller action:
public function index() {
HAML::compile('home.index');
$this->layout->nest('content', 'home.index');
}
When this action is requested, LaravelHaml will:
1. Check that the enviornment is "local". If it isn't, it will exit.
2. Look for a file at /app/views/home/index.haml. If not found, an exception is raised.
3. Compile the the template using [MtHaml](https://github.com/arnaud-lb/MtHaml). But only if there isn't an existing compiled view that is still valid.
4. Save the compiled haml into a php file at /app/views/home/index.php.
Thus, when Laravel goes to nest the `home.index` view, it will find the compiled php view.
@@ -0,0 +1,22 @@
{
"name": "bkwld/laravel-haml",
"description": "Wraps MtHaml for ease use in Laravel",
"authors": [
{
"name": "Robert Reinhard",
"email": "info@bkwld.com"
}
],
"license": "MIT",
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.0.x",
"mthaml/mthaml": "~1.3.0"
},
"autoload": {
"psr-0": {
"Bkwld\\LaravelHaml": "src/"
}
},
"minimum-stability": "dev"
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
@@ -0,0 +1,3 @@
<?php namespace Bkwld\LaravelHaml;
class Exception extends \Exception { }
@@ -0,0 +1,4 @@
<?php namespace Bkwld\LaravelHaml;
class Facade extends \Illuminate\Support\Facades\Facade {
protected static function getFacadeAccessor() { return 'laravel-haml'; }
}
@@ -0,0 +1,57 @@
<?php namespace Bkwld\LaravelHaml;
class Haml {
/**
* Inject dependencies
*
* @param MtHaml\Environment $mthaml
* @param string $dir The views directory
* @param string $env The current environment
*/
public function __construct($mthaml, $dir, $env) {
$this->mthaml = $mthaml;
$this->dir = $dir;
$this->env = $env;
}
/**
* Compile a haml file into a Laravel PHP view if called from a "local"
* enviornment. It will save the rendered template in the same directory
* as the haml view, with the same name.
*
* This is based largely on the example file from mthaml:
* https://github.com/arnaud-lb/MtHaml/blob/master/examples/example-php.php
*
* @param string $haml_view The relative path (in the Laravel style) to the PHP
* file containg the HAML. Ex: 'home.index'. The suffix is assumed to
* be "haml". Ex: '/app/views/home/index.haml'.
* @return void
*/
public function compile($haml_view) {
// Only compile while on a local enviornment
if ($this->env != 'local') return;
// Get haml template code
$view = $this->dir.DIRECTORY_SEPARATOR.str_replace('.', DIRECTORY_SEPARATOR, $haml_view);
$haml_file = $view.'.haml';
$php_file = $view.'.php';
if (!file_exists($haml_file)) throw new Exception('HAML file does not exist at '.$haml_file);
$haml_code = file_get_contents($haml_file);
// No need to compile if already compiled and up to date
if (file_exists($php_file) && filemtime($php_file) == filemtime($haml_file)) return;
// Compile haml to php
$php_code = $this->mthaml->compileString($haml_code, $haml_file);
// Save php file which will get used by Laravel as a view
$tmp = tempnam(dirname($haml_file), basename($haml_file));
file_put_contents($tmp, $php_code);
rename($tmp, $php_file);
touch($php_file, filemtime($haml_file));
}
}
@@ -0,0 +1,41 @@
<?php namespace Bkwld\LaravelHaml;
class ServiceProvider extends \Illuminate\Support\ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* @return void
*/
public function register() {
// Register the main class which sits behind the HAML facade
$this->app->singleton('laravel-haml', function($app) {
// Inject Dependencies
return new Haml(
new \MtHaml\Environment('php'),
$this->app->make('path').'/views',
$this->app->environment()
);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return array('laravel-haml');
}
}
No changes.

0 comments on commit e2c69c8

Please sign in to comment.