/** * find elements * * @param Charcoal_String|string $selector * @param Charcoal_Integer|integer $index */ public function find($selector, $index = NULL) { Charcoal_ParamTrait::validateString(1, $selector); Charcoal_ParamTrait::validateInteger(2, $index, TRUE); if (!$this->simple_html_dom) { _throw(new SimpleHtmlDomComponentException("SimpleHtmlDom object is not created")); } $selector = us($selector); log_debug("debug", "index:{$index}"); if ($index !== NULL) { // returns single element $index = ui($index); log_debug("debug", "returns single element"); $result = $this->simple_html_dom->find($selector, $index); log_debug("debug", "result: " . print_r($result, true)); return $result ? new Charcoal_SimpleHtmlDomElement($result) : NULL; } // returns all elements log_debug("debug", "selector:" . print_r($selector, true)); $result = $this->simple_html_dom->find($selector); log_debug("debug", "result: " . print_r($result, true)); $elements = array(); foreach ($result as $e) { $elements[] = new Charcoal_SimpleHtmlDomElement($e); } return $result ? $elements : array(); }
/** * execute exception handlers * * @param Exception $e exception to handle * * @return boolean TRUE means the exception is handled, otherwise FALSE */ public function handleException($e) { Charcoal_ParamTrait::validateException(1, $e); if ($e instanceof Charcoal_ProcessEventAtTaskManagerException) { $e = $e->getPrevious(); if ($e instanceof Charcoal_CreateObjectException) { $path = $e->getObjectPath(); echo "illegal object path: {$path}" . PHP_EOL; return true; } else { if ($e instanceof Charcoal_ObjectPathFormatException) { $path = $e->getObjectPath(); echo "bad object path: {$path}" . PHP_EOL; return true; } else { if ($e instanceof Charcoal_ModuleLoaderException) { $path = $e->getModulePath(); echo "failed to load module: {$path}" . PHP_EOL; return true; } else { if ($e instanceof Charcoal_InvalidShellArgumentException) { $option_name = $e->getOptionName(); $argument = $e->getArgument(); echo "invalid option[{$option_name}]: {$argument}" . PHP_EOL; return true; } } } } } return false; }
public static function splitParams($args) { Charcoal_ParamTrait::validateString(1, $args); $args = us($args); $pos = 0; $dq = FALSE; $sq = FALSE; $max_pos = strlen($args); $args_array = array(); $arg_tmp = ''; $escape = FALSE; while ($pos < $max_pos) { $ch = substr($args, $pos, 1); $escape_now = FALSE; if ($escape) { $arg_tmp .= $ch; } elseif ($dq) { if ($ch == '\\') { $escape = TRUE; $escape_now = TRUE; } elseif ($ch == '"') { $dq = FALSE; } else { $arg_tmp .= $ch; } } elseif ($sq) { if ($ch == '\\') { $escape = TRUE; $escape_now = TRUE; } elseif ($ch == "'") { $sq = FALSE; } else { $arg_tmp .= $ch; } } else { if ($ch == ' ') { $args_array[] = $arg_tmp; $arg_tmp = ''; } elseif ($ch == '\\') { $escape = TRUE; $escape_now = TRUE; } elseif ($ch == '"') { $dq = TRUE; } elseif ($ch == "'") { $sq = TRUE; } else { $arg_tmp .= $ch; } } if (!$escape_now && $escape) { $escape = FALSE; } $pos++; } if (strlen($arg_tmp) > 0) { $args_array[] = $arg_tmp; } return $args_array; }
/** * render buffer * * @param Charcoal_String|string $buffer rendering data */ public function render($buffer) { Charcoal_ParamTrait::validateString(1, $buffer); $buffer = us($buffer); if (is_string($buffer)) { echo $buffer; } }
/** * Construct */ public function __construct($message, $prev = NULL) { Charcoal_ParamTrait::validateString(1, $message); Charcoal_ParamTrait::validateException(2, $prev, TRUE); parent::__construct(us($message), 0, $prev); $this->backtrace = debug_backtrace(); $this->previous = $prev; }
public function __construct($component_name, $entry, $message = NULL, $prev = NULL) { Charcoal_ParamTrait::validateString(1, $component_name); Charcoal_ParamTrait::validateString(2, $entry); Charcoal_ParamTrait::validateString(3, $message); Charcoal_ParamTrait::validateException(4, $prev, TRUE); parent::__construct("component({$component_name}) config maybe wrong: [entry]{$entry} [message]{$message}", $prev); }
public function __construct($argument, $option_name, $prev = NULL) { Charcoal_ParamTrait::validateString(1, $argument); Charcoal_ParamTrait::validateString(2, $option_name); Charcoal_ParamTrait::validateException(3, $prev, TRUE); $this->argument = $argument; $this->option_name = $option_name; parent::__construct("Invalid argument for -{$option_name}: {$argument}", $prev); }
public function __construct($name, $value, $params) { Charcoal_ParamTrait::validateString(1, $name); Charcoal_ParamTrait::validateString(2, $value); Charcoal_ParamTrait::validateVector(3, $params); $this->name = us($name); $this->value = us($value); $this->params = uv($params); }
public function __construct($sandbox, $obj_path, $params = NULL) { Charcoal_ParamTrait::validateSandbox(1, $sandbox); Charcoal_ParamTrait::validateStringOrObjectPath(1, $obj_path); Charcoal_ParamTrait::validateHashMap(2, $params, TRUE); parent::__construct(p(array())); $this->_sandbox = $sandbox; $this->_obj_path = is_string(us($obj_path)) ? new Charcoal_ObjectPath($obj_path) : $obj_path; $this->_params = $params ? $params : m(array()); }
/** * Output HTML * */ public function output($e) { Charcoal_ParamTrait::validateException(1, $e); $out = ''; $version = Charcoal_Framework::getVersion(); $out .= "=============================================================" . PHP_EOL; $out .= "CharcoalPHP Ver.{$version}: Exception stack trace " . PHP_EOL; $out .= "=============================================================" . PHP_EOL; $out .= PHP_EOL; $out .= "* Exception Stack *" . PHP_EOL; $out .= "-------------------------------------------------------------" . PHP_EOL; $first_exception = $e; $no = 1; while ($e) { // get exception info $clazz = get_class($e); $file = $e->getFile(); $line = $e->getLine(); $message = $e->getMessage(); // print exception info $out .= "[{$no}]{$clazz}" . PHP_EOL; $out .= " {$file}({$line})" . PHP_EOL; $out .= " {$message}" . PHP_EOL; // move to previous exception $e = method_exists($e, 'getPreviousException') ? $e->getPreviousException() : NULL; $no++; if ($e) { $out .= PHP_EOL; } } // get backtrace if ($first_exception instanceof Charcoal_CharcoalException) { $backtrace = $first_exception->getBackTrace(); $out .= PHP_EOL; $out .= "* Call Stack *" . PHP_EOL; $out .= "-------------------------------------------------------------" . PHP_EOL; // print backtrace $call_no = 0; foreach ($backtrace as $element) { $klass = isset($element['class']) ? $element['class'] : ''; $func = isset($element['function']) ? $element['function'] : ''; $type = isset($element['type']) ? $element['type'] : ''; $file = isset($element['file']) ? $element['file'] : ''; $line = isset($element['line']) ? $element['line'] : ''; if ($call_no > 0) { $out .= PHP_EOL; } $out .= "[{$call_no}]{$klass}{$type}{$func}()" . PHP_EOL; $out .= " {$file}({$line})" . PHP_EOL; $call_no++; } } return $out; }
/** * indicates FROM clause and switches to from context * * @param Charcoal_String $model_name model name for table * @param Charcoal_String $alias alias name for table * * @return Charcoal_FromContext from context */ public function from($model_name, $alias = NULL) { Charcoal_ParamTrait::validateString(1, $model_name); Charcoal_ParamTrait::validateString(2, $alias, TRUE); $query_target = new Charcoal_QueryTarget($model_name); if ($alias) { $query_target->setAlias($alias); } $this->getContext()->setQueryTarget($query_target); return new Charcoal_FromContext($this->getContext()); }
/** * execute exception handlers * * @param Exception $e exception to handle * * @return boolean TRUE means the exception is handled, otherwise FALSE */ public function handleException($e) { Charcoal_ParamTrait::validateException(1, $e); if ($e instanceof Charcoal_HttpStatusException) { $status_code = $e->getStatusCode(); // Show HTTP error document self::showHttpErrorDocument($status_code); log_warning('system,error', 'exception', "http_exception: status_code={$status_code}"); return TRUE; } return FALSE; }
public function __construct($path, $parent = NULL) { Charcoal_ParamTrait::validateString(1, $path); Charcoal_ParamTrait::validateIsA(2, 'Charcoal_File', $parent, TRUE); parent::__construct($path, $parent); if (!is_readable(parent::getPath())) { _throw(new Charcoal_FileNotFoundException($this)); } $this->_data = getimagesize(parent::getPath()); if ($this->_data === FALSE) { _throw(new Charcoal_ImageGetSizeException($this)); } }
public static function makeAbsoluteURL($sandbox, $obj_path, $params = NULL) { Charcoal_ParamTrait::validateSandbox(1, $sandbox); Charcoal_ParamTrait::validateStringOrObject(2, 'Charcoal_ObjectPath', $obj_path); Charcoal_ParamTrait::validateHashMap(3, $params, TRUE); // サーバ名 $url = 'http://' . $_SERVER['SERVER_NAME']; // サーバのパス $url .= dirname($_SERVER['SCRIPT_NAME']); // 相対部分を追加 $url .= self::makeRelativeURL($sandbox, $obj_path, $params); return $url; }
/** * Generate RDBMS-specific SQL for CREATE TABLE * * @param Charcoal_ITableModel $model table model object related with th query * * @return string SQL */ public function buildCreateTableSQL(Charcoal_ITableModel $model) { Charcoal_ParamTrait::validateIsA(1, 'Charcoal_ITableModel', $model); try { $field_list = $model->getFieldList(); $SQL_field_list = array(); foreach ($field_list as $name) { $pk = $model->getAnnotation($name, 'pk'); $type = $model->getAnnotation($name, 'type'); $notnull = $model->getAnnotation($name, 'notnull'); $charset = $model->getAnnotation($name, 'charset'); $serial = $model->getAnnotation($name, 'serial'); $default = $model->getAnnotation($name, 'default'); $type_name = $this->mapType($type->getValue()); $length = $type->getParameter(); if ($length) { $field_expr = $name . ' ' . $type_name . '(' . $length . ')'; } else { $field_expr = $name . ' ' . $type_name; } if ($pk) { if ($serial) { $field_expr = $name . ' serial primary key'; } if ($notnull) { $field_expr .= ' NOT NULL'; } } else { if ($default) { $field_expr .= ' DEFAULT ' . $default->getValue(); } if ($charset) { $field_expr .= " CHARACTER SET {$charset}"; } if ($notnull) { $field_expr .= ' NOT NULL'; } } $SQL_field_list[] = $field_expr; } $SQL_field_list = implode(",\n", $SQL_field_list); $table_name = $model->getTableName(); $sql = "CREATE TABLE {$table_name} (\n {$SQL_field_list} \n ) \n"; return $sql; } catch (Exception $e) { _throw(new Charcoal_SQLBuilderException("PostgreSQL_SQLBuilder#buildCreateTableSQL failed")); } }
/** * Constructor */ public function __construct($page_info, $where = NULL, $params = NULL, $order_by = NULL, $group_by = NULL) { Charcoal_ParamTrait::validateIsA(1, 'Charcoal_DBPageInfo', $page_info); Charcoal_ParamTrait::validateString(2, $where, TRUE); Charcoal_ParamTrait::validateVector(2, $params, TRUE); Charcoal_ParamTrait::validateString(3, $order_by, TRUE); Charcoal_ParamTrait::validateString(4, $group_by, TRUE); $this->page_info = $page_info; $page = $page_info->getPage(); $page_size = $page_info->getPageSize(); $page = ui($page); $page_size = ui($page_size); $limit = $page_size; $offset = ($page - 1) * $page_size; parent::__construct($where, $params, $order_by, $limit, $offset, $group_by); }
/** * execute exception handlers * * @param Exception $e exception to handle * * @return boolean TRUE means the exception is handled, otherwise FALSE */ public function handleException($e) { Charcoal_ParamTrait::validateException(1, $e); log_info("system, debug", "exception", "handled a framework exception!"); try { /** @var Charcoal_IDebugtraceRenderer $renderer */ $renderer = $this->getSandbox()->createObject('console', 'debugtrace_renderer', array(), 'Charcoal_IDebugtraceRenderer'); // Render exception $renderer->render($e); return TRUE; } catch (Exception $e) { _catch($e); log_info("system, debug", "exception", "debugtrace_renderer creation failed."); } return FALSE; }
/** * render buffer * * @param Charcoal_String|string $buffer rendering data */ public function render($buffer) { Charcoal_ParamTrait::validateString(1, $buffer); $buffer = us($buffer); if (is_string($buffer)) { $dir = new Charcoal_File(dirname($this->file_path)); if (!$dir->exists()) { $dir->makeDirectory($this->dir_mode); } if (!$dir->isWriteable()) { _throw(new Charcoal_RenderTargetException("directory not writable: {$dir}")); } $result = @file_put_contents($this->file_path, $buffer, LOCK_EX); if ($result === FALSE) { $last_error = print_r(error_get_last(), true); _throw(new Charcoal_RenderTargetException("file_put_contents failed: {$this->file_path} last error: {$last_error}")); } } }
/** * find elements * * @param Charcoal_String|string $selector * @param Charcoal_Integer|integer $index */ public function find($selector, $index = NULL) { Charcoal_ParamTrait::validateString(1, $selector); Charcoal_ParamTrait::validateInteger(2, $index, TRUE); $selector = us($selector); if ($index !== NULL) { // returns single element $index = ui($index); $result = $this->element->find($selector, $index); return $result ? new Charcoal_SimpleHtmlDomElement($result) : NULL; } // returns all elements $result = $this->element->find($selector); $elements = array(); foreach ($result as $e) { $elements[] = new Charcoal_SimpleHtmlDomElement($e); } return $result ? $elements : array(); }
/** * execute exception handlers * * @param Exception $e exception to handle * * @return boolean TRUE means the exception is handled, otherwise FALSE */ public function handleException($e) { Charcoal_ParamTrait::validateException(1, $e); log_info('system, debug', "handled a framework exception!", 'exception_handler'); // Create Debug Trace Renderer try { $debugtrace_renderer = $this->getSandbox()->createObject('html', 'debugtrace_renderer', array(), 'Charcoal_IDebugtraceRenderer'); log_info('system, debug', "debugtrace_renderer[{$debugtrace_renderer}] created.", 'exception_handler'); // Render exception $error_html = $debugtrace_renderer->output($e); log_info('system, debug', "debugtrace_renderer[{$debugtrace_renderer}] output html.", 'exception_handler'); // generate error dump(HTML) log_error('error_dump', $error_html, 'exception_handler'); log_info('system, debug', "error_html:\n{$error_html}", 'exception_handler'); return TRUE; } catch (Exception $e) { _catch($e); log_info('system, debug', "debugtrace_renderer[{$debugtrace_renderer}] creation failed.", 'exception_handler'); } return FALSE; }
/** * execute exception handlers * * @param Exception $e exception to handle * * @return boolean TRUE means the exception is handled, otherwise FALSE */ public function handleException($e) { Charcoal_ParamTrait::validateException(1, $e); if ($e instanceof Charcoal_HttpStatusException) { $status_code = $e->getStatusCode(); // ヘッダ文字列 $status_message_file = Charcoal_ResourceLocator::getFrameworkPath('preset', 'status_messages.ini'); if (!is_file($status_message_file)) { // log_warning( 'system,debug,error', 'framework',"ステータスメッセージファイル($status_message_file)が存在しません。"); } $status_messages = parse_ini_file($status_message_file, FALSE); if (FALSE === $status_messages) { // log_warning( 'system,debug,error', 'framework',"ステータスメッセージファイル($status_message_file)の読み込みに失敗しました。"); } $header_msg = isset($status_messages[$status_code]) ? $status_messages[$status_code] : ''; // HTTPバージョン文字列 $http_ver = $this->http_version; // ヘッダ出力 header("HTTP/{$http_ver} {$status_code} {$header_msg}", true, $status_code); log_error('system,debug,error', "HTTP/1.0 {$status_code} {$header_msg}", 'framework'); } return FALSE; }
public function createFile($file_path, $contents, $overwrite = TRUE, $mode = 0777) { Charcoal_ParamTrait::validateString(1, $file_path); Charcoal_ParamTrait::validateString(2, $contents); Charcoal_ParamTrait::validateBoolean(3, $overwrite); Charcoal_ParamTrait::validateInteger(4, $mode); $obj = new Charcoal_File($file_path, $this->_base_dir_obj); if ($overwrite) { if ($obj->exists() && !$obj->canWrite()) { _throw(new Charcoal_FileSystemComponentException('specified file is not writeable.')); } } elseif ($obj->exists()) { _throw(new Charcoal_FileSystemComponentException('specified file is already exists.')); } try { // create file with parent directory $obj->makeFile($mode, $contents, TRUE); return $obj; } catch (Exception $e) { _catch($e); _throw(new Charcoal_FileSystemComponentException(s('creating file failed.'), $e)); } }
/** * Render debug trace * */ public function render($e) { Charcoal_ParamTrait::validateException(1, $e); fputs(STDERR, $this->output($e)); return TRUE; }
/** * set author * * @param Charcoal_String|string $author */ public function setAuthor($author) { Charcoal_ParamTrait::validateString(1, $author, TRUE); $this->item->author = us($author); return $this; }
/** * create * */ public function create($params) { Charcoal_ParamTrait::validateHashMap(1, $params); $this->pager = @Pager::factory($params); }
public function execute($sql) { Charcoal_ParamTrait::validateString(1, $sql); // 接続処理 $this->connect(); // SQLを実行 $sqliteerror = ''; $result = $this->sqlite_exec($sql, SQLITE_BOTH, $sqliteerror); if ($result === FALSE) { _throw(new Charcoal_DBDataSourceException(__METHOD__ . " Failed. SQLite error: {$sqliteerror}")); } }
/** * generate feed and output to the buffer * * @param Charcoal_String|string $format format name of the feed */ public function outputFeed($format = self::DEFAULT_FORMAT) { Charcoal_ParamTrait::validateString(1, $format); echo 'outputFeed' . PHP_EOL; $this->creator->outputFeed(us($format)); }
/** * Prepare for statement and execute query * * @param string|Charcoal_String $sql SQL statement(placeholders can be included) * @param array|Charcoal_HashMap $params Parameter values for prepared statement * @param array|Charcoal_HashMap $driver_options Driver options * * @return PDOStatement */ private function _prepareExecute($sql, $params, $driver_options) { Charcoal_ParamTrait::validateString(1, $sql); Charcoal_ParamTrait::validateHashMap(2, $params, TRUE); Charcoal_ParamTrait::validateHashMap(3, $driver_options, TRUE); $params = um($params); $driver_options = um($driver_options); $params = $params ? $params : array(); $driver_options = $driver_options ? $driver_options : array(); $timer_handle = Charcoal_Benchmark::start(); $command_id = $this->command_id++; $params_disp = $params ? implode(',', $params) : ''; log_debug('data_source,sql,debug', "[ID]{$command_id} [SQL]{$sql}", self::TAG); log_debug('data_source,sql,debug', "[ID]{$command_id} [params]{$params_disp}", self::TAG); /** @var PDOStatement $stmt */ $stmt = $this->getConnection()->prepare($sql, $driver_options); $this->sql_histories->push(new Charcoal_SQLHistory($sql, $params)); $success = $stmt->execute($params); if (!$success) { list($sqlstate, $err_code, $err_msg) = $stmt->errorInfo(); $msg = "PDO#execute failed. [ID]{$command_id} [SQL]{$sql} [params]{$params_disp} [SQLSTATE]{$sqlstate} [ERR_CODE]{$err_code} [ERR_MSG]{$err_msg}"; log_error('data_source,sql,debug', "...FAILED: {$msg}", self::TAG); _throw(new Charcoal_DBDataSourceException($msg)); } $this->num_rows = $rows = $stmt->rowCount(); log_info('data_source,sql,debug', "[ID]{$command_id} ...success(numRows={$rows})", self::TAG); // ログ $elapse = Charcoal_Benchmark::stop($timer_handle); log_debug('data_source,sql,debug', "[ID]{$command_id} _prepareExecute() end. time=[{$elapse}]msec.", self::TAG); // SQL benchmark ini_set('serialize_precision', 16); log_debug('sql_bench', var_export(array($sql, $rows, $elapse), true), self::TAG); return $stmt; }
public function __construct($url) { Charcoal_ParamTrait::validateString(1, $url); parent::__construct(p(array())); $this->url = $url; }
public function fetchAssoc($result) { Charcoal_ParamTrait::validateResource(1, $result); $flag = new Charcoal_ErrorReportingSwitcher(0, E_DEPRECATED); if (!is_object($result)) { _throw(new Charcoal_NonObjectException($result)); } return $result->fetchRow(DB_FETCHMODE_ASSOC); }