Skip to content

Commit b2f2be2

Browse files
committed
Don't create a new Option object each time
1 parent 792b896 commit b2f2be2

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

includes/Config/Init.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public function __construct(InitDefaults $actions) {
6767
return;
6868
}
6969

70+
// Init options
71+
if ($actions->init_options === true) {
72+
yourls_init_options();
73+
}
74+
7075
// Read options right from start
7176
if ($actions->get_all_options === true) {
7277
yourls_get_all_options();

includes/Config/InitDefaults.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ class InitDefaults {
8080
*/
8181
public $return_if_fast_init = true;
8282

83+
/**
84+
* Whether to init the option system
85+
* @var bool
86+
*/
87+
public $init_options = true;
88+
8389
/**
8490
* Whether to read all options at once during starting
8591
* @var bool

includes/functions.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,12 +1112,27 @@ function yourls_get_option( $option_name, $default = false ) {
11121112
return $pre;
11131113

11141114
global $ydb;
1115-
$option = new \YOURLS\Database\Options($ydb);
1116-
$value = $option->get($option_name, $default);
1115+
$value = $ydb->yourls_options->get($option_name, $default);
11171116

11181117
return yourls_apply_filter( 'get_option_'.$option_name, $value );
11191118
}
11201119

1120+
/**
1121+
* Init option
1122+
*
1123+
* Create an option object to be re-used
1124+
*
1125+
* @since
1126+
* @param unknown_type $a
1127+
* @param unknown_type $b
1128+
* @return unknown
1129+
*/
1130+
function yourls_init_options() {
1131+
global $ydb;
1132+
1133+
$ydb->yourls_options = new \YOURLS\Database\Options($ydb);
1134+
}
1135+
11211136
/**
11221137
* Read all options from DB at once
11231138
*
@@ -1135,9 +1150,7 @@ function yourls_get_all_options() {
11351150
return $pre;
11361151

11371152
global $ydb;
1138-
$options = new \YOURLS\Database\Options($ydb);
1139-
1140-
if ($options->get_all_options() === false) {
1153+
if ($ydb->yourls_options->get_all_options() === false) {
11411154
// Zero option found but no unexpected error so far: YOURLS isn't installed
11421155
yourls_set_installed(false);
11431156
return;
@@ -1158,9 +1171,7 @@ function yourls_get_all_options() {
11581171
*/
11591172
function yourls_update_option( $option_name, $newvalue ) {
11601173
global $ydb;
1161-
1162-
$option = new \YOURLS\Database\Options($ydb);
1163-
$update = $option->update($option_name, $newvalue);
1174+
$update = $ydb->yourls_options->update($option_name, $newvalue);
11641175

11651176
return $update;
11661177
}
@@ -1177,9 +1188,7 @@ function yourls_update_option( $option_name, $newvalue ) {
11771188
*/
11781189
function yourls_add_option( $name, $value = '' ) {
11791190
global $ydb;
1180-
1181-
$option = new \YOURLS\Database\Options($ydb);
1182-
$add = $option->add($name, $value);
1191+
$add = $ydb->yourls_options->add($name, $value);
11831192

11841193
return $add;
11851194
}
@@ -1196,9 +1205,7 @@ function yourls_add_option( $name, $value = '' ) {
11961205
*/
11971206
function yourls_delete_option( $name ) {
11981207
global $ydb;
1199-
1200-
$option = new \YOURLS\Database\Options($ydb);
1201-
$delete = $option->delete($name);
1208+
$delete = $ydb->yourls_options->delete($name);
12021209

12031210
return $delete;
12041211
}

tests/tests/install/install.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function test_init_tables() {
2929
* Test (sort of) table creation
3030
*/
3131
public function test_create_tables() {
32-
32+
3333
/* The expected result has:
3434
* - success messages: the table are created with a "CREATE IF NOT EXISTS",
3535
* hence, will not be recreated once more, they're already created
@@ -41,7 +41,7 @@ public function test_create_tables() {
4141
* set of tables (with another prefix for instance).
4242
* Well. Consider this for next DB engine maybe? :)
4343
*/
44-
44+
4545
$expected = array(
4646
'success' => array (
4747
"Table 'yourls_url' created.",
@@ -54,21 +54,21 @@ public function test_create_tables() {
5454
'Could not insert sample short URLs',
5555
),
5656
);
57-
57+
5858
$this->assertSame( $expected, yourls_create_sql_tables() );
5959
}
60-
60+
6161
/**
6262
* Test (sort of) defining constants
6363
*/
6464
public function test_correct_config() {
6565
$test = new \YOURLS\Config\Config(YOURLS_CONFIGFILE);
66-
66+
6767
// This should return a readable file
6868
$readable = is_readable($test->find_config(YOURLS_CONFIGFILE));
6969
$this->assertTrue($readable);
7070
// For the record, $this->assertFileIsReadable() was introduced around PHPUnit 5.6
71-
71+
7272
// redefining YOURLS_ constants should not throw any error ("constant already defined...")
7373
// or define any new constants
7474
$consts = get_defined_constants(true);
@@ -103,7 +103,7 @@ public function test_not_found_config() {
103103
*/
104104
public function test_init_defaults() {
105105
$test = new \YOURLS\Config\InitDefaults();
106-
106+
107107
$expected = array (
108108
'include_core_funcs' => true,
109109
'include_auth_funcs' => false,
@@ -116,6 +116,7 @@ public function test_init_defaults() {
116116
'include_db' => true,
117117
'include_cache' => true,
118118
'return_if_fast_init' => true,
119+
'init_options' => true,
119120
'get_all_options' => true,
120121
'register_shutdown' => true,
121122
'core_loaded' => true,
@@ -126,10 +127,10 @@ public function test_init_defaults() {
126127
'check_new_version' => true,
127128
'init_admin' => true,
128129
);
129-
130+
130131
$actual = get_class_vars(get_class($test));
131-
132+
132133
$this->assertSame($expected, $actual);
133134
}
134-
135+
135136
}

0 commit comments

Comments
 (0)