added example for developpers module

This commit is contained in:
Bachir Soussi Chiadmi
2018-01-06 11:28:03 +01:00
parent 5017966908
commit 5ec2f4311c
396 changed files with 26299 additions and 0 deletions
@@ -0,0 +1,14 @@
name: Cache Example
description: Demonstrates how to use Cache API.
package: Example modules
# core: 8.x
type: module
dependencies:
- drupal:node
- examples:examples
# Information added by Drupal.org packaging script on 2017-12-17
version: '8.x-1.x-dev'
core: '8.x'
project: 'examples'
datestamp: 1513537386
@@ -0,0 +1,4 @@
cache_example.description:
title: 'Cache Example'
description: 'Example of Drupal Cache API'
route_name: cache_example.description
@@ -0,0 +1,36 @@
<?php
/**
* @file
* Outlines how a module can use the Cache API.
*
* @todo: Demonstrate allowing invalid cache items.
* @todo: Demonstrate deleteing cache entries.
* @todo: Demonstrate deleteing cache tags.
* @todo: Demonstrate deleteing all cache items.
* @todo: Demonstrate invalidating cache entries.
* @todo: Demonstrate invalidating cache tags.
* @todo: Demonstrate invalidating all cache items.
*/
/**
* @defgroup cache_example Example: Cache API
* @ingroup examples
* @{
* Outlines how a module can use the Cache API.
*
* Cache API allows us to cache data that is heavy to calculate. As this can
* significantly speed up the Drupal site, it is recommended to use cache
* mechanism when it is appropriate.
*
* Cache in Drupal is very easy to use. This example will search entire Drupal
* folder and display all files. Since this operation includes filesystem it can
* take a while. This list will not change much on production
* websites, so we decide to cache it.
*
* @see \Drupal\Core\Cache\CacheBackendInterface
*/
/**
* @} End of "defgroup cache_example".
*/
@@ -0,0 +1,6 @@
cache_example.description:
path: 'examples/cache-example'
defaults:
_form: '\Drupal\cache_example\Form\CacheExampleForm'
requirements:
_permission: 'access content'
@@ -0,0 +1,306 @@
<?php
namespace Drupal\cache_example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Form with examples on how to use cache.
*/
class CacheExampleForm extends FormBase {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The cache.default cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheBackend;
/**
* Dependency injection through the constructor.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack service.
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* The string translation service.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current active user service.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache object associated with the default bin.
*/
public function __construct(
RequestStack $request_stack,
TranslationInterface $translation,
AccountProxyInterface $current_user,
CacheBackendInterface $cache_backend
) {
$this->setRequestStack($request_stack);
$this->setStringTranslation($translation);
$this->currentUser = $current_user;
$this->cacheBackend = $cache_backend;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Forms that require a Drupal service or a custom service should access
// the service using dependency injection.
// @link https://www.drupal.org/node/2203931.
// Those services are passed in the $container through the static create
// method.
return new static(
$container->get('request_stack'),
$container->get('string_translation'),
$container->get('current_user'),
$container->get('cache.default')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'cron_cache';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Log execution time.
$start_time = microtime(TRUE);
// Try to load the files count from cache. This function will accept two
// arguments:
// - cache object name (cid)
// - cache bin, the (optional) cache bin (most often a database table) where
// the object is to be saved.
//
// cache_get() returns the cached object or FALSE if object does not exist.
if ($cache = $this->cacheBackend->get('cache_example_files_count')) {
/*
* Get cached data. Complex data types will be unserialized automatically.
*/
$files_count = $cache->data;
}
else {
// If there was no cached data available we have to search filesystem.
// Recursively get all .PHP files from Drupal's core folder.
$files_count = count(file_scan_directory('core', '/.php/'));
// Since we have recalculated, we now need to store the new data into
// cache. Complex data types will be automatically serialized before
// being saved into cache.
// Here we use the default setting and create an unexpiring cache item.
// See below for an example that creates an expiring cache item.
$this->cacheBackend->set('cache_example_files_count', $files_count, CacheBackendInterface::CACHE_PERMANENT);
}
$end_time = microtime(TRUE);
$duration = $end_time - $start_time;
// Format intro message.
$intro_message = '<p>' . $this->t("This example will search Drupal's core folder and display a count of the PHP files in it.") . ' ';
$intro_message .= $this->t('This can take a while, since there are a lot of files to be searched.') . ' ';
$intro_message .= $this->t('We will search filesystem just once and save output to the cache. We will use cached data for later requests.') . '</p>';
$intro_message .= '<p>'
. $this->t(
'<a href="@url">Reload this page</a> to see cache in action.',
['@url' => $this->getRequest()->getRequestUri()]
)
. ' ';
$intro_message .= $this->t('You can use the button below to remove cached data.') . '</p>';
$form['file_search'] = [
'#type' => 'fieldset',
'#title' => $this->t('File search caching'),
];
$form['file_search']['introduction'] = [
'#markup' => $intro_message,
];
$color = empty($cache) ? 'red' : 'green';
$retrieval = empty($cache) ? $this->t('calculated by traversing the filesystem') : $this->t('retrieved from cache');
$form['file_search']['statistics'] = [
'#type' => 'item',
'#markup' => $this->t('%count files exist in this Drupal installation; @retrieval in @time ms. <br/>(Source: <span style="color:@color;">@source</span>)', [
'%count' => $files_count,
'@retrieval' => $retrieval,
'@time' => number_format($duration * 1000, 2),
'@color' => $color,
'@source' => empty($cache) ? $this->t('actual file search') : $this->t('cached'),
]
),
];
$form['file_search']['remove_file_count'] = [
'#type' => 'submit',
'#submit' => [[$this, 'expireFiles']],
'#value' => $this->t('Explicitly remove cached file count'),
];
$form['expiration_demo'] = [
'#type' => 'fieldset',
'#title' => $this->t('Cache expiration settings'),
];
$form['expiration_demo']['explanation'] = [
'#markup' => $this->t('A cache item can be set as CACHE_PERMANENT, meaning that it will only be removed when explicitly cleared, or it can have an expiration time (a Unix timestamp).'),
];
$item = $this->cacheBackend->get('cache_example_expiring_item', TRUE);
if ($item == FALSE) {
$item_status = $this->t('Cache item does not exist');
}
else {
$item_status = $item->valid ? $this->t('Cache item exists and is set to expire at %time', ['%time' => $item->data]) :
$this->t('Cache_item is invalid');
}
$form['expiration_demo']['current_status'] = [
'#type' => 'item',
'#title' => $this->t('Current status of cache item "cache_example_expiring_item"'),
'#markup' => $item_status,
];
$form['expiration_demo']['expiration'] = [
'#type' => 'select',
'#title' => $this->t('Time before cache expiration'),
'#options' => [
'never_remove' => $this->t('CACHE_PERMANENT'),
-10 => $this->t('Immediate expiration'),
10 => $this->t('10 seconds from form submission'),
60 => $this->t('1 minute from form submission'),
300 => $this->t('5 minutes from form submission'),
],
'#default_value' => -10,
'#description' => $this->t('Any cache item can be set to only expire when explicitly cleared, or to expire at a given time.'),
];
$form['expiration_demo']['create_cache_item'] = [
'#type' => 'submit',
'#value' => $this->t('Create a cache item with this expiration'),
'#submit' => [[$this, 'createExpiringItem']],
];
$form['cache_clearing'] = [
'#type' => 'fieldset',
'#title' => $this->t('Expire and remove options'),
'#description' => $this->t("We have APIs to expire cached items and also to just remove them. Unfortunately, they're all the same API, cache_clear_all"),
];
$form['cache_clearing']['cache_clear_type'] = [
'#type' => 'radios',
'#title' => $this->t('Type of cache clearing to do'),
'#options' => [
'expire' => $this->t('Remove items from the "cache" bin that have expired'),
'remove_all' => $this->t('Remove all items from the "cache" bin regardless of expiration'),
'remove_tag' => $this->t('Remove all items in the "cache" bin with the tag "cache_example" set to 1'),
],
'#default_value' => 'expire',
];
// Submit button to clear cached data.
$form['cache_clearing']['clear_expired'] = [
'#type' => 'submit',
'#value' => $this->t('Clear or expire cache'),
'#submit' => [[$this, 'cacheClearing']],
'#access' => $this->currentUser->hasPermission('administer site configuration'),
];
return $form;
}
/**
* Submit handler that explicitly clears cache_example_files_count from cache.
*/
public function expireFiles($form, &$form_state) {
// Clear cached data. This function will delete cached object from cache
// bin.
//
// The first argument is cache id to be deleted. Since we've provided it
// explicitly, it will be removed whether or not it has an associated
// expiration time. The second argument (required here) is the cache bin.
// Using cache_clear_all() explicitly in this way
// forces removal of the cached item.
$this->cacheBackend->delete('cache_example_files_count');
// Display message to the user.
drupal_set_message($this->t('Cached data key "cache_example_files_count" was cleared.'), 'status');
}
/**
* Submit handler to create a new cache item with specified expiration.
*/
public function createExpiringItem($form, &$form_state) {
$tags = [
'cache_example:1',
];
$interval = $form_state->getValue('expiration');
if ($interval == 'never_remove') {
$expiration = CacheBackendInterface::CACHE_PERMANENT;
$expiration_friendly = $this->t('Never expires');
}
else {
$expiration = time() + $interval;
$expiration_friendly = format_date($expiration);
}
// Set the expiration to the actual Unix timestamp of the end of the
// required interval. Also add a tag to it to be able to clear caches more
// precise.
$this->cacheBackend->set('cache_example_expiring_item', $expiration_friendly, $expiration, $tags);
drupal_set_message($this->t('cache_example_expiring_item was set to expire at %time', ['%time' => $expiration_friendly]));
}
/**
* Submit handler to demonstrate the various uses of cache_clear_all().
*/
public function cacheClearing($form, &$form_state) {
switch ($form_state->getValue('cache_clear_type')) {
case 'expire':
// Here we'll remove all cache keys in the 'cache' bin that have
// expired.
$this->cacheBackend->garbageCollection();
drupal_set_message($this->t('\Drupal::cache()->garbageCollection() was called, removing any expired cache items.'));
break;
case 'remove_all':
// This removes all keys in a bin using a super-wildcard. This
// has nothing to do with expiration. It's just brute-force removal.
$this->cacheBackend->deleteAll();
drupal_set_message($this->t('ALL entries in the "cache" bin were removed with \Drupal::cache()->deleteAll().'));
break;
case 'remove_tag':
// This removes cache entries with the tag "cache_example" set to 1 in
// the "cache".
$tags = [
'cache_example:1',
];
Cache::invalidateTags($tags);
drupal_set_message($this->t('Cache entries with the tag "cache_example" set to 1 in the "cache" bin were invalidated with \Drupal\Core\Cache\Cache::invalidateTags($tags).'));
break;
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
@@ -0,0 +1,110 @@
<?php
namespace Drupal\Tests\cache_example\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Tests for the cache_example module.
*
* @ingroup cache_example
*
* @group cache_example
* @group examples
*/
class CacheExampleTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['cache_example'];
/**
* The installation profile to use with this test.
*
* @var string
*/
protected $profile = 'minimal';
/**
* Test menu links and routes.
*
* Test the following:
* - A link to the cache_example in the Tools menu.
* - That you can successfully access the cache_example form.
*/
public function testCacheExampleMenu() {
$assert = $this->assertSession();
// Test for a link to the cache_example in the Tools menu.
$this->drupalGet('');
$assert->statusCodeEquals(200);
$assert->linkByHrefExists('examples/cache-example');
// Verify if the can successfully access the cache_example form.
$this->drupalGet('examples/cache-example');
$assert->statusCodeEquals(200);
}
/**
* Test that our caches function.
*
* Does the following:
* - Load cache example page and test if displaying uncached version.
* - Reload once again and test if displaying cached version.
* - Find reload link and click on it.
* - Clear cache at the end and test if displaying uncached version again.
*/
public function testCacheExampleBasic() {
$assert = $this->assertSession();
// We need administrative privileges to clear the cache.
$admin_user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($admin_user);
// Get initial page cache example page, first time accessed,
// and assert uncached output.
$this->drupalGet('examples/cache-example');
$assert->pageTextContains('Source: actual file search');
// Reload the page; the number should be cached.
$this->drupalGet('examples/cache-example');
$assert->pageTextContains('Source: cached');
// Now push the button to remove the count.
$this->drupalPostForm('examples/cache-example', [], 'Explicitly remove cached file count');
$assert->pageTextContains('Source: actual file search');
// Create a cached item. First make sure it doesn't already exist.
$assert->pageTextContains('Cache item does not exist');
$this->drupalPostForm('examples/cache-example', ['expiration' => -10], 'Create a cache item with this expiration');
// We should now have an already-expired item. Automatically invalid.
$assert->pageTextContains('Cache_item is invalid');
// Now do the expiration operation.
$this->drupalPostForm('examples/cache-example', ['cache_clear_type' => 'expire'], 'Clear or expire cache');
// And verify that it was removed.
$assert->pageTextContains('Cache item does not exist');
// Create a cached item. This time we'll make it not expire.
$this->drupalPostForm('examples/cache-example', ['expiration' => 'never_remove'], 'Create a cache item with this expiration');
// We should now have an never-remove item.
$assert->pageTextContains('Cache item exists and is set to expire at Never expires');
// Now do the expiration operation.
$this->drupalPostForm('examples/cache-example', ['cache_clear_type' => 'expire'], 'Clear or expire cache');
// And verify that it was not removed.
$assert->pageTextContains('Cache item exists and is set to expire at Never expires');
// Now do tag invalidation.
$this->drupalPostForm('examples/cache-example', ['cache_clear_type' => 'remove_tag'], 'Clear or expire cache');
// And verify that it was invalidated.
$assert->pageTextContains('Cache_item is invalid');
// Do the hard delete.
$this->drupalPostForm('examples/cache-example', ['cache_clear_type' => 'remove_all'], 'Clear or expire cache');
// And verify that it was removed.
$assert->pageTextContains('Cache item does not exist');
}
}