Skip to content
This repository was archived by the owner on Nov 21, 2025. It is now read-only.

Commit e441d07

Browse files
committed
Revert PHP-Activerecord version
1 parent d516369 commit e441d07

26 files changed

+7417
-7744
lines changed
Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
11
<?php
2-
3-
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 50300) {
4-
die('PHP ActiveRecord requires PHP 5.3 or higher');
5-
}
6-
7-
define('PHP_ACTIVERECORD_VERSION_ID', '1.0');
8-
9-
if (!defined('PHP_ACTIVERECORD_AUTOLOAD_PREPEND')) {
10-
define('PHP_ACTIVERECORD_AUTOLOAD_PREPEND', true);
11-
}
12-
13-
require __DIR__ . '/lib/Singleton.php';
14-
require __DIR__ . '/lib/Config.php';
15-
require __DIR__ . '/lib/Utils.php';
16-
require __DIR__ . '/lib/DateTimeInterface.php';
17-
require __DIR__ . '/lib/DateTime.php';
18-
require __DIR__ . '/lib/Model.php';
19-
require __DIR__ . '/lib/Table.php';
20-
require __DIR__ . '/lib/ConnectionManager.php';
21-
require __DIR__ . '/lib/Connection.php';
22-
require __DIR__ . '/lib/Serialization.php';
23-
require __DIR__ . '/lib/SQLBuilder.php';
24-
require __DIR__ . '/lib/Reflections.php';
25-
require __DIR__ . '/lib/Inflector.php';
26-
require __DIR__ . '/lib/CallBack.php';
27-
require __DIR__ . '/lib/Exceptions.php';
28-
require __DIR__ . '/lib/Cache.php';
29-
30-
if (!defined('PHP_ACTIVERECORD_AUTOLOAD_DISABLE')) {
31-
spl_autoload_register('activerecord_autoload', true, PHP_ACTIVERECORD_AUTOLOAD_PREPEND);
32-
}
2+
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 50300)
3+
die('PHP ActiveRecord requires PHP 5.3 or higher');
4+
5+
define('PHP_ACTIVERECORD_VERSION_ID','1.0');
6+
7+
if (!defined('PHP_ACTIVERECORD_AUTOLOAD_PREPEND'))
8+
define('PHP_ACTIVERECORD_AUTOLOAD_PREPEND',true);
9+
10+
require __DIR__.'/lib/Singleton.php';
11+
require __DIR__.'/lib/Config.php';
12+
require __DIR__.'/lib/Utils.php';
13+
require __DIR__.'/lib/DateTime.php';
14+
require __DIR__.'/lib/Model.php';
15+
require __DIR__.'/lib/Table.php';
16+
require __DIR__.'/lib/ConnectionManager.php';
17+
require __DIR__.'/lib/Connection.php';
18+
require __DIR__.'/lib/Serialization.php';
19+
require __DIR__.'/lib/SQLBuilder.php';
20+
require __DIR__.'/lib/Reflections.php';
21+
require __DIR__.'/lib/Inflector.php';
22+
require __DIR__.'/lib/CallBack.php';
23+
require __DIR__.'/lib/Exceptions.php';
24+
require __DIR__.'/lib/Cache.php';
25+
26+
if (!defined('PHP_ACTIVERECORD_AUTOLOAD_DISABLE'))
27+
spl_autoload_register('activerecord_autoload',false,PHP_ACTIVERECORD_AUTOLOAD_PREPEND);
3328

