/**
  * Sets a timer point before rendering a file.
  *
  * @param string $viewFile The view being rendered
  */
 public function beforeRenderFile($viewFile)
 {
     if ($this->_renderComplete) {
         return;
     }
     DebugTimer::start('render_' . basename($viewFile), __d('debug_kit', 'Rendering %s', Debugger::trimPath($viewFile)));
 }
 /**
  * Wrapper around _isAuthorized to allow for timing
  *
  * @param array $user an array of user data. Can also be null
  * @param string $action The action to check access for
  * @return bool whether or not the user is authorized for access
  */
 public function isAuthorized($user, $action)
 {
     $timerExists = Configure::read('debug') && class_exists('DebugTimer');
     if ($timerExists) {
         DebugTimer::start(get_called_class() . '->isAuthorized()');
     }
     $return = $this->performCheck($user, $action);
     if ($timerExists) {
         DebugTimer::stop(get_called_class() . '->isAuthorized()');
     }
     return $return;
 }
Exemplo n.º 3
0
 /**
  * Renders view for given action and layout.
  * Adds timers, for all subsequent rendering, and injects the debugKit toolbar.
  *
  * @param string $action Name of action to render for
  * @param string $layout Layout to use
  * @return string Rendered Element
  */
 public function render($action = null, $layout = null)
 {
     DebugTimer::start('viewRender', __d('debug_kit', 'Rendering View'));
     $out = parent::render($action, $layout);
     DebugTimer::stop('viewRender');
     DebugTimer::stop('controllerRender');
     DebugMemory::record(__d('debug_kit', 'View render complete'));
     if (empty($this->request->params['requested']) && $this->Helpers && $this->Helpers->attached('Toolbar')) {
         $backend = $this->Helpers->Toolbar->getName();
         $this->Helpers->Toolbar->{$backend}->send();
     }
     if (empty($this->output)) {
         return $out;
     }
     return $this->output;
 }
Exemplo n.º 4
0
 /**
  * test that vars are gathered and state is saved on beforeRedirect
  *
  * @return void
  */
 public function testBeforeRedirect()
 {
     $this->_loadController(array('panels' => array('session', 'history')));
     $configName = 'debug_kit';
     $this->Controller->Toolbar->cacheKey = 'toolbar_history';
     Cache::delete('toolbar_history', $configName);
     DebugTimer::start('controllerAction', 'testing beforeRedirect');
     $MockPanel = $this->getMock('DebugPanel');
     $MockPanel->expects($this->once())->method('beforeRender');
     $this->Controller->Toolbar->panels['session'] = $MockPanel;
     $this->Controller->Toolbar->beforeRedirect($this->Controller, '/another/url');
     $result = Cache::read('toolbar_history', $configName);
     $this->assertTrue(isset($result[0]['session']));
     $this->assertFalse(isset($result[0]['history']));
     $timers = DebugTimer::getAll();
     $this->assertTrue(isset($timers['controllerAction']));
 }
 /**
  * beforeRender callback
  *
  * Calls beforeRender on all the panels and set the aggregate to the controller.
  *
  * @param Controller $controller
  * @return void
  */
 public function beforeRender(Controller $controller)
 {
     if (!class_exists('DebugTimer')) {
         return null;
     }
     DebugTimer::stop('controllerAction');
     DebugTimer::start('processToolbar', __d('debug_kit', 'Processing toolbar data'));
     $vars = $this->_gatherVars($controller);
     $this->_saveState($controller, $vars);
     $this->javascript = array_unique(array_merge($this->javascript, $vars['javascript']));
     $this->css = array_unique(array_merge($this->css, $vars['css']));
     unset($vars['javascript'], $vars['css']);
     $controller->set(array('debugToolbarPanels' => $vars, 'debugToolbarJavascript' => $this->javascript, 'debugToolbarCss' => $this->css));
     $isHtml = !isset($controller->request->params['ext']) || $controller->request->params['ext'] === 'html';
     if (!$controller->request->is('ajax') && $isHtml) {
         $format = 'Html';
     } else {
         $format = 'FirePhp';
     }
     $controller->helpers[] = 'DebugKit.DebugTimer';
     $controller->helpers['DebugKit.Toolbar'] = array('output' => sprintf('DebugKit.%sToolbar', $format), 'cacheKey' => $this->cacheKey, 'cacheConfig' => 'debug_kit', 'forceEnable' => $this->settings['forceEnable']);
     DebugTimer::stop('processToolbar');
     DebugMemory::record(__d('debug_kit', 'Controller render start'));
 }
Exemplo n.º 6
0
 /**
  * Start an benchmarking timer.
  *
  * @param string $name The name of the timer to start.
  * @param string $message A message for your timer
  * @return bool true
  * @deprecated use DebugTimer::start()
  */
 public static function startTimer($name = null, $message = null)
 {
     return DebugTimer::start($name, $message);
 }
Exemplo n.º 7
0
 protected function _findView($state, $query, $results = array())
 {
     if ($state == 'before') {
         if (empty($query['package_id'])) {
             throw new InvalidArgumentException(__('Invalid package'));
         }
         $query['conditions'] = array("{$this->alias}.{$this->primaryKey}" => $query['package_id']);
         $query['contain'] = array('Maintainer' => array('name', 'username'));
         $query['limit'] = 1;
         if (!empty($query['fields'])) {
             $query['fields'] = array_merge($this->getDataSource()->fields($this, null, $query['fields']), $this->Category->getDataSource()->fields($this->Category));
         } else {
             $query['fields'] = array_merge($this->getDataSource()->fields($this), $this->Category->getDataSource()->fields($this->Category));
         }
         $this->unbindModel(array('belongsTo' => array('Categories.Category')));
         $query['joins'] = array(array('alias' => 'Category', 'table' => 'categories', 'type' => 'LEFT', 'conditions' => array('`Category`.`id` = `Package`.`category_id`')));
         // Join additional records if necessary
         if ($query['user_id']) {
             if (!empty($query['fields'])) {
                 $query['fields'] = array_merge($this->getDataSource()->fields($this, null, $query['fields']), $this->Rating->getDataSource()->fields($this->Rating), $this->Favorite->getDataSource()->fields($this->Favorite));
             } else {
                 $query['fields'] = array_merge($this->getDataSource()->fields($this), $this->Rating->getDataSource()->fields($this->Rating), $this->Favorite->getDataSource()->fields($this->Favorite));
             }
             $this->unbindModel(array('hasMany' => array('Ratings.Rating', 'Favorites.Favorite')));
             $query['joins'][] = array('alias' => 'Favorite', 'table' => 'favorites', 'type' => 'LEFT', 'conditions' => array('`Favorite`.`foreign_key` = `Package`.`id`', 'Favorite.model' => 'Package', 'Favorite.type' => 'bookmark', 'Favorite.user_id' => $query['user_id']));
             $query['joins'][] = array('alias' => 'Rating', 'table' => 'ratings', 'type' => 'LEFT', 'conditions' => array('`Rating`.`foreign_key` = `Package`.`id`', 'Rating.model' => 'Package', 'Rating.user_id' => $query['user_id']));
         }
         return $query;
     }
     if (empty($results[0])) {
         throw new NotFoundException(__('Invalid package'));
     }
     if (empty($results[0]['Maintainer'])) {
         throw new NotFoundException(__('Invalid maintainer'));
     }
     if (empty($results[0]['Favorite']['id'])) {
         $results[0]['Favorite'] = false;
     }
     if (empty($results[0]['Rating']['id'])) {
         $results[0]['Rating'] = false;
     }
     if ($this->shouldForceUpdate($results[0][$this->alias]['modified'])) {
         try {
             $this->enqueue('UpdatePackageJob', array($results[0][$this->alias][$this->primaryKey]));
         } catch (Exception $e) {
             CakeLog::warning('Package::find(\'view\')' . $e->getMessage());
         }
     }
     DebugTimer::start('app.Package::rss', __d('app', 'Package::rss()'));
     list($results[0]['Rss'], $results[0]['Cache']) = $this->rss($results[0]);
     DebugTimer::stop('app.Package::rss');
     return $results[0];
 }
 /**
  * Start timer for Crud.Delete
  *
  * @param CakeEvent $event
  * @return void
  */
 public function beforeDelete(CakeEvent $event)
 {
     DebugTimer::start('Event: Crud.Delete');
 }
Exemplo n.º 9
0
 /**
  * beforeRender callback
  *
  * Calls beforeRender on all the panels and set the aggregate to the controller.
  *
  * @return void
  **/
 public function beforeRender($controller)
 {
     if (!class_exists('DebugTimer')) {
         return null;
     }
     DebugTimer::stop('controllerAction');
     $vars = $this->_gatherVars($controller);
     $this->_saveState($controller, $vars);
     $controller->set(array('debugToolbarPanels' => $vars, 'debugToolbarJavascript' => $this->javascript));
     DebugTimer::start('controllerRender', __d('debug_kit', 'Render Controller Action'));
     DebugMemory::record(__d('debug_kit', 'Controller render start'));
 }
 public function afterFind(Model $Model, $results, $primary = false)
 {
     if (CakePlugin::loaded('DebugKit')) {
         DebugTimer::start('data-copy-after-find', __d('data_copy', 'DataCopy: Running after find on %1$s with origin %2$s', $Model->alias, $Model->OriginModel->alias));
     }
     $query = $Model->query;
     if (CakePlugin::loaded('DebugKit')) {
         DebugTimer::start('data-copy-list-old', __d('data_copy', 'DataCopy: Checking for old data from %1$s', $Model->alias));
     }
     $oldestItems = array_map('strtotime', array_values($Model->find('list', array('fields' => array($Model->alias . '.' . $Model->primaryKey, $Model->alias . '.data_copy_modified'), 'order' => array($Model->alias . '.data_copy_modified' => 'ASC'), 'limit' => 10, 'copy' => true, 'callbacks' => false))));
     if (CakePlugin::loaded('DebugKit')) {
         DebugTimer::stop('data-copy-list-old');
     }
     $oldestData = null;
     foreach ($oldestItems as $oldestItemInArray) {
         if ($oldestItemInArray < $oldestData) {
             $oldestData = $oldestItemInArray;
         }
     }
     if ($query['copy'] === false) {
         DataCopyDebug::logExpiredLookup($Model, $Model->OriginModel, $query, __d('data_copy', 'Force update'));
         $results = false;
     }
     if ($query['copy'] === null && $oldestData !== null && time() - $oldestData > $this->settings[$Model->alias]['expire']) {
         DataCopyDebug::logExpiredLookup($Model, $Model->OriginModel, $query, __d('data_copy', 'Regular expire. Oldest data: %1$s', date('r', $oldestData)));
         $results = false;
     }
     if (is_array($results)) {
         DataCopyDebug::logLookup($Model, $Model->OriginModel, $query);
         if (CakePlugin::loaded('DebugKit')) {
             DebugTimer::stop('data-copy-after-find');
         }
         return $results;
     }
     $Model->OriginModel->recursive = $Model->recursive;
     $queryData = $query;
     unset($queryData['conditions']);
     if (is_array($query['conditions'])) {
         foreach ($query['conditions'] as $field => $condition) {
             $queryData['conditions'][$this->convertField($Model, $field)] = $condition;
         }
     }
     if (is_array($query['order'][0])) {
         foreach ($query['order'][0] as $field => $direction) {
             unset($queryData['order'][0][$field]);
             $queryData['order'][$this->convertField($Model, $field)] = $direction;
         }
     }
     unset($queryData['order'][0]);
     if (is_array($queryData['fields'])) {
         foreach ($queryData['fields'] as $index => &$field) {
             $startQuote = isset($Model->getDataSource()->startQuote) ? $Model->getDataSource()->startQuote : null;
             $endQuote = isset($Model->getDataSource()->endQuote) ? $Model->getDataSource()->endQuote : null;
             $field = str_replace(array($startQuote, $endQuote), '', $field);
             $field = $this->convertField($Model, $field);
             if ($field === $Model->OriginModel->alias . '.data_copy_modified') {
                 unset($queryData['fields'][$index]);
             }
         }
     }
     $originResults = $Model->OriginModel->find('all', $queryData);
     foreach ($originResults as $index => &$result) {
         $result[$Model->alias] = $result[$Model->OriginModel->alias];
         unset($result[$Model->OriginModel->alias]);
         foreach ($result as $association => $data) {
             foreach ($data as $field => &$value) {
                 $value = $this->convertValue($Model, $field, $value);
                 $originResults[$index][$association][$field] = $value;
             }
             $originResults[$index][$association]['data_copy_modified'] = date($Model->getDataSource()->columns['datetime']['format']);
         }
     }
     foreach ($Model->getAssociated('belongsTo') as $association) {
         if (!isset($Model->belongsTo[$association]['origin'])) {
             continue;
         }
         $origin = $Model->belongsTo[$association]['origin'];
         foreach ($originResults as $index => &$result) {
             if (!isset($result[$origin])) {
                 continue;
             }
             $data = $result[$origin];
             unset($originResults[$index][$origin]);
             $originResults[$index][$association] = $data;
         }
     }
     if (CakePlugin::loaded('DebugKit')) {
         DebugTimer::start('data-copy-save-all', __d('data_copy', 'DataCopy: Saving data in %1$s from origin %2$s', $Model->alias, $Model->OriginModel->alias));
     }
     $Model->saveAll($originResults, array('deep' => true));
     if (CakePlugin::loaded('DebugKit')) {
         DebugTimer::stop('data-copy-save-all');
     }
     if (!is_array($query['order'][0])) {
         $query['order'] = array();
     } else {
         $query['order'] = $query['order'][0];
     }
     $query['copy'] = true;
     $results = $Model->find('all', $query);
     if (CakePlugin::loaded('DebugKit')) {
         DebugTimer::stop('data-copy-after-find');
     }
     return $results;
 }
