示例#1
0
 public function query($type, $sql, $as_object = false, array $params = NULL)
 {
     // Make sure the database is connected
     $this->_connection or $this->connect();
     $benchmark = false;
     if (config('debug')) {
         // Benchmark this query for the current instance
         $benchmark = \mii\util\Profiler::start("Sphinx ({$this->_instance})", $sql);
     }
     // Execute the query
     if (($result = $this->_connection->query($sql)) === false) {
         if ($benchmark) {
             // This benchmark is worthless
             \mii\util\Profiler::delete($benchmark);
         }
         throw new DatabaseException(':error [ :query ]', [':error' => $this->_connection->error, ':query' => $sql], $this->_connection->errno);
     }
     if ($benchmark) {
         \mii\util\Profiler::stop($benchmark);
     }
     // Set the last query
     $this->last_query = $sql;
     if ($type === Database::SELECT) {
         return $result->fetch_all(MYSQLI_ASSOC);
     } elseif ($type === Database::INSERT) {
         // Return a list of insert id and rows created
         return [$this->_connection->insert_id, $this->_connection->affected_rows];
     } else {
         // Return the number of rows affected
         return $this->_connection->affected_rows;
     }
 }
示例#2
0
文件: Router.php 项目: levmorozov/mii
 public function match($uri)
 {
     $benchmark = false;
     if (config('debug')) {
         $benchmark = \mii\util\Profiler::start('Router match', $uri);
     }
     if ($uri !== '/') {
         $uri = trim($uri, '//');
     }
     foreach ($this->_routes_list as $pattern => $route) {
         $result = $this->match_route($uri, $pattern, $route);
         if ($result !== false) {
             $this->_current_route = $pattern;
             if ($benchmark) {
                 \mii\util\Profiler::stop($benchmark);
             }
             return $result;
         }
     }
     if ($benchmark) {
         \mii\util\Profiler::stop($benchmark);
     }
     return false;
 }
示例#3
0
 /**
  * Gets the min, max, average and total of profiler groups as an array.
  *
  *     $stats = Profiler::group_stats('test');
  *
  * @param   mixed   $groups single group name string, or array with group names; all groups by default
  * @return  array   min, max, average, total
  * @uses    Profiler::groups
  * @uses    Profiler::stats
  */
 public static function group_stats($groups = NULL)
 {
     // Which groups do we need to calculate stats for?
     $groups = $groups === NULL ? Profiler::groups() : array_intersect_key(Profiler::groups(), array_flip((array) $groups));
     // All statistics
     $stats = array();
     foreach ($groups as $group => $names) {
         foreach ($names as $name => $tokens) {
             // Store the stats for each subgroup.
             // We only need the values for "total".
             $_stats = Profiler::stats($tokens);
             $stats[$group][$name] = $_stats['total'];
         }
     }
     // Group stats
     $groups = array();
     foreach ($stats as $group => $names) {
         // Min and max are unknown by default
         $groups[$group]['min'] = $groups[$group]['max'] = array('time' => NULL, 'memory' => NULL);
         // Total values are always integers
         $groups[$group]['total'] = array('time' => 0, 'memory' => 0);
         foreach ($names as $total) {
             if (!isset($groups[$group]['min']['time']) or $groups[$group]['min']['time'] > $total['time']) {
                 // Set the minimum time
                 $groups[$group]['min']['time'] = $total['time'];
             }
             if (!isset($groups[$group]['min']['memory']) or $groups[$group]['min']['memory'] > $total['memory']) {
                 // Set the minimum memory
                 $groups[$group]['min']['memory'] = $total['memory'];
             }
             if (!isset($groups[$group]['max']['time']) or $groups[$group]['max']['time'] < $total['time']) {
                 // Set the maximum time
                 $groups[$group]['max']['time'] = $total['time'];
             }
             if (!isset($groups[$group]['max']['memory']) or $groups[$group]['max']['memory'] < $total['memory']) {
                 // Set the maximum memory
                 $groups[$group]['max']['memory'] = $total['memory'];
             }
             // Increase the total time and memory
             $groups[$group]['total']['time'] += $total['time'];
             $groups[$group]['total']['memory'] += $total['memory'];
         }
         // Determine the number of names (subgroups)
         $count = count($names);
         // Determine the averages
         $groups[$group]['average']['time'] = $groups[$group]['total']['time'] / $count;
         $groups[$group]['average']['memory'] = $groups[$group]['total']['memory'] / $count;
     }
     return $groups;
 }
示例#4
0
 /**
  * Executes all validation rules. This should
  * typically be called within an if/else block.
  *
  *     if ($validation->check())
  *     {
  *          // The data is valid, do something here
  *     }
  *
  * @return  boolean
  */
 public function check()
 {
     $benchmark = false;
     if (config('debug')) {
         // Start a new benchmark
         $benchmark = \mii\util\Profiler::start('Validation', __FUNCTION__);
     }
     $this->_errors = [];
     // Get a list of the expected fields
     $expected = Arr::merge(array_keys($this->_data), array_keys($this->_labels));
     // Import the rules locally
     $rules = $this->_rules;
     $data = [];
     foreach ($expected as $field) {
         // Use the submitted value or NULL if no data exists
         $data[$field] = isset($this->_data[$field]) ? $this->_data[$field] : null;
     }
     // Overload the current array with the new one
     $this->_data = $data;
     // Execute the rules
     foreach ($rules as $field => $set) {
         // Get the field value
         $value = $this->_data[$field];
         foreach ($set as $array) {
             // Rules are defined as array($rule, $params)
             list($rule, $params) = $array;
             array_unshift($params, $value);
             // Default the error name to be the rule (except array and lambda rules)
             $error_name = $rule;
             if (is_array($rule)) {
                 // Allows rule('field', array(':model', 'some_rule'));
                 if (is_string($rule[0]) and array_key_exists($rule[0], $this->_bound)) {
                     // Replace with bound value
                     $rule[0] = $this->_bound[$rule[0]];
                 }
                 // This is an array callback, the method name is the error name
                 $error_name = $rule[1];
                 $passed = call_user_func_array($rule, $params);
             } elseif (!is_string($rule)) {
                 // This is a lambda function, there is no error name (errors must be added manually)
                 $error_name = FALSE;
                 array_unshift($params, $field);
                 array_unshift($params, $this);
                 $passed = call_user_func_array($rule, $params);
             } elseif (method_exists('mii\\valid\\Rules', $rule)) {
                 // Use a method in this object
                 $method = new \ReflectionMethod('mii\\valid\\Rules', $rule);
                 // Call static::$rule($this[$field], $param, ...) with Reflection
                 $passed = $method->invokeArgs(NULL, $params);
             } elseif (strpos($rule, '::') === FALSE) {
                 // Use a function call
                 $function = new \ReflectionFunction($rule);
                 // Call $function($this[$field], $param, ...) with Reflection
                 $passed = $function->invokeArgs($params);
             } else {
                 // Split the class and method of the rule
                 list($class, $method) = explode('::', $rule, 2);
                 // Use a static method call
                 $method = new \ReflectionMethod($class, $method);
                 // Call $Class::$method($this[$field], $param, ...) with Reflection
                 $passed = $method->invokeArgs(NULL, $params);
             }
             // Ignore return values from rules when the field is empty
             if (!in_array($rule, $this->_empty_rules) and !Rules::not_empty($value)) {
                 continue;
             }
             if ($passed === FALSE and $error_name !== FALSE) {
                 // Add the rule to the errors
                 $this->error($field, $error_name, $params);
                 // This field has an error, stop executing rules
                 break;
             } elseif (isset($this->_errors[$field])) {
                 // The callback added the error manually, stop checking rules
                 break;
             }
         }
     }
     if ($benchmark) {
         // Stop benchmarking
         \mii\util\Profiler::stop($benchmark);
     }
     return empty($this->_errors);
 }
示例#5
0
    <?php 
foreach ($og as $name => $content) {
    ?>
        <meta property="og:<?php 
    echo $name;
    ?>
" content="<?php 
    echo str_replace('"', "'", $content);
    ?>
" />
    <?php 
}
?>

    <?php 
echo Mii::$app->blocks;
?>
</head>
<body>
<?php 
echo $layout;
?>

<?php 
if (config('profiling')) {
    \mii\util\Profiler::show();
}
?>

</body>
</html>
示例#6
0
文件: Blocks.php 项目: levmorozov/mii
 public function render()
 {
     if (config('debug')) {
         $benchmark = \mii\util\Profiler::start('Assets', __FUNCTION__);
     }
     foreach ($this->_blocks as $block_name => $block) {
         if ($block->__has_parent) {
             continue;
         }
         $this->process_block_assets($block_name, $block_name, $block->_depends);
     }
     foreach ($this->_files as $type => $blocks) {
         foreach ($blocks as $block_name => $block) {
             if (isset($block['files'])) {
                 $this->_build_block($block_name, $type, $block['files']);
             }
             if (isset($block['remote'])) {
                 if ($type === '.js') {
                     foreach ($block['remote'] as $position => $remote) {
                         $this->_js[$position][] = implode("\n", $remote);
                     }
                 } else {
                     foreach ($block['remote'] as $condition => $css_remote) {
                         if ($condition) {
                             $this->_css[] = '<!--[if ' . $condition . ']><link type="text/css" href="' . implode("\n", $css_remote) . '" rel="stylesheet" /><![endif]-->';
                         } else {
                             $this->_css[] = '<link type="text/css" href="' . implode("\n", $css_remote) . '" rel="stylesheet" />';
                         }
                     }
                 }
             }
             if (isset($block['inline'])) {
                 if ($type === '.js') {
                     foreach ($block['inline'] as $position => $inline) {
                         $this->_js[$position][] = '<script type="text/javascript">' . implode("\n", $inline) . '</script>';
                     }
                 } else {
                     $content = implode("\n", $block['inline']);
                     $this->_css[] = '<link type="text/css" href="' . $content . '" rel="stylesheet" />';
                 }
             }
         }
     }
     $this->_rendered = true;
     if (config('debug')) {
         \mii\util\Profiler::stop($benchmark);
     }
 }
示例#7
0
文件: Block.php 项目: levmorozov/mii
 /**
  * Renders the view object to a string. Global and local data are merged
  * and extracted to create local variables within the view file.
  *
  *     $output = $view->render();
  *
  * [!!] Global variables with the same key name as local variables will be
  * overwritten by the local variable.
  *
  * @param   string $file view filename
  * @return  string
  * @uses    Block::capture
  */
 public function render($force = false)
 {
     if (!$this->_loaded and !$force) {
         return '';
     }
     if (empty($this->_file)) {
         throw new Exception('Block :block does not have a php file', [':block' => $this->__name]);
     }
     $benchmark = false;
     if (config('debug')) {
         $benchmark = \mii\util\Profiler::start('Block:render', \mii\util\Debug::path($this->_file));
     }
     // Combine local and global data and capture the output
     $c = $this->capture($this->_file);
     if ($benchmark) {
         \mii\util\Profiler::stop($benchmark);
     }
     return $c;
 }