3429
function activerecord_autoload($class_name)
3530
{
36-
$path = ActiveRecord\Config::instance()->get_model_directory();
37-
$root = realpath(isset($path) ? $path : '.');
38-
39-
if (($namespaces = ActiveRecord\get_namespaces($class_name))) {
40-
$class_name = array_pop($namespaces);
41-
$directories = [];
42-
43-
foreach ($namespaces as $directory) {
44-
$directories[] = $directory;
45-
}
46-
47-
$root .= DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $directories);
48-
}
49-
50-
$file = "$root/$class_name.php";
51-
52-
if (file_exists($file)) {
53-
require_once $file;
54-
}
31+
$paths = ActiveRecord\Config::instance()->get_model_directories();
32+
$namespace_directory = '';
33+
if (($namespaces = ActiveRecord\get_namespaces($class_name)))
34+
{
35+
$class_name = array_pop($namespaces);
36+
$directories = array();
37+
38+
foreach ($namespaces as $directory)
39+
$directories[] = $directory;
40+
41+
$namespace_directory = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $directories);
42+
}
43+
$paths = count($paths) ? $paths : array('.');
44+
45+
foreach($paths as $path)
46+
{
47+
$root = realpath($path);
48+
$file = "{$root}{$namespace_directory}/{$class_name}.php";
49+
50+
if (file_exists($file))
51+
{
52+
require $file;
53+
return;
54+
}
55+
}
5556
}

lib/php-activerecord/lib/Cache.php

Lines changed: 112 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
2-
32
namespace ActiveRecord;
4-
53
use Closure;
64