Exemplo n.º 11
0
 /**
  * test getting all the set timers.
  *
  * @return void
  */
 public function testGetTimers()
 {
     DebugTimer::start('test1', 'this is my first test');
     DebugTimer::stop('test1');
     usleep(50);
     DebugTimer::start('test2');
     DebugTimer::stop('test2');
     $timers = DebugTimer::getAll();
     $this->assertEquals(3, count($timers));
     $this->assertTrue(is_float($timers['test1']['time']));
     $this->assertTrue(isset($timers['test1']['message']));
     $this->assertTrue(isset($timers['test2']['message']));
 }
Exemplo n.º 12
0
 /**
  * Starts a benchmarking timer only if DebugKit is enabled.
  *
  * @param string $name The name of the timer to start.
  * @param string $message A message for your timer
  * @return bool Always true
  */
 public static function startTimer($name, $message = null)
 {
     if (!Reveal::is('DebugKit.running')) {
         return;
     }
     App::uses('DebugTimer', 'DebugKit.Lib');
     DebugTimer::start($name, $message);
 }
Exemplo n.º 13
0
 /**
  * beforeSave, starts a time before a save is initiated.
  *
  * @param Model $Model The model.
  * @param array $options The options.
  * @return bool Always true.
  */
 public function beforeSave(Model $Model, $options = array())
 {
     DebugTimer::start($Model->alias . '_save', $Model->alias . '->save()');
     return true;
 }
Exemplo n.º 14
0
 /**
  * Start the DebugKitTimer.
  *
  * @param string $method The method that is being called.
  * @return boolean Whether the DebugKitTimer is started.
  */
 public function startTimer($method)
 {
     if (Configure::read('debug') == 0) {
         return false;
     }
     if ($this->_useDebugKit) {
         return DebugTimer::start("soapQuery_{$method}", "Soap::{$method}");
     }
     return true;
 }