-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.php
63 lines (57 loc) · 1.78 KB
/
cli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
class WP_LCache_CLI {
/**
* Enable WP LCache by creating a stub for object-cache.php
* and/or creating the required database tables.
*/
public function enable() {
wp_lcache_initialize_database_schema();
if ( defined( 'WP_LCACHE_OBJECT_CACHE' ) && WP_LCACHE_OBJECT_CACHE ) {
WP_CLI::success( 'WP LCache is already enabled.' );
return;
}
$drop_in = WP_CONTENT_DIR . '/object-cache.php';
if ( file_exists( $drop_in ) ) {
WP_CLI::error( 'Unknown wp-content/object-cache.php already exists.' );
}
$object_cache = dirname( __FILE__ ) . '/object-cache.php';
$target = str_replace( WP_CONTENT_DIR, '', $object_cache );
// @codingStandardsIgnoreStart
if ( self::make_stub( $target ) ) {
// @codingStandardsIgnoreEnd
WP_CLI::success( 'Enabled WP LCache by creating wp-content/object-cache.php stub file.' );
} else {
WP_CLI::error( 'Failed create wp-content/object-cache.php stub to enable WP LCache.' );
}
}
/**
* Stub contents.
*/
private function make_stub( $target ) {
$stub = <<<EndPHPBlock
<?php
# Engage LCache object caching system.
# We use a 'require_once()' here because in PHP 5.5+ changes to symlinks
# are not detected by the opcode cache, making it frustrating to deploy.
#
# More info: http://codinghobo.com/opcache-and-symlink-based-deployments/
#
\$lcache_path = dirname( realpath( __FILE__ ) ) . '$target';
require_once( \$lcache_path );
EndPHPBlock;
chdir( WP_CONTENT_DIR );
try {
$fp = fopen( 'object-cache.php', 'w' );
// @codingStandardsIgnoreStart
// It's ok to write files.
fwrite( $fp, $stub );
// @codingStandardsIgnoreEnd
fclose( $fp );
} catch ( Exception $e ) {
// TODO: more granular exception handling?
return false;
}
return true;
}
}
WP_CLI::add_command( 'lcache', 'WP_LCache_CLI' );