Skip to content

Instantly share code, notes, and snippets.

@liamr
Created April 21, 2011 23:35
Show Gist options
  • Save liamr/935717 to your computer and use it in GitHub Desktop.
Save liamr/935717 to your computer and use it in GitHub Desktop.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Deploy extends Front_Controller
{
protected $file_name = '';
protected $_path = '';
/**
* Constructor
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->load->driver('minify');
$this->_path = FCPATH.'s3/';
$this->file_name = ENVIRONMENT;
}
// ------------------------------------------------------------------------
/**
* Index File
*
* Handles deploying files to S3.
*/
public function index()
{
//do processing here
// first delete old file revisions
$this->_remove_old(ENVIRONMENT); // $this->config->item('script_revision'));
// get the web hook data
// $sent_data = file_get_contents('php://input');
// $data = json_decode($sent_data);
// Update script revision
$this->settings->add_setting('script_revision', $this->file_name, 'site', 'yes');
// Deploy js and css
$this->_js();
$this->_css();
}
public function manual($revision = '')
{
// Update script revision
$this->settings->add_setting('script_revision', $this->file_name, 'site', 'yes');
// Deploy js and css
$this->_js();
$this->_css();
}
// ------------------------------------------------------------------------
/**
* Remove Old
*
* Remove old versions uploaded to s3.
*
* @param string $old
* @return void
*/
private function _remove_old($old = '')
{
$this->load->library('s3');
$this->load->helper('directory');
foreach (directory_map($this->_path.'css', TRUE) as $dir => $file)
{
if (pathinfo($file, PATHINFO_EXTENSION) == 'css')
{
$this->s3->deleteObject('admin', $old.'/css/'.$file);
}
}
foreach (directory_map($this->_path.'js', TRUE) as $dir => $file)
{
if (pathinfo($file, PATHINFO_EXTENSION) == 'js')
{
$this->s3->deleteObject('admin', $old.'/js/'.$file);
}
}
}
// ------------------------------------------------------------------------
/**
* Combine all the js and minify.
*
* description
*
* @param string $file_name
* @return bool
*/
private function _js($file_name = 'all.js')
{
$contents = $this->minify->combine_directory($this->_path.'js/min/', array('nestedsortable.min.js'), TRUE);
$this->minify->save_file($contents, $this->_path.'js/all.js');
$this->_put('s3/js/all.js', $file_name);
$this->_put('s3/js/tools.js', 'tools.js');
$this->_put('s3/js/ghcore.js', 'ghcore.js');
$this->_put('s3/js/head.js', 'head.js');
$this->_put('s3/js/fileupload.js', 'fileupload.js');
$this->_put('s3/js/fileupload-ui.js', 'fileupload-ui.js');
$this->_put('s3/js/jcrop.js', 'jcrop.js');
}
// ------------------------------------------------------------------------
/**
* Combine all the css and minify.
*
* @param string $file_name
* @return bool
*/
private function _css($file_name = 'all.css')
{
$this->load->helper('directory');
foreach (directory_map($this->_path.'css', TRUE) as $dir => $file)
{
if (pathinfo($file, PATHINFO_EXTENSION) == 'css')
{
$this->_put($this->_path.'css/'.$file, $file, 'css');
}
}
}
// ------------------------------------------------------------------------
/**
* Send a file to s3
*
* @param string
* @param string
* @param string
* @param array
* @return bool
*/
private function _put($file_path, $file_name, $type = 'js', $headers = array())
{
$this->load->library('s3');
// $file_name = ENVIRONMENT.'_'.$file_name;
if ($type == 'js')
{
$meta_headers = array(
'Content-Type' => 'application/javascript',
"Cache-Control" => "max-age=315360000",
"Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years"))
);
}
else
{
$meta_headers = array(
'Content-Type' => 'text/css',
"Cache-Control" => "max-age=315360000",
"Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years"))
);
}
$headers = array(
"Cache-Control" => "max-age=315360000",
"Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years"))
);
if ($this->s3->putObject($this->s3->inputFile($file_path), 'admin', $this->file_name.'/'.$type.'/'.$file_name, S3::ACL_PUBLIC_READ, $headers, $meta_headers))
{
return TRUE;
}
return FALSE;
}
}
/* End of file deploy.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment