Cacheable Behavior

php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
 * Cacheable Model Behavior
 * 
 * Caches the file to the database
 *
 * @package app
 * @subpackage app.models.behaviors
 * @author Jose Diaz-Gonzalez
 * @version $Id$
 * @copyright Stoop Dev
 **/
class CacheableBehavior extends ModelBehavior {

/**
 * Initiate Cacheable Behavior
 *
 * @param object $model
 * @param array $config
 * @return void
 * @access public
 */
  function setup(&$model, $config = array()) {
  }

/**
 * After save callback
 *
 * @param object $model Model using this behavior
 * @param boolean $created True if this save created a new record
 * @access public
 * @return boolean True if the operation succeeded, false otherwise
 */
  function afterSave(&$model, $created) {
      $pluginName = get_parent_class($model);
      if ($pluginName == 'AppModel') {
          $pluginName = null;
      } else {
          $pluginName = substr($pluginName, 0 , -8);
          $pluginName = strtolower(Inflector::camelize($pluginName));
      }
      $requestPath = array(
          'plugin' => $pluginName,
          'controller' => Inflector::tableize($model->alias),
          'action' => 'view',
          'admin' => false,
          $model->id,
          Inflector::slug($model->data[$model->alias][$model->displayField])
      );

      $stops = array('published', 'approved');
      $stopped = false;
      $arr = array_keys($model->data[$model->alias]);
      foreach ($stops as $stop) {
          if (in_array($stop, $arr) and ($model->data[$model->alias][$stop] == 0)) {
              $stopped = true;
              break;
          }
      }

      if (!$stopped) {
          $cachePath = Router::url($requestPath, false);
          $surrenderOuput = $this->requestAction($cachePath, array('return', 'cacheableRequest' => true));
          $path = WWW_ROOT . "cache" . $cachePath . "/" . 'index.html';
          $file = new File($path, true);
          $file->write($surrenderOuput);
          $file->close();
      }

      return true;
  }

/**
 * Removes the respective file from cache
 *
 * @return void
 * @author Jose Diaz-Gonzalez
 **/
  function resetCache(&$model, $primaryKey = null, $displayField = null) {
      $path = '/' . Inflector::tableize($model->alias) . '/' . 'view' . '/' . $primaryKey . "/" . Inflector::slug($displayField);
      $path = WWW_ROOT . "cache" . $path . "/" . 'index.html';
      $file = new File($path, true);
      $file->delete();
      $file->close();
  }
}
?>
0 Responses. Add Yours!

Discussion

blog comments powered by Disqus