Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Bug #14, Code standard fixes: whitespace, indent, etc - libraries [ie…
Browse files Browse the repository at this point in the history
…t:3633574]

* Mostly automated via Composer: ` phpcbf --standard=./phpcs.xml ...`
  • Loading branch information
nfreear committed Jun 18, 2015
1 parent 44e4fb4 commit 5a6ae4e
Show file tree
Hide file tree
Showing 8 changed files with 856 additions and 776 deletions.
287 changes: 150 additions & 137 deletions application/libraries/Gitlib.php
Original file line number Diff line number Diff line change
@@ -1,137 +1,150 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Gitlib: a simple Git library, to get changeset hashes and information.
*
* See: https://bitbucket.org/cloudengine/cloudengine/src/tip/system/application/libraries/Hglib.php
*
* @copyright 2012 The Open University.
* @author N.D.Freear, 2012-04-25.
*/

class Gitlib {

const GIT_DESCRIBE_REGEX = '/(?P<major>\d+)\.(?P<minor>\d+)(?P<id>-[\w\.]+)?-(?P<patch>\d+)-(?P<hash>g.+)/';

protected $_hash;
protected $CI;

static $REVISION_FILE = 'version.json';

public function __construct() {
$this->CI =& get_instance();

self::$REVISION_FILE = dirname(__FILE__) .'/../../'. self::$REVISION_FILE;
}

/**
* Return the start of the most recent commit hash (from file).
* Maybe md5/ sha() the result?
*/
public function lastHash($length = 6) {
if ($length < 4) $length = 4;

if ($this->_hash) {
return substr($this->_hash, 0, $length);
}

$log = $this->get_revision();
$this->_hash = $log->commit;

return substr($this->_hash, 0, $length);
}

/** Save revision meta-data to a '.' file, JSON-encoded.
* (CloudEngine's Hglib uses PHP (un)serialize.)
*/
public function put_revision() {
$log = $this->_exec('log -1');

$log = explode("\n", $log);
$result = FALSE;
//Hmm, a more efficient way?
foreach ($log as $line) {
if (FALSE !== ($p = strpos($line, ' '))) { #':'
$key = trim(substr($line, 0, $p), ' :');
if (!$key) $key = 'message';
$result[strtolower($key)] = trim(substr($line, $p+1));
if ('message'==$key) break;
}
}
// Describe "v0.86-usertest-95-g.."
// Semantic Versioning, http://semver.org
$result['describe'] = trim($this->_exec('describe --tags --long'));
$result[ 'version' ] = $result[ 'describe' ];
if (preg_match( self::GIT_DESCRIBE_REGEX, $result[ 'describe' ], $m )) {
$result[ 'version' ] = $m['major'] .'.'. $m['minor'] .'.'. $m['patch'] . $m['id'] .'+'. $m['hash'];
}
// http://stackoverflow.com/questions/4089430/how-can-i-determine-the-url-that-a-local-git-repo-was-originally-pulled-from
$result['origin'] = rtrim($this->_exec('config --get remote.origin.url'), "\r\n");
#$result['origin url'] = str_replace(array('git@', 'com:'), array('https://', 'com/'), $result['origin']);
#$result['agent'] = basename(__FILE__);
#$result['git'] = rtrim($this->_exec('--version'), "\r\n ");
$result['file_date'] = date( 'c' );

$bytes = $this->put_json(self::$REVISION_FILE, $result);

echo "File written, $bytes: ", self::$REVISION_FILE;

return $result;
}

/** Read revision meta-data from the '.' file.
*/
public function get_revision() {
return (object) json_decode(file_get_contents(self::$REVISION_FILE));
}


protected function put_json( $filename, $data ) {
return file_put_contents( $filename, str_replace( '","', "\",\n\"", json_encode( $data )));
}

/** Execute a Git command, if allowed.
*/
protected function _exec($cmd) {

if (! $this->CI->input->is_cli_request()) {
echo "Warning, Git must be run from the commandline.".PHP_EOL;
return FALSE;
}

//Security?
$git_path = $this->CI->config->item('git_path');


if (! $git_path) {
$git_path = "git"; #"/usr/bin/env git";
#$git_path = "/usr/bin/git"; #Redhat6
#$git_path = "/usr/local/git/bin/git"; #Mac
}

$git_cmd = "$git_path $cmd";

$result = FALSE;
// The path may contain 'sudo ../git'.
if (! file_exists($git_path)) {
echo "Warning, not found, $git_path".PHP_EOL;
}


#if (file_exists($git_path)) {
$cwd = getcwd();
if ($cwd) {
chdir(APPPATH);
}

$handle = popen(escapeshellcmd($git_cmd), 'r'); //2>&1
$result = fread($handle, 2096);
pclose($handle);

if ($cwd) {
chdir($cwd);
}
#}
return $result;
}

}
<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
* Gitlib: a simple Git library, to get changeset hashes and information.
*
* See: https://bitbucket.org/cloudengine/cloudengine/src/tip/system/application/libraries/Hglib.php
*
* @copyright 2012 The Open University.
* @author N.D.Freear, 2012-04-25.
*/

