Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core_cache;
18
 
19
use core\exception\coding_exception;
20
use ArrayObject;
21
 
22
/**
23
 * An array of cacheable objects.
24
 *
25
 * This class allows a developer to create an array of cacheable objects and store that.
26
 * The cache API doesn't check items within an array to see whether they are cacheable. Such a check would be very costly to both
27
 * arrays using cacheable object and those that don't.
28
 * Instead the developer must explicitly use a cacheable_object_array instance.
29
 *
30
 * The following is one example of how this class can be used.
31
 * <code>
32
 * $data = array();
33
 * $data[] = new cacheable_object('one');
34
 * $data[] = new cacheable_object('two');
35
 * $data[] = new cacheable_object('three');
36
 * $cache->set(new cacheable_object_array($data));
37
 * </code>
38
 * Another example would be
39
 * <code>
40
 * $data = new cacheable_object_array();
41
 * $data[] = new cacheable_object('one');
42
 * $data[] = new cacheable_object('two');
43
 * $data[] = new cacheable_object('three');
44
 * $cache->set($data);
45
 * </code>
46
 *
47
 * @copyright  2012 Sam Hemelryk
48
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49
 * @package core_cache
50
 */
51
class cacheable_object_array extends ArrayObject implements cacheable_object_interface {
52
    /**
53
     * Constructs a new array object instance.
54
     * @param array $items
55
     */
56
    final public function __construct(array $items = []) {
57
        parent::__construct($items, ArrayObject::STD_PROP_LIST);
58
    }
59
 
60
    /**
61
     * Returns the data to cache for this object.
62
     *
63
     * @return cached_object[] An array of cached_object instances.
64
     * @throws coding_exception
65
     */
66
    final public function prepare_to_cache() {
67
        $result = [];
68
        foreach ($this as $key => $value) {
69
            if ($value instanceof cacheable_object_interface) {
70
                $value = new cached_object($value);
71
            } else {
72
                throw new coding_exception('Only cacheable_object instances can be added to a cacheable_array');
73
            }
74
            $result[$key] = $value;
75
        }
76
        return $result;
77
    }
78
 
79
    /**
80
     * Returns the cacheable_object_array that was originally sent to the cache.
81
     *
82
     * @param array $data
83
     * @return self
84
     * @throws coding_exception
85
     */
86
    final public static function wake_from_cache($data) {
87
        if (!is_array($data)) {
88
            throw new coding_exception('Invalid data type when reviving cacheable_array data');
89
        }
90
        $result = [];
91
        foreach ($data as $key => $value) {
92
            $result[$key] = $value->restore_object();
93
        }
94
        $class = __CLASS__;
95
        return new $class($result);
96
    }
97
}
98
 
99
// Alias this class to the old name.
100
// This file will be autoloaded by the legacyclasses autoload system.
101
// In future all uses of this class will be corrected and the legacy references will be removed.
102
class_alias(cacheable_object_array::class, \cacheable_object_array::class);