1
0

Initial Commit

This commit is contained in:
2014-05-06 12:16:20 +02:00
commit 0db46dd097
77 changed files with 23839 additions and 0 deletions

View File

@@ -0,0 +1,416 @@
<?php
/**
* ArrayX class file.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @copyright 2013 2amigOS! Consultation Group LLC
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace Yiinitializr\Helpers;
/**
* ArrayX provides a set of useful functions
*
* @author Antonio Ramirez <ramirez.cobos@gmail.com>
* @package Yiinitializr.helpers
* @since 1.0
*/
class ArrayX
{
/**
* Get an item from an array using "dot" notation.
*
* <code>
* // Get the $array['user']['name'] value from the array
* $name = ArrayX::get($array, 'user.name');
*
* // Return a default from if the specified item doesn't exist
* $name = ArrayX::get($array, 'user.name', 'Taylor');
* </code>
*
* @param array $array
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get($array, $key, $default = null)
{
if (is_null($key)) return $array;
// To retrieve the array item using dot syntax, we'll iterate through
// each segment in the key and look for that value. If it exists, we
// will return it, otherwise we will set the depth of the array and
// look for the next segment.
foreach (explode('.', $key) as $segment)
{
if (!is_array($array) || !array_key_exists($segment, $array))
{
return self::value($default);
}
$array = $array[$segment];
}
return $array;
}
/**
* Removes an item from the given options and returns the value.
*
* If no key is found, then default value will be returned.
*
* @param $array
* @param $key
* @param null $default
* @return mixed|null
*/
public static function pop(&$array, $key, $default = null)
{
if (is_array($array))
{
$value = self::get($array, $key, $default);
unset($array[$key]);
return $value;
} else
return $default;
}
/**
* Set an array item to a given value using "dot" notation.
*
* If no key is given to the method, the entire array will be replaced.
*
* <code>
* // Set the $array['user']['name'] value on the array
* ArrayX::set($array, 'user.name', 'Taylor');
*
* // Set the $array['user']['name']['first'] value on the array
* ArrayX::set($array, 'user.name.first', 'Michael');
* </code>
*
* @param array $array
* @param string $key
* @param mixed $value
* @return void
*/
public static function set(&$array, $key, $value)
{
if (is_null($key)) return $array = $value;
$keys = explode('.', $key);
// This loop allows us to dig down into the array to a dynamic depth by
// setting the array value for each level that we dig into. Once there
// is one key left, we can fall out of the loop and set the value as
// we should be at the proper depth.
while (count($keys) > 1)
{
$key = array_shift($keys);
// If the key doesn't exist at this depth, we will just create an
// empty array to hold the next value, allowing us to create the
// arrays to hold the final value.
if (!isset($array[$key]) || !is_array($array[$key]))
$array[$key] = array();
$array =& $array[$key];
}
$array[array_shift($keys)] = $value;
}
/**
* Remove an array item from a given array using "dot" notation.
*
* <code>
* // Remove the $array['user']['name'] item from the array
* ArrayX::forget($array, 'user.name');
*
* // Remove the $array['user']['name']['first'] item from the array
* ArrayX::forget($array, 'user.name.first');
* </code>
*
* @param array $array
* @param string $key
* @return void
*/
public static function forget(&$array, $key)
{
$keys = explode('.', $key);
// This loop functions very similarly to the loop in the "set" method.
// We will iterate over the keys, setting the array value to the new
// depth at each iteration. Once there is only one key left, we will
// be at the proper depth in the array.
while (count($keys) > 1)
{
$key = array_shift($keys);
// Since this method is supposed to remove a value from the array,
// if a value higher up in the chain doesn't exist, there is no
// need to keep digging into the array, since it is impossible
// for the final value to even exist.
if (!isset($array[$key]) || !is_array($array[$key]))
return;
$array =& $array[$key];
}
unset($array[array_shift($keys)]);
}
/**
* Return the first element in an array which passes a given truth test.
*
* <code>
* // Return the first array element that equals "Taylor"
* $value = ArrayX::first($array, function($k, $v) {return $v == 'Taylor';});
*
* // Return a default value if no matching element is found
* $value = ArrayX::first($array, function($k, $v) {return $v == 'Taylor'}, 'Default');
* </code>
*
* @param array $array
* @param Closure $callback
* @param mixed $default
* @return mixed
*/
public static function first($array, $callback, $default = null)
{
foreach ($array as $key => $value)
{
if (call_user_func($callback, $key, $value)) return $value;
}
return value($default);
}
/**
* Recursively remove slashes from array keys and values.
*
* @param array $array
* @return array
*/
public static function stripSlashes($array)
{
$result = array();
foreach ($array as $key => $value)
{
$key = stripslashes($key);
// If the value is an array, we will just recurse back into the
// function to keep stripping the slashes out of the array,
// otherwise we will set the stripped value.
if (is_array($value))
{
$result[$key] = array_strip_slashes($value);
} else
{
$result[$key] = stripslashes($value);
}
}
return $result;
}
/**
* Divide an array into two arrays. One with keys and the other with values.
*
* @param array $array
* @return array
*/
public static function divide($array)
{
return array(array_keys($array), array_values($array));
}
/**
* Pluck an array of values from an array.
*
* @param array $array
* @param string $key
* @return array
*/
public static function pluck($array, $key)
{
return array_map(function ($v) use ($key)
{
return is_object($v) ? $v->$key : $v[$key];
}, $array);
}
/**
* Get a subset of the items from the given array.
*
* @param array $array
* @param array $keys
* @return array
*/
public static function only($array, $keys)
{
return array_intersect_key($array, array_flip((array)$keys));
}
/**
* Get all of the given array except for a specified array of items.
*
* @param array $array
* @param array $keys
* @return array
*/
public static function except($array, $keys)
{
return array_diff_key($array, array_flip((array)$keys));
}
/**
* Return the first element of an array.
*
* This is simply a convenient wrapper around the "reset" method.
*
* @param array $array
* @return mixed
*/
public static function head($array)
{
return reset($array);
}
/**
* Merges two or more arrays into one recursively.
* If each array has an element with the same string key value, the latter
* will overwrite the former (different from array_merge_recursive).
* Recursive merging will be conducted if both arrays have an element of array
* type and are having the same key.
* For integer-keyed elements, the elements from the latter array will
* be appended to the former array.
* @param array $a array to be merged to
* @param array $b array to be merged from. You can specifiy additional
* arrays via third argument, fourth argument etc.
* @return array the merged array (the original arrays are not changed.)
*/
public static function merge($a, $b)
{
$args = func_get_args();
$res = array_shift($args);
while (!empty($args))
{
$next = array_shift($args);
foreach ($next as $k => $v)
{
if (is_integer($k))
isset($res[$k]) ? $res[] = $v : $res[$k] = $v;
elseif (is_array($v) && isset($res[$k]) && is_array($res[$k]))
$res[$k] = self::merge($res[$k], $v); else
$res[$k] = $v;
}
}
return $res;
}
/**
* Searches for a given value in an array of arrays, objects and scalar
* values. You can optionally specify a field of the nested arrays and
* objects to search in.
*
* Credits to Util.php
*
* @param array $array The array to search
* @param string $search The value to search for
* @param bool $field The field to search in, if not specified all fields will be searched
* @return bool|mixed|string False on failure or the array key on
* @link https://github.com/brandonwamboldt/utilphp/blob/master/util.php
*/
public static function deepSearch(array $array, $search, $field = FALSE)
{
// *grumbles* stupid PHP type system
$search = (string)$search;
foreach ($array as $key => $elem)
{
// *grumbles* stupid PHP type system
$key = (string)$key;
if ($field)
{
if (is_object($elem) && $elem->{$field} === $search)
{
return $key;
} else if (is_array($elem) && $elem[$field] === $search)
{
return $key;
} else if (is_scalar($elem) && $elem === $search)
{
return $key;
}
} else
{
if (is_object($elem))
{
$elem = (array)$elem;
if (in_array($search, $elem))
{
return $key;
}
} else if (is_array($elem) && in_array($search, $elem))
{
return array_search($search, $elem);
} else if (is_scalar($elem) && $elem === $search)
{
return $key;
}
}
}
return false;
}
/**
* Returns an array containing all the elements of arr1 after applying
* the callback function to each one.
*
* Credits to Util.php
*
* @param array $array an array to run through the callback function
* @param $callback Callback function to run for each element in each array
* @param bool $on_nonscalar whether or not to call the callback function on nonscalar values (objects, resr, etc)
* @return array
* @link https://github.com/brandonwamboldt/utilphp/blob/master/util.php
*/
public static function deepMap(array $array, $callback, $on_nonscalar = FALSE)
{
foreach ($array as $key => $value)
{
if (is_array($value))
{
$args = array($value, $callback, $on_nonscalar);
$array[$key] = call_user_func_array(array(__CLASS__, __FUNCTION__), $args);
} else if (is_scalar($value) || $on_nonscalar)
{
$array[$key] = call_user_func($callback, $value);
}
}
return $array;
}
/**
* Return the value of the given item.
*
* If the given item is a Closure the result of the Closure will be returned.
*
* @param mixed $value
* @return mixed
*/
public static function value($value)
{
return (is_callable($value) and !is_string($value)) ? call_user_func($value) : $value;
}
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* Config class file.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @copyright 2013 2amigOS! Consultation Group LLC
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace Yiinitializr\Helpers;
use Yiinitializr\Helpers\ArrayX;
/**
* Config provides easy access to Yiinitializr configuration file
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @package Yiinitializr.helpers
* @since 1.0
*/
class Config
{
/**
* @var array the configuration settings
*/
private static $_settings;
private static $_config_dir_path;
private static $_envlock_file_path;
/**
* Returns a value of the array
* @param $value
* @return mixed | null if no key is found
*/
public static function value($value)
{
return ArrayX::get(self::settings(), $value);
}
/**
* Reads the configuration settings from the file
* @return array|mixed
* @throws \Exception
*/
public static function settings()
{
if (null === self::$_settings)
{
self::$_settings = file_exists(self::getConfigurationDirectoryPath() . '/settings.php')
? require_once(self::getConfigurationDirectoryPath() . '/settings.php')
: array();
self::$_settings['envlock'] = file_exists(self::getEnvironmentLockFilePath());
}
if (empty(self::$_settings))
throw new \Exception('Unable to find Yiinitialzr settings file!');
return self::$_settings;
}
/**
* @param string $content
*/
public static function createEnvironmentLockFile($content = '')
{
umask(0);
file_put_contents(self::getEnvironmentLockFilePath(), $content);
@chmod(self::getEnvironmentLockFilePath(), 0644);
}
/**
* Returns the configuration directory path
* @return string
*/
public static function getConfigurationDirectoryPath()
{
if (null === self::$_config_dir_path)
self::$_config_dir_path = dirname(__FILE__) . '/../config';
return self::$_config_dir_path;
}
/**
* Returns the environment lock file path
* @return string
*/
public static function getEnvironmentLockFilePath()
{
if (null === self::$_envlock_file_path)
self::$_envlock_file_path = self::getConfigurationDirectoryPath() . '/env.lock';
return self::$_envlock_file_path;
}
}

View File

@@ -0,0 +1,251 @@
<?php
/**
* Initializer class file.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @copyright 2013 2amigOS! Consultation Group LLC
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace Yiinitializr\Helpers;
use Yiinitializr\Helpers\Config;
use Yiinitializr\Helpers\ArrayX;
use Yiinitializr\Cli\Console;
/**
* Initializer provides a set of useful functions to initialize a Yii Application development.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @package Yiinitializr.helpers
* @since 1.0
*/
class Initializer
{
/**
* @param $root
* @param string $configName
* @param mixed $mergeWith
* @return mixed
* @throws Exception
*/
public static function create($root, $configName = 'main', $mergeWith = array('common', 'env'))
{
if (($root = realpath($root)) === false)
throw new Exception('could not initialize framework.');
$config = self::config($configName, $mergeWith);
if (php_sapi_name() !== 'cli') // aren't we in console?
$app = \Yii::createWebApplication($config); // create web
else
{
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
$app = \Yii::createConsoleApplication($config);
$app->commandRunner->addCommands($root . '/cli/commands');
$env = @getenv('YII_CONSOLE_COMMANDS');
if (!empty($env))
$app->commandRunner->addCommands($env);
}
// return an app
return $app;
}
/**
* @param string $configName config name to load (main, test, etc)
* @param null|string $mergeWith
* @return array
* @throws \Exception
*/
public static function config($configName = 'main', $mergeWith = null)
{
$files = array($configName);
$directory = Config::value('yiinitializr.app.directories.config.' . $configName);
if (null === $directory)
throw new \Exception("Unable to find 'yiinitializr.app.directories.config.'{$configName} on the settings.");
if (null !== $mergeWith)
{
if (is_array($mergeWith))
{
foreach($mergeWith as $file)
$files[] = $file;
}
else
$files[] = $mergeWith;
}
// do we have any other configuration files to merge with?
$mergedSettingFiles = Config::value('yiinitializr.app.files.config.' . $configName);
if (null !== $mergedSettingFiles)
{
if (is_array($mergedSettingFiles))
{
foreach($mergedSettingFiles as $file)
$files[] = $file;
}
else
$files[] = $mergedSettingFiles;
}
$config = self::build($directory, $files);
$params = isset($config['params'])
? $config['params']
: array();
self::setOptions($params);
return $config;
}
/**
* @param $directory
* @param $files array of configuration files to merge
* @return array
*/
public static function build($directory, $files)
{
$result = array();
if (!is_array($files))
$files = array($files);
foreach ($files as $file)
{
$config = file_exists($file) && is_file($file)
? require($file)
: (is_string($file) && file_exists($directory . '/' . $file . '.php')
? require($directory . '/' . $file . '.php')
: array());
if (is_array($config))
$result = ArrayX::merge($result, $config);
}
return $result;
}
/**
* Set php and yii options - some based on the loaded config params
* @param array $params The config params being used for the app
*/
protected static function setOptions(array $params)
{
// yii config
defined('YII_DEBUG') or define('YII_DEBUG', isset($params['yii.debug']) ? $params['yii.debug'] : false);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', isset($params['yii.traceLevel']) ? $params['yii.traceLevel'] : 0);
defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER', isset($params['yii.handleErrors']) ? $params['yii.handleErrors'] : true);
defined('YII_ENABLE_EXCEPTION_HANDLER') or define('YII_ENABLE_EXCEPTION_HANDLER', YII_ENABLE_ERROR_HANDLER);
// php config
error_reporting(-1);
if(isset($params['php.defaultCharset']))
ini_set('default_charset', $params['php.defaultCharset']);
if(isset($params['php.timezone']))
date_default_timezone_set($params['php.timezone']);
date_default_timezone_set($params['php.timezone']);
if(!class_exists('YiiBase'))
require(Config::value('yii.path').'/yii.php');
}
/**
* Helper function to build environment files
* @param $environment
* @throws \Exception
*/
public static function buildEnvironmentFiles($environment = 'dev')
{
self::output("\n%gBuilding environment files.%n");
umask(0);
$directories = Config::value('yiinitializr.app.directories.config');
if (null === $directories)
throw new \Exception("Unable to find 'yiinitializr.app.directories.config' on the settings.");
if (!is_array($directories))
$directories = array($directories);
$environment = strlen($environment)
? $environment
: 'dev';
foreach ($directories as $directory)
{
if (file_exists($directory))
{
$environment_directory = $directory . '/env';
if (!file_exists($environment_directory))
{
mkdir($environment_directory);
self::output("Your environment directory has been created: %r{$environment_directory}%n.\n");
}
$environment_file = $environment_directory . '/' . $environment . '.php';
if (!file_exists($environment_file))
{
file_put_contents($environment_file, "<?php\n/**\n * {$environment}.php\n */\n\nreturn array(\n);");
@chmod($environment_file, 0644);
self::output("%gEnvironment configuration file has been created: %r{$environment_file}%n.\n");
}
if (!file_exists($directory . '/env.php'))
{
@copy($environment_file, $directory . '/env.php');
self::output("Your environment configuration file has been created on {$directory}.\n");
} else
self::output("'{$directory}/env.php'\n%pfile already exists. No action has been executed.%n");
}
}
Config::createEnvironmentLockFile($environment);
self::output("%gEnvironment files creation process finished.%n\n");
}
/**
* @param string $name the name of the runtime folder,
* @throws \Exception
*/
public static function createRuntimeFolders($name = 'runtime')
{
self::output("\n%gBuilding runtime '{$name}' folders.%n");
umask(0);
$directories = Config::value('yiinitializr.app.directories.' . $name);
if (null === $directories)
throw new \Exception("Unable to find 'yiinitializr.app.directories.{$name}' on the settings.");
if (!is_array($directories))
$directories = array($directories);
foreach ($directories as $directory)
{
$runtime = $directory . '/' . $name;
if (!file_exists($runtime))
{
@mkdir($runtime, 02777);
self::output("Your {$name} folder has been created on {$directory}.");
} else
self::output("'{$name}' %pfolder already exists. No action has been executed.%n");
}
self::output("%gRuntime '{$name}' folders creation process finished.%n");
}
/**
* Outputs text only to console
* @param $message
*/
protected static function output($message)
{
if (php_sapi_name() === 'cli')
Console::output($message);
}
}