class Gitlib
{

const GIT_DESCRIBE_REGEX = '/(?P<major>\d+)\.(?P<minor>\d+)(?P<id>-[\w\.]+)?-(?P<patch>\d+)-(?P<hash>g.+)/';

protected $_hash;
protected $CI;

protected static $REVISION_FILE = 'version.json';

public function __construct()
{
$this->CI =& get_instance();

self::$REVISION_FILE = dirname(__FILE__) .'/../../'. self::$REVISION_FILE;
}

/**
* Return the start of the most recent commit hash (from file).
* Maybe md5/ sha() the result?
*/
public function lastHash($length = 6)
{
if ($length < 4) {
$length = 4;
}

if ($this->_hash) {
return substr($this->_hash, 0, $length);
}

$log = $this->get_revision();
$this->_hash = $log->commit;

return substr($this->_hash, 0, $length);
}

/** Save revision meta-data to a '.' file, JSON-encoded.
* (CloudEngine's Hglib uses PHP (un)serialize.)
*/
public function put_revision()
{
$log = $this->_exec('log -1');

$log = explode("\n", $log);
$result = false;
//Hmm, a more efficient way?
foreach ($log as $line) {
if (false !== ($p = strpos($line, ' '))) {
#':'
$key = trim(substr($line, 0, $p), ' :');
if (!$key) {
$key = 'message';
}
$result[strtolower($key)] = trim(substr($line, $p+1));
if ('message'==$key) {
break;
}
}
}
// Describe "v0.86-usertest-95-g.."
// Semantic Versioning, http://semver.org
$result['describe'] = trim($this->_exec('describe --tags --long'));
$result[ 'version' ] = $result[ 'describe' ];
if (preg_match(self::GIT_DESCRIBE_REGEX, $result[ 'describe' ], $m)) {
$result[ 'version' ] = $m['major'] .'.'. $m['minor'] .'.'. $m['patch'] . $m['id'] .'+'. $m['hash'];
}
// http://stackoverflow.com/questions/4089430/how-can-i-determine-the-url-that-a-local-git-repo-was-originally-pulled-from
$result['origin'] = rtrim($this->_exec('config --get remote.origin.url'), "\r\n");
#$result['origin url'] = str_replace(array('git@', 'com:'), array('https://', 'com/'), $result['origin']);
#$result['agent'] = basename(__FILE__);
#$result['git'] = rtrim($this->_exec('--version'), "\r\n ");
$result['file_date'] = date('c');

$bytes = $this->put_json(self::$REVISION_FILE, $result);

echo "File written, $bytes: ", self::$REVISION_FILE;

return $result;
}

/** Read revision meta-data from the '.' file.
*/
public function get_revision()
{
return (object) json_decode(file_get_contents(self::$REVISION_FILE));
}


protected function put_json($filename, $data)
{
return file_put_contents($filename, str_replace('","', "\",\n\"", json_encode($data)));
}

/** Execute a Git command, if allowed.
*/
protected function _exec($cmd)
{

if (! $this->CI->input->is_cli_request()) {
echo "Warning, Git must be run from the commandline.".PHP_EOL;
return false;
}

//Security?
$git_path = $this->CI->config->item('git_path');


if (! $git_path) {
$git_path = "git"; #"/usr/bin/env git";
#$git_path = "/usr/bin/git"; #Redhat6
#$git_path = "/usr/local/git/bin/git"; #Mac
}

$git_cmd = "$git_path $cmd";

$result = false;
// The path may contain 'sudo ../git'.
if (! file_exists($git_path)) {
echo "Warning, not found, $git_path".PHP_EOL;
}


#if (file_exists($git_path)) {
$cwd = getcwd();
if ($cwd) {
chdir(APPPATH);
}

$handle = popen(escapeshellcmd($git_cmd), 'r'); //2>&1
$result = fread($handle, 2096);
pclose($handle);

if ($cwd) {
chdir($cwd);
}
#}
return $result;
}
}
25 changes: 10 additions & 15 deletions application/libraries/Layout.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
<?php defined('BASEPATH') or exit('No direct script access allowed');

class Layout
{

var $obj;
var $layout;
protected $obj;
protected $layout;

function Layout($layout = "layout_main")
public function Layout($layout = "layout_main")
{
#ou-specific bug fix
if (is_array($layout)) {
Expand All @@ -18,25 +17,21 @@ function Layout($layout = "layout_main")
$this->layout = $layout;
}

function setLayout($layout)
public function setLayout($layout)
{
$this->layout = $layout;
$this->layout = $layout;
}

function view($view, $data=null, $return=false)
public function view($view, $data = null, $return = false)
{
$loadedData = array();
$loadedData['content_for_layout'] = $this->obj->load->view($view,$data,true);
$loadedData['content_for_layout'] = $this->obj->load->view($view, $data, true);

if($return)
{
if ($return) {
$output = $this->obj->load->view($this->layout, $loadedData, true);
return $output;
}
else
{
} else {
$this->obj->load->view($this->layout, $loadedData, false);
}
}
}
?>
Loading

0 comments on commit 5a6ae4e

Please sign in to comment.