public function __construct($uri) { if (strpos($uri, '://') === false) { throw new Exception('The resource of uri ' . $uri . ' is not valid!'); } $this->uri = $uri; if (strpos($uri, "string://") === false) { $info = parse_url($uri); $proto = $info['scheme']; $clips_class = "Clips\\ResourceHandlers\\" . ucfirst($proto) . "ResourceHandler"; if (class_exists($clips_class)) { $handler = new $clips_class(); } else { // Using the configuration to load the handlers foreach (clips_config("resource_handlers") as $c) { if (isset($c->{$proto})) { $class = $c->{$proto}; if (class_exists($class)) { $handler = new $class(); } } } // We really can't find the handler, let's doing a load. if (!isset($handler)) { $tool =& get_clips_tool(); $handler = $tool->load_class(ucfirst($info['scheme']), true, new LoadConfig($tool->config->resource_handler_dir, "ResourceHandler", "ResourceHandlers\\")); } } } else { $handler = new ResourceHandlers\StringResourceHandler(); } if (!$handler) { throw new Exception('No handler found for resource of uri ' . $uri . ' !'); } $this->handler = $handler; }
/** * Get the configuration * * @author Jack * @date Sat Feb 21 10:52:02 2015 * @param name * The name of the config * @param default * if no configuration is found, the default value to be returned */ function config($name, $default = null) { return clips_config($name, $default); }
public function setUp() { $mute = getenv('MUTE_PHPUNIT'); $ref = new \ReflectionClass($this); $func = $this->getName(); if (!$mute && $func != 'testStub') { echo "\n----------" . $ref->name . " | " . $func . "----------\n"; } $this->tool =& get_clips_tool(); $this->tool->helper('fake'); $this->clips = new Engine(); $re = new \Addendum\ReflectionAnnotatedClass($this); foreach (array($re, $re->getMethod($func)) as $m) { foreach ($m->getAnnotations() as $a) { switch (get_class($a)) { case "Clips\\Rules": // We need to load the rules before test execution $this->clips->clear(); // We should clear the clips every time $rules = get_default($a, 'rules', array()); $templates = get_default($a, 'templates', array()); // Load the templates foreach ($templates as $t) { $this->clips->template($t); } // Load the rules foreach ($rules as $r) { $this->clips->load($r); } break; case "Clips\\FakeModel": $ds = $this->tool->library('dataSource'); $fake = $this->tool->library('fakeDataSource'); $ds->fake = $fake; if (valid_obj($this, 'Clips\\Interfaces\\FakeDataSourceHandler')) { $fake->handler = $this; } else { if (!$a->value) { $a->value = 'expectFakeDataSourceHandler'; } $fake->handler = $this->tool->library($a->value); $this->fake_handler = $fake->handler; } $ds->_datasources = array('fake'); break; case "Clips\\TestData": case "Clips\\DataGenerator": $this->data = $this->tool->enhance($a); break; case "Clips\\TestValue": if (isset($a->json)) { $a->file = $a->json; } if (isset($a->file)) { $test_config_dir = clips_config('test_data_dir'); if (!$test_config_dir) { $test_config_dir = clips_config('test_dir'); $test_config_dir = path_join($test_config_dir[0], 'data'); } else { $test_config_dir = $test_config_dir[0]; } $p = path_join($test_config_dir, $a->file); if (\file_exists($p)) { $this->value = \file_get_contents($p); if (isset($a->json)) { $this->value = parse_json($this->value); } } } else { if (isset($a->context)) { $this->value = clips_context($a->context); } else { if (isset($a->value)) { $this->value = $a->value; } } } break; default: $this->tool->annotationEnhance($a, $this); } } } $this->doSetUp(); }
/** * The interactive wizzard function, will take the options to run. * * The options is something like this(the generate widget option): * * <code> * { * "header": "Clips Widget Generator v1.0", * "steps": [ * { * "prompt": "The name of the widget:", * "field": "widget", * "rules": { * "minlength": 4, * "maxlength": 10 * } * }, * { * "prompt": "Widget Author:", * "field": "author", * "default": "!Clips\\current_user" * }, * { * "prompt": "Widget Version:", * "default": "1.0", * "field": "version" * }, * { * "prompt": "Widget Doc:", * "field": "doc" * }, * { * "prompt": "The widget configuration is:", * "call": "dump" * }, * { * "prompt": "Are you sure:", * "confirm": true, * "condition": { * "cancel": [{"jump": 0}] * } * } * ] * } * </code> * * Concept in details: * * 1. Step: The interactive step, can be 2 types [step, confirm] * 2. Operation: Only support 2 kind of operations by this version call and jump, for call * operation, will call the method from callback, for jump operation, will jump to the step * it set, like this {"jump":0} * 3. Confirm Step: This step will prompt a question to user, and let the user to choose, * use the condition part to add operation to the interactive console * 4. Field: If step has this settings, will prompt to get the user input using readline, and set * the input as field of the return object. * 5. Default: If no input is there, use this as default value, support method call, like this !time, * do not support arguments for this version */ function interactive($config, $callback = null) { if (is_string($config)) { // This is the config location $conf_dir = clips_config('config_dir'); foreach ($conf_dir as $dir) { $p = try_path(path_join($dir, $config . '.json')); if ($p) { break; } } $config = parse_json(file_get_contents($p)); } $prompt = get_default($config, 'prompt', '$'); if (isset($config->header)) { echo $config->header . "\n"; } $tool =& get_clips_tool(); $ret = array(); $validator = new Validator(); for ($i = 0; $i < count($config->steps); $i++) { $step = $config->steps[$i]; $operations = array(); $default = null; if (isset($step->field)) { if (isset($step->default)) { $default = $step->default; if (strpos($default, '!') === 0) { $default = call_user_func('\\' . str_replace('!', '', $default)); } } if (isset($step->prompt)) { $field = readline($step->prompt . (isset($default) ? ' (' . $default . ') ' : ' ')); } else { $field = readline($prompt . isset($default) ? ' (' . $default . ') ' : ' '); } if (!$field) { if (isset($default)) { $field = $default; } } if (isset($step->rules)) { // TODO Add the validation } $ret[$step->field] = $field; } else { if (isset($step->confirm)) { // This is the confirm step if (isset($step->confirm->options)) { $options = $step->confirm->options; } else { $options = array('yes', 'no'); } if (isset($step->prompt)) { $confirm = readline($step->prompt . ' [' . implode(', ', $options) . ']' . ' '); } else { $confirm = readline($prompt . ' '); } if (!is_array($step->confirm)) { $result_array = array('', 'y', 'Y', 'yes', 'Yes'); } else { $result_array = $step->confirm; } if (array_search($confirm, $result_array) !== false) { $condition = 'confirm'; } else { $condition = 'cancel'; } if (isset($step->condition)) { if (isset($step->condition->{$condition})) { $operations = array_merge($operations, $step->condition->{$condition}); } } } else { echo $step->prompt . "\n"; // Just print the prompt } } if (isset($step->jump)) { $operations[] = (object) array('jump' => $step->jump); } if (isset($step->call)) { $operations[] = (object) array('call' => $step->call); } if ($operations) { foreach ($operations as $operation) { if (isset($operation->jump)) { $i = $operation->jump - 1; } if ($callback && isset($operation->call)) { \call_user_func_array(array($callback, $operation->call), array($ret)); } } } } return (object) $ret; }