75
/**
@@ -12,112 +10,116 @@
1210
*/
1311
class Cache
1412
{
15-
public static $adapter = null;
16-
public static $options = [];
17-
18-
/**
19-
* Initializes the cache.
20-
*
21-
* With the $options array it's possible to define:
22-
* - expiration of the key, (time in seconds)
23-
* - a namespace for the key
24-
*
25-
* this last one is useful in the case two applications use
26-
* a shared key/store (for instance a shared Memcached db)
27-
*
28-
* Ex:
29-
* $cfg_ar = ActiveRecord\Config::instance();
30-
* $cfg_ar->set_cache('memcache://localhost:11211',array('namespace' => 'my_cool_app',
31-
* 'expire' => 120
32-
* ));
33-
*
34-
* In the example above all the keys expire after 120 seconds, and the
35-
* all get a postfix 'my_cool_app'.
36-
*
37-
* (Note: expiring needs to be implemented in your cache store.)
38-
*
39-
* @param string $url URL to your cache server
40-
* @param array $options Specify additional options
41-
*/
42-
public static function initialize($url, $options=[])
43-
{
44-
if ($url) {
45-
$url = parse_url($url);
46-
$file = ucwords(Inflector::instance()->camelize($url['scheme']));
47-
$class = "ActiveRecord\\$file";
48-
require_once __DIR__ . "/cache/$file.php";
49-
static::$adapter = new $class($url);
50-
} else {
51-
static::$adapter = null;
52-
}
53-
54-
static::$options = array_merge(['expire' => 30, 'namespace' => ''], $options);
55-
}
56-
57-
public static function flush()
58-
{
59-
if (static::$adapter) {
60-
static::$adapter->flush();
61-
}
62-
}
63-
64-
/**
65-
* Attempt to retrieve a value from cache using a key. If the value is not found, then the closure method
66-
* will be invoked, and the result will be stored in cache using that key.
67-
*
68-
* @param $key
69-
* @param $closure
70-
* @param $expire in seconds
71-
*
72-
* @return mixed
73-
*/
74-
public static function get($key, $closure, $expire=null)
75-
{
76-
if (!static::$adapter) {
77-
return $closure();
78-
}
79-
80-
if (is_null($expire)) {
81-
$expire = static::$options['expire'];
82-
}
83-
84-
$key = static::get_namespace() . $key;
85-
86-
if (!($value = static::$adapter->read($key))) {
87-
static::$adapter->write($key, ($value = $closure()), $expire);
88-
}
89-
90-
return $value;
91-
}
92-
93-
public static function set($key, $var, $expire=null)
94-
{
95-
if (!static::$adapter) {
96-
return;
97-
}
98-
99-
if (is_null($expire)) {
100-
$expire = static::$options['expire'];
101-
}
102-
103-
$key = static::get_namespace() . $key;
104-
105-
return static::$adapter->write($key, $var, $expire);
106-
}
107-
108-
public static function delete($key)
109-
{
110-
if (!static::$adapter) {
111-
return;
112-
}
113-
114-
$key = static::get_namespace() . $key;
115-
116-
return static::$adapter->delete($key);
117-
}
118-
119-
private static function get_namespace()
120-
{
121-
return (isset(static::$options['namespace']) && strlen(static::$options['namespace']) > 0) ? (static::$options['namespace'] . '::') : '';
122-
}
13+
static $adapter = null;
14+
static $options = array();
15+
16+
/**
17+
* Initializes the cache.
18+
*
19+
* With the $options array it's possible to define:
20+
* - expiration of the key, (time in seconds)
21+
* - a namespace for the key
22+
*
23+
* this last one is useful in the case two applications use
24+
* a shared key/store (for instance a shared Memcached db)
25+
*
26+
* Ex:
27+
* $cfg_ar = ActiveRecord\Config::instance();
28+
* $cfg_ar->set_cache('memcache://localhost:11211',array('namespace' => 'my_cool_app',
29+
* 'expire' => 120
30+
* ));
31+
*
32+
* In the example above all the keys expire after 120 seconds, and the
33+
* all get a postfix 'my_cool_app'.
34+
*
35+
* (Note: expiring needs to be implemented in your cache store.)
36+
*
37+
* @param string $url URL to your cache server
38+
* @param array $options Specify additional options
39+
*/
40+
public static function initialize($url, $options=array())
41+
{
42+
if ($url)
43+
{
44+
$url = parse_url($url);
45+
$file = ucwords(Inflector::instance()->camelize($url['scheme']));
46+
$class = "ActiveRecord\\$file";
47+
require_once __DIR__ . "/cache/$file.php";
48+
static::$adapter = new $class($url);
49+
}
50+
else
51+
static::$adapter = null;
52+
53+
static::$options = array_merge(array('expire' => 30, 'namespace' => ''),$options);
54+
}
55+
56+
public static function flush()
57+
{
58+
if (static::$adapter)
59+
static::$adapter->flush();
60+
}
61+
62+
/**
63+
* Attempt to retrieve a value from cache using a key. If the value is not found, then the closure method
64+
* will be invoked, and the result will be stored in cache using that key.
65+
* @param $key
66+
* @param $closure
67+
* @param $expire in seconds
68+
* @return mixed
69+
*/
70+
public static function get($key, $closure, $expire=null)
71+
{
72+
if (!static::$adapter)
73+
return $closure();
74+
75+
if (is_null($expire))
76+
{
77+
$expire = static::$options['expire'];
78+
}
79+
80+
$key = static::get_namespace() . $key;
81+
82+
if (!($value = static::$adapter->read($key)))
83+
static::$adapter->write($key, ($value = $closure()), $expire);
84+
85+
return $value;
86+
}
87+
88+
/**
89+
* @param $key
90+
* @param $var
91+
* @param null $expire
92+
* @return void
93+
*/
94+
public static function set($key, $var, $expire=null)
95+
{
96+
if (!static::$adapter)
97+
return;
98+
99+
if (is_null($expire))
100+
{
101+
$expire = static::$options['expire'];
102+
}
103+
104+
$key = static::get_namespace() . $key;
105+
return static::$adapter->write($key, $var, $expire);
106+
}
107+
108+
/**
109+
* @param $key
110+
* @return void
111+
*/
112+
public static function delete($key)
113+
{
114+
if (!static::$adapter)
115+
return;
116+
117+
$key = static::get_namespace() . $key;
118+
return static::$adapter->delete($key);
119+
}
120+
121+
private static function get_namespace()
122+
{
123+
return (isset(static::$options['namespace']) && strlen(static::$options['namespace']) > 0) ? (static::$options['namespace'] . "::") : "";
124+
}
123125
}

0 commit comments

Comments
 (0)