public
Last active

Creates a file in DOCROOT if found within the CFS.

  • Download Gist
controller-publicize.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Controller for Publicize. Currently only contains a redirect
* action.
*
* @package Publicize
* @category Helpers
* @author Luke Morton
* @copyright Luke Morton, 2011
* @license MIT
*/
class Controller_Publicize extends Controller {
 
/**
* Simple redirect action for use by Publicize.
*
* @return void
*/
public function action_redirect()
{
$this->request->redirect(
$this->request->param('uri')
);
}
}
init.php
PHP
1 2 3 4
<?php defined('SYSPATH') or die('No direct script access.');
 
// Set route if not in production
Publicize::set_route();
publicize.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Publicize is a small helper class that provides routing
* functionality that enables modules to include files that are
* publically available via your htdocs, public, html, www,
* %%insert another public folder variation%%.
*
* When not in production mode going to the URL `css/screen.css`
* (a file that does not exist in your public directory), the
* Kohana Cascading File System will be searched for this path
* within a folder called `public`, if found it is copied to the
* public directory.
*
* In order to update the file you do need to delete it from
* your public directory.
*
* @package Publicize
* @category Helpers
* @author Luke Morton
* @copyright Luke Morton, 2011
* @license MIT
*/
class Publicize {
 
const PUBLIC_FOLDER = 'public';
 
/**
* Sets route if not in production.
*
* @return void
*/
public static function set_route()
{
if (Kohana::$environment !== Kohana::PRODUCTION)
{
Route::set('Publicize', array('Publicize', 'route_callback'));
}
}
 
/**
* Route callback, contains the copying functionality.
*
* @param string URI to search for
* @return array Params for Controler_Publicize::action_redirect()
* @return void If no file found
*/
public static function route_callback($uri)
{
if ($asset = Kohana::find_file(self::PUBLIC_FOLDER, $uri, FALSE))
{
$public_asset = DOCROOT.$uri;
if ( ! is_file($public_asset))
{
$public_asset_dir = dirname($public_asset);
if ( ! is_dir($public_asset_dir))
{
mkdir($public_asset_dir, NULL, TRUE);
}
touch($public_asset);
}
file_put_contents($public_asset, file_get_contents($asset));
return array(
'controller' => 'publicize',
'action' => 'redirect',
'uri' => $uri,
);
}
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.