Пример #1
0
 /**
  * Gets or sets the cache config to be used
  *
  * @param string $config
  * @return string
  */
 public static function config($config = null)
 {
     if (empty($config)) {
         return self::$_config;
     }
     self::$_config = $config;
 }
Пример #2
0
 public function testInvalidate()
 {
     SlugCache::set('User', array(1 => 2));
     SlugCache::set('Route', array(3 => 4));
     SlugCache::invalidate('User');
     $this->assertFalse(Cache::read('User', 'SluggerTest'));
     $expected = array(3 => 4);
     $result = Cache::read('Route', 'SluggerTest');
     $this->assertEquals($expected, $result);
     $expected = array('SluggerTest' => array('Route' => array(3 => 4)));
     $result = SlugCache::$_varCache;
     $this->assertEquals($expected, $result);
 }
Пример #3
0
 public function testGetSlugs()
 {
     $Sluggable = new SluggableRoute(null, null, null);
     $Sluggable->options['models'] = array('RouteTest' => array());
     $Sluggable->config();
     $results = $Sluggable->getSlugs($this->RouteTest->alias);
     $expected = array(1 => 'a-page-title', 2 => 'another-title', 3 => 'i-love-cakephp');
     $this->assertEquals($expected, $results);
     SlugCache::invalidate('RouteTest');
     $Sluggable->options['models'] = array('RouteTest' => array('slugField' => 'name'));
     $Sluggable->config();
     $results = $Sluggable->getSlugs($this->RouteTest->alias);
     $expected = array(1 => 'page-title', 2 => 'routing-is-fun', 3 => 'cake-rocks');
     $this->assertEquals($expected, $results);
 }
Пример #4
0
 /**
  * Gets slugs, checks cache first
  *
  * @param string $modelName The name of the model
  * @return array Array of slugs
  */
 public function getSlugs($modelName)
 {
     $slugs = SlugCache::get($modelName);
     if (empty($slugs)) {
         $Model = ClassRegistry::init($modelName, true);
         if ($Model === false) {
             return false;
         }
         $field = $Model->displayField;
         if (!empty($this->models[$modelName]['slugField'])) {
             $field = $this->models[$modelName]['slugField'];
         }
         $slugs = $Model->find('all', array('fields' => array($Model->name . '.' . $Model->primaryKey, $Model->name . '.' . $field), 'recursive' => -1));
         $counts = $Model->find('all', array('fields' => array('LOWER(TRIM(' . $Model->name . '.' . $field . ')) AS ' . $field, 'COUNT(*) AS count'), 'group' => array($field)));
         $counts = Set::combine($counts, '{n}.0.' . $field, '{n}.0.count');
         $listedSlugs = array();
         foreach ($slugs as $pk => $fields) {
             $values = array('_field' => $fields[$Model->name][$field], '_count' => $counts[strtolower(trim($fields[$Model->name][$field]))], '_pk' => $fields[$Model->name][$Model->primaryKey]);
             $listedSlugs[$fields[$Model->name][$Model->primaryKey]] = $this->slug($values);
         }
         SlugCache::set($modelName, $listedSlugs);
     }
     return SlugCache::get($modelName);
 }