Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
Migrating structure from https://github.com/BKWLD/laravel-haml
  • Loading branch information
weotch committed Jul 19, 2016
0 parents commit d459bd7
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
composer.lock
.DS_Store
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "bkwld/laravel-pug",
"description": "Pug view adapter for Laravel",
"type": "library",
"require": {
"php": ">=5.4.0",
"illuminate/support": "4 - 5",
"illuminate/view": "4 - 5",
"pug-php/pug": "^1.0.0"
},
"autoload": {
"psr-4": {
"Bkwld\\LaravelPug\\": "src/"
}
},
"license": "MIT",
"authors": [
{
"name": "Robert Reinhard",
"email": "[email protected]"
}
]
}
24 changes: 24 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php return [

/**
* Passthrough php-pug config options
* https://github.com/pug-php/pug/blob/master/src/Jade/Jade.php
*/

// 'cache' => null,
// 'stream' => null,
// 'extension' => array('.pug', '.jade'),
// 'prettyprint' => false,
// 'phpSingleLine' => false,
// 'keepBaseName' => false,
// 'allowMixinOverride' => true,
// 'allowMixedIndent' => true,
// 'keepNullAttributes' => false,
// 'restrictedScope' => false,
// 'singleQuote' => false,
// 'filterAutoLoad' => true,
// 'indentSize' => 2,
// 'indentChar' => ' ',
// 'customKeywords' => array(),
// 'classAttribute' => null,
];
3 changes: 3 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php namespace Bkwld\LaravelPug;

class Exception extends \Exception { }
53 changes: 53 additions & 0 deletions src/PugBladeCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php namespace Bkwld\LaravelPug;

// Dependencies
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Compilers\CompilerInterface;
use Illuminate\Filesystem\Filesystem;
use Pug\Pug;

class PugBladeCompiler extends BladeCompiler implements CompilerInterface {

/**
* The MtHaml instance.
*
* @var Pug
*/
protected $pug;

/**
* Create a new compiler instance.
*
* @param Pug $pug
* @param \Illuminate\Filesystem\Filesystem $files
* @param string $cachePath
* @return void
*/
public function __construct(Pug $pug, Filesystem $files, $cachePath)
{
$this->pug = $pug;
parent::__construct($files, $cachePath);
}

/**
* Compile the view at the given path.
*
* @param string $path
* @return void
*/
public function compile($path) {
$this->footer = array();

if (is_null($this->cachePath)) return;

// First compile the Haml
$contents = $this->pug->compile($this->files->get($path));

// Then the Blade syntax
$contents = $this->compileString($contents);

// Save
$this->files->put($this->getCompiledPath($path), $contents);
}

}
44 changes: 44 additions & 0 deletions src/PugCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php namespace Bkwld\LaravelPug;

// Dependencies
use Illuminate\View\Compilers\Compiler;
use Illuminate\View\Compilers\CompilerInterface;
use Illuminate\Filesystem\Filesystem;
use Pug\Pug;

class PugCompiler extends Compiler implements CompilerInterface {

/**
* The MtHaml instance.
*
* @var Pug
*/
protected $pug;

/**
* Create a new compiler instance.
*
* @param Pug $pug
* @param Illuminate\Filesystem\Filesystem $files
* @param string $cachePath
* @return void
*/
public function __construct(Pug $pug, Filesystem $files, $cachePath)
{
$this->pug = $pug;
parent::__construct($files, $cachePath);
}

/**
* Compile the view at the given path.
*
* @param string $path
* @return void
*/
public function compile($path) {
if (is_null($this->cachePath)) return;
$contents = $this->pug->compile($this->files->get($path));
$this->files->put($this->getCompiledPath($path), $contents);
}

}
163 changes: 163 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php namespace Bkwld\LaravelPug;

// Dependencies
use Pug\Pug;
use Illuminate\View\Engines\CompilerEngine;

class ServiceProvider extends \Illuminate\Support\ServiceProvider {

/**
* Get the major Laravel version number
*
* @return integer
*/
public function version() {
$app = $this->app;
return intval($app::VERSION);
}

/**
* Register the service provider.
*
* @return void
*/
public function register() {

// Version specific registering
if ($this->version() == 5) $this->registerLaravel5();

// Determine the cache dir
$cache_dir = storage_path($this->version() == 5 ? '/framework/views' : '/views');

// Bind the package-configued Pug instance
$this->app->singleton('laravel-pug.pug', function($app) {
$config = $this->getConfig();
return new Pug($config);
});

// Bind the Pug compiler
$this->app->singleton('Bkwld\LaravelPug\PugCompiler', function($app) use ($cache_dir) {
return new PugCompiler($app['laravel-pug.pug'], $app['files'], $cache_dir);
});

// Bind the Pug Blade compiler
$this->app->singleton('Bkwld\LaravelPug\PugBladeCompiler', function($app) use ($cache_dir) {
return new PugBladeCompiler($app['laravel-pug.pug'], $app['files'], $cache_dir);
});

}

/**
* Register specific logic for Laravel 5. Merges package config with user config
*
* @return void
*/
public function registerLaravel5() {
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-pug');
}

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot() {

// Version specific booting
switch($this->version()) {
case 4: $this->bootLaravel4(); break;
case 5: $this->bootLaravel5(); break;
default: throw new Exception('Unsupported Laravel version');
}

// Register compilers
$this->registerPugCompiler();
$this->registerPugBladeCompiler();
}

/**
* Boot specific logic for Laravel 4. Tells Laravel about the package for auto
* namespacing of config files
*
* @return void
*/
public function bootLaravel4() {
$this->package('bkwld/laravel-pug');
}

/**
* Boot specific logic for Laravel 5. Registers the config file for publishing
* to app directory
*
* @return void
*/
public function bootLaravel5() {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('pug.php')
], 'laravel-pug');
}

/**
* Register the regular Pug compiler
*
* @return void
*/
public function registerPugCompiler() {

// Add resolver
$this->app['view.engine.resolver']->register('pug', function() {
return new CompilerEngine($this->app['Bkwld\LaravelPug\PugCompiler']);
});

// Add extensions
$this->app['view']->addExtension('pug', 'pug');
$this->app['view']->addExtension('pug.php', 'pug');
$this->app['view']->addExtension('jade', 'pug');
$this->app['view']->addExtension('jade.php', 'pug');
}

/**
* Register the blade compiler compiler
*
* @return void
*/
public function registerPugBladeCompiler() {

// Add resolver
$this->app['view.engine.resolver']->register('pug.blade', function() {
return new CompilerEngine($this->app['Bkwld\LaravelPug\PugBladeCompiler']);
});

// Add extensions
$this->app['view']->addExtension('pug.blade', 'pug.blade');
$this->app['view']->addExtension('pug.blade.php', 'pug.blade');
$this->app['view']->addExtension('jade.blade', 'jade.blade');
$this->app['view']->addExtension('jade.blade.php', 'jade.blade');
}

/**
* Get the configuration, which is keyed differently in L5 vs l4
*
* @return array
*/
public function getConfig() {
$key = $this->version() == 5 ? 'laravel-pug' : 'laravel-pug::config';
return $this->app->make('config')->get($key);
}



/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return array(
'Bkwld\LaravelPug\PugCompiler',
'Bkwld\LaravelPug\PugBladeCompiler',
'laravel-pug.pug',
);
}

}

0 comments on commit d459bd7

Please sign in to comment.