| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 shiftwidth=4: */
|
|
|
3 |
/**
|
|
|
4 |
* File containing the example simple file based Schema Caching class.
|
|
|
5 |
*
|
|
|
6 |
* PHP version 5
|
|
|
7 |
*
|
|
|
8 |
* @category Net
|
|
|
9 |
* @package Net_LDAP2
|
|
|
10 |
* @author Benedikt Hallinger <beni@php.net>
|
|
|
11 |
* @copyright 2009 Benedikt Hallinger
|
|
|
12 |
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
|
|
|
13 |
* @version SVN: $Id: SimpleFileSchemaCache.php 286718 2009-08-03 07:30:49Z beni $
|
|
|
14 |
* @link http://pear.php.net/package/Net_LDAP2/
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* A simple file based schema cacher with cache aging.
|
|
|
19 |
*
|
|
|
20 |
* Once the cache is too old, the loadSchema() method will return false, so
|
|
|
21 |
* Net_LDAP2 will fetch a fresh object from the LDAP server that will
|
|
|
22 |
* overwrite the current (outdated) old cache.
|
|
|
23 |
*/
|
|
|
24 |
class Net_LDAP2_SimpleFileSchemaCache implements Net_LDAP2_SchemaCache
|
|
|
25 |
{
|
|
|
26 |
/**
|
|
|
27 |
* Internal config of this cache
|
|
|
28 |
*
|
|
|
29 |
* @see Net_LDAP2_SimpleFileSchemaCache()
|
|
|
30 |
* @var array
|
|
|
31 |
*/
|
|
|
32 |
protected $config = array(
|
|
|
33 |
'path' => '/tmp/Net_LDAP_Schema.cache',
|
|
|
34 |
'max_age' => 1200
|
|
|
35 |
);
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Initialize the simple cache
|
|
|
39 |
*
|
|
|
40 |
* Config is as following:
|
|
|
41 |
* path Complete path to the cache file.
|
|
|
42 |
* max_age Maximum age of cache in seconds, 0 means "endlessly".
|
|
|
43 |
*
|
|
|
44 |
* @param array $cfg Config array
|
|
|
45 |
*/
|
|
|
46 |
public function Net_LDAP2_SimpleFileSchemaCache($cfg)
|
|
|
47 |
{
|
|
|
48 |
foreach ($cfg as $key => $value) {
|
|
|
49 |
if (array_key_exists($key, $this->config)) {
|
|
|
50 |
if (gettype($this->config[$key]) != gettype($value)) {
|
|
|
51 |
$this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key does not match type ".gettype($this->config[$key])."!");
|
|
|
52 |
}
|
|
|
53 |
$this->config[$key] = $value;
|
|
|
54 |
} else {
|
|
|
55 |
$this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key is not defined!");
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Return the schema object from the cache
|
|
|
62 |
*
|
|
|
63 |
* If file is existent and cache has not expired yet,
|
|
|
64 |
* then the cache is deserialized and returned.
|
|
|
65 |
*
|
|
|
66 |
* @return Net_LDAP2_Schema|Net_LDAP2_Error|false
|
|
|
67 |
*/
|
|
|
68 |
public function loadSchema()
|
|
|
69 |
{
|
|
|
70 |
$return = false; // Net_LDAP2 will load schema from LDAP
|
|
|
71 |
if (file_exists($this->config['path'])) {
|
|
|
72 |
$cache_maxage = filemtime($this->config['path']) + $this->config['max_age'];
|
|
|
73 |
if (time() <= $cache_maxage || $this->config['max_age'] == 0) {
|
|
|
74 |
$return = unserialize(file_get_contents($this->config['path']));
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
return $return;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Store a schema object in the cache
|
|
|
82 |
*
|
|
|
83 |
* This method will be called, if Net_LDAP2 has fetched a fresh
|
|
|
84 |
* schema object from LDAP and wants to init or refresh the cache.
|
|
|
85 |
*
|
|
|
86 |
* To invalidate the cache and cause Net_LDAP2 to refresh the cache,
|
|
|
87 |
* you can call this method with null or false as value.
|
|
|
88 |
* The next call to $ldap->schema() will then refresh the caches object.
|
|
|
89 |
*
|
|
|
90 |
* @param mixed $schema The object that should be cached
|
|
|
91 |
* @return true|Net_LDAP2_Error|false
|
|
|
92 |
*/
|
|
|
93 |
public function storeSchema($schema) {
|
|
|
94 |
file_put_contents($this->config['path'], serialize($schema));
|
|
|
95 |
return true;
|
|
|
96 |
}
|
|
|
97 |
}
|