@ 2014-04-08T14:04:17Z <?php
/**
* SETTINGS.
*
* Objects of class Settings are injected to factories
* as initiators of various classes.
*
* Process:
* - FUNCTIONS.PHP: Create object Settings().
* - FUNCTIONS.PHP: Set args by Settings:set().
* - FUNCTIONS.PHP: Create object Factory($settings).
* - CLASS-FACTORY.PHP: Create functionality objects according to settings.
* - CLASS-WORKER.PHP: Parent functionality.
*
* @author Petr Cibulka <cibulka.me@gmail.com>
* @link http://cibulka.me
*
* @package Cibulka_Framework\Architecture
* @since 1.0
* @version 1.0
*
* @filesource
*/
// ----------------------------------------------------------------------------------
/**
* SETTINGS CLASS.
*
* @used-by Factory()
* @package Cibulka_Framework\Architecture
*
*/
// ----------------------------------------------------------------------------------
class Settings {
private $prefix = '';
private $spec = [];
private $workers = [];
/**
* CONSTRUCTOR.
*
* Sets prefix. The rest is done by Settings::setters();
*
* @param string $prefix Sets prefix for all chained classes.
*
*/
function __construct($prefix) {
$this->prefix = $prefix;
}
// --------------------------------------------------------------------------
/**
* SETTER.
*
* Note: If unique worker needed, leave ID blank
* => arg TYPE will act both like type of functionality AND unique ID.
*
* @param string $type Sets type of functionality. If class should be unique, acts as ID as well.
* @param string $id
* @param array $args Args are later validated by Worker().
*
*/
public function set($type,$id='',$args='') {
/*ID*/
$id = $this->get_id($type,$id);
if (!$id) { return false; }
/*SPEC*/
$spec = $this->create_spec($type,$id,$args);
/*VALIDATION & TRANSLATION*/
$v = new Validator();
try {
// SPEC: Validation
$v->spec_validate($type);
// ARGS: Translation
$v->args_translate($args,$type);
// ARGS: Validation
$v->args_validate($args,$type);
} catch (Exception $e) {
$message = $e->getMessage();
$line = $e->getLine();
$file = $e->getFile();
$trace = $e->getTrace();
?>
<table>
<tr>
<th colspan="2"><?php echo $message; ?></th>
</tr><tr>
<td>Line: <?php echo $line; ?></td>
<td>File: <?php echo $file; ?></td>';
</tr><tr>
</tr><tr>
<td colspan="2">Trace: <?php echo $trace; ?></td>
</tr>
</table>
<?php
return false;
}
/*ARGS*/
$this->workers[$id]['type'] = $type;
$this->workers[$id]['args'] = (!empty($args)) ? $args : array();
/*TEST*/
//helper::printr($this);
}
// --------------------------------------------------------------------------
private function get_id($type,$id) {
$id = (!empty($id)) ? $id : $type;
// Error
// TODO: Create proper exception
if (array_key_exists($id,$this->workers)) {
$text = 'Worker with ID '.$id.' already exists.';
$error = new Error($this,$text);
trigger_error();
return false;
}
return $id;
}
private function create_spec($type,$id,$args) {
$spec_name = ucfirst($this->prefix).'_'.ucfirst($type).'_Spec';
// Initiate, if not already
if (!array_key_exists($type, $this->spec)) {
$this->spec[$type] = new $spec_name($type,$id);
}
return $spec_name;
}
// --------------------------------------------------------------------------
private function spec_validate($type) {
$spec = $this->spec[$type];
foreach ($spec as $spec_term => $spec_row) {
// Conflicting spec keys
// req & def
if (array_key_exists('req',$spec_row)) {}
// wrong type to check
if (!empty($spec_row['type'])) {
$type = helper::str_to_array($spec_row['type']);
$type_supported = array('boolean','integer','double','string','array','object','resource','null','file');
$type_unsupported = array_diff($type,$type_supported);
if ($type_unsupported) {
trigger_error('Unsupported type!');
return false;
}
}
// wrong count
if (!empty($spec_row['count'] && is_string($spec_row['count'])) {
// max & min
// range
}
}
return true;
}
private function args_translate($args,$type) {
$spec = $this->spec[$type];
foreach ($spec as $spec_term => $spec_row) {
/*KEYS & DEF values*/
$spec_def_value = (!empty($property_row['def'])) ? $property_row['def'] : null;
// SET
if (!empty($this->args[$spec_term])) {
continue;
} else {
$this->args[$spec_term] = $spec_def_value;
}
}
return true;
}
private function args_validate($args,$type) {
$spec = $this->spec[$type]->get_all();
/*SHOULD WE VALIDATE?*/
if (!$spec) {
// ARGS disabled & Not set => ok
if (empty($this->args)) {
return;
}
// ARGS disabled & set => not ok
else {
trigger_error('ARGS are not allowed');
}
}
/*UNSUPPORTED SPEC*/
$args_terms_unsupported = array_diff_key($this->args, $spec);
if ($args_terms_unsupported) {
trigger_error('Unsupported keys: '.implode(', ', $args_terms_unsupported));
return false;
}
/*LOOP THROUGH SPEC*/
foreach ($spec as $spec_term => $spec_row) {
$user_value = $this->args[$spec_term];
// ERROR: Required keys missing
$req = $spec_row['req'];
if ($req && empty($user_value)) {
trigger_error('Required ARG '.$spec_term.' is empty.');
return false;
}
// CONTINUE if $user_val empty
if (empty($user_value)) { continue; }
// ERROR: Count
if (!empty($spec_row['count'])) {
$user_count = count(helper::str_to_array($user_value));
$count_error = $this->user_val_count($user_count,$spec_row['count']);
if ($count_error) {
trigger_error('Wrong count on '.$spec_term);
}
}
// ERROR: TYPE: File
if (!empty($spec_row['type']) && !is_array($spec_row['type']) && 'file' === $spec_row['type']) {
// Escape type checking later
$type_escape = true;
// Error: File is not a string
$user_value_is_string = is_string($user_value);
if (!$user_value_is_string) {
trigger_error('File must be string.');
return false;
}
// Error: File does not exist
$file_exists = $this->check_file($user_value);
if (!$file_exists) {
trigger_error('File has weird SRC.');
return false;
}
}
// ERROR: TYPE: Common types
if (!empty($spec_row['type'] && !$type_escape) {
$user_value_type_supported = in_array(gettype($user_value),helper::str_to_array($spec_types));
if (!$user_value_type_supported) {
trigger_error('Value type is not supported');
return false;
}
}
// ERROR: VALUES
if (!empty($spec_row['vals'])) {
$user_val_unsupported = array_diff(helper::str_to_array($user_val),$spec_val);
if ($user_val_unsupported) {
trigger_error('Value is unsupported');
return false;
}
}
}
return true;
}
}
// ------------------------------------------------------------------------------
interface Spec_Interface {
public function set($property,$key,$value='');
public function get($property);
}
interface Spec_Interface_Child {
public function spec_set();
}
abstract class Spec implements Spec_Interface {
protected $worker = array(
'type' => '',
'id' => ''
);
// --------------------------------------------------------------------------
function __construct($type,$id) {
$this->worker['type'] = $type;
$this->worker['id'] = $id;
/*SPEC*/
// Sets default values & allowed values
if (method_exists($this,'spec_set')) {
try {
// parent
$this->spec_set();
// child
if (method_exists($this,'spec_set_child')) {
$this->spec_set_child();
}
} catch (Exception $e) {
}
}
}
// --------------------------------------------------------------------------
// TODO: Exceptions
function set($property,$spec_key,$value='') {
$allowed_keys = array('def','vals');
if (!in_array($spec_key,$allowed_keys)) {
trigger_error('TYPES accepted: '.implode(', ', $allowed_keys).'. Type provided: <b>'.$spec_key.'</b>.');
}
if (property_exists($this,$property)) {
$this->{$property}[$spec_key] = $value;
} else {
trigger_error('Property <b>'.$spec_key.'</b> is not supported.');
}
}
function get($property) {
if (property_exists($this,$property)) {
return $this->{$property};
} else {
trigger_error('Property '.$property.' does not exist.');
}
}
function get_all() {
return get_object_vars($this);
}
}
abstract class Scripts_Spec extends Spec implements Spec_Interface_Child {
protected
$script_type = array(
'req' => true,
'num' => 1,
'vals' => array('wp','admin','login')
);
protected
$load = array(
'req' => false,
'num' => 1,
'type' => array('string','object'),
'vals' => array('post_edit')
);
protected
$autoload = array(
'req' => false,
'num' => 1,
'type' => 'boolean',
'def' => false
);
protected
$local = array(
'req' => false,
'num' => 1,
'type' => 'boolean',
'def' => true
);
protected
$id = array(
'req' => false,
'num' => 1,
'type' => 'string',
'def' => ''
);
protected
$src = array(
'req' => false,
'num' => 1,
'type' => 'string'
);
protected
$deps = array(
'req' => false,
'num' => '',
'type' => array('string','array')
);
protected
$ver = array(
'req' => false,
'num' => 1,
'type' => 'string'
);
function spec_set() {
// ID > Def: id of worker
$this->set('id','def',$this->worker['id']);
}
}
class Scripts_Js_Spec extends Scripts_Spec {
}
class Scripts_Css_Spec extends Scripts_Spec {
}
?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.4.27 0.003 0.057 18.66 5.4.26 0.020 0.057 18.88 5.4.25 0.010 0.053 18.77 5.4.24 0.020 0.043 18.96 5.4.23 0.020 0.067 18.76 5.4.22 0.017 0.073 18.98 5.4.21 0.017 0.053 18.96 5.4.20 0.013 0.080 18.74 5.4.19 0.013 0.073 19.02 5.4.18 0.013 0.047 18.88 5.4.17 0.017 0.060 18.88 5.4.16 0.023 0.037 18.92 5.4.15 0.007 0.077 18.64 5.4.14 0.000 0.073 16.52 5.4.13 0.000 0.060 16.46 5.4.12 0.003 0.053 16.58 5.4.11 0.017 0.057 16.50 5.4.10 0.013 0.053 16.39 5.4.9 0.010 0.053 16.57 5.4.8 0.003 0.073 16.47 5.4.7 0.010 0.053 16.50 5.4.6 0.020 0.060 16.40 5.4.5 0.010 0.050 16.45 5.4.4 0.013 0.057 16.41 5.4.3 0.010 0.057 16.45 5.4.2 0.010 0.067 16.36 5.4.1 0.003 0.060 16.50 5.4.0 0.013 0.047 15.77 5.3.28 0.023 0.053 14.58 5.3.27 0.017 0.060 14.50 5.3.26 0.017 0.060 14.57 5.3.25 0.007 0.077 14.73 5.3.24 0.007 0.060 14.45 5.3.23 0.007 0.050 14.78 5.3.22 0.017 0.043 14.63 5.3.21 0.020 0.070 14.47 5.3.20 0.007 0.053 14.66 5.3.19 0.007 0.050 14.54 5.3.18 0.013 0.060 14.53 5.3.17 0.013 0.057 14.63 5.3.16 0.017 0.043 14.73 5.3.15 0.010 0.053 14.78 5.3.14 0.007 0.060 14.68 5.3.13 0.010 0.073 14.61 5.3.12 0.020 0.057 14.51 5.3.11 0.007 0.053 14.63 5.3.10 0.020 0.050 14.12 5.3.9 0.007 0.050 14.09 5.3.8 0.010 0.057 13.84 5.3.7 0.017 0.043 14.24 5.3.6 0.017 0.050 14.17 5.3.5 0.003 0.060 13.77 5.3.4 0.013 0.060 14.01 5.3.3 0.010 0.050 14.12 5.3.2 0.010 0.047 13.66 5.3.1 0.010 0.043 13.73 5.3.0 0.017 0.040 13.71 5.2.17 0.007 0.040 10.97 5.2.16 0.010 0.033 11.23 5.2.15 0.017 0.047 11.32 5.2.14 0.007 0.057 11.23 5.2.13 0.017 0.053 11.32 5.2.12 0.007 0.047 11.11 5.2.11 0.003 0.060 11.12 5.2.10 0.007 0.037 11.27 5.2.9 0.000 0.067 11.16 5.2.8 0.010 0.057 11.16 5.2.7 0.010 0.040 11.25 5.2.6 0.010 0.043 11.13 5.2.5 0.017 0.030 11.18 5.2.4 0.010 0.047 10.83 5.2.3 0.003 0.043 11.06 5.2.2 0.007 0.040 10.86 5.2.1 0.000 0.057 11.03 5.2.0 0.007 0.047 10.79 5.1.6 0.007 0.030 10.00 5.1.5 0.007 0.047 9.99 5.1.4 0.010 0.043 9.89 5.1.3 0.003 0.037 10.41 5.1.2 0.010 0.047 10.57 5.1.1 0.010 0.030 10.27 5.1.0 0.013 0.037 10.07 5.0.5 0.013 0.033 8.61 5.0.4 0.003 0.033 8.51 5.0.3 0.003 0.053 8.32 5.0.2 0.010 0.030 8.20 5.0.1 0.010 0.030 8.23 5.0.0 0.000 0.047 7.95 4.4.9 0.007 0.027 7.25 4.4.8 0.013 0.017 7.25 4.4.7 0.003 0.020 7.25 4.4.6 0.003 0.020 7.25 4.4.5 0.003 0.023 7.25 4.4.4 0.003 0.037 7.25 4.4.3 0.007 0.023 7.25 4.4.2 0.007 0.020 7.25 4.4.1 0.003 0.030 7.24 4.4.0 0.007 0.037 7.24 4.3.11 0.007 0.023 7.24 4.3.10 0.007 0.020 7.24 4.3.9 0.007 0.017 7.24 4.3.8 0.000 0.037 7.24 4.3.7 0.000 0.030 7.24 4.3.6 0.000 0.030 7.24 4.3.5 0.000 0.027 7.24 4.3.4 0.007 0.040 7.24 4.3.3 0.003 0.027 7.24 4.3.2 0.010 0.020 7.24 4.3.1 0.003 0.030 7.24 4.3.0 0.007 0.030 6.99
preferences:dark mode live preview
140.87 ms | 1394 KiB | 7 Q