Skip to content

Commit b5c7c7a

Browse files
Merge branch 'main' into 6.0
2 parents bc4b3fb + 85d5724 commit b5c7c7a

File tree

4 files changed

+239
-1
lines changed

4 files changed

+239
-1
lines changed

src/Adapter/WordPress.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
namespace phpbu\App\Adapter;
3+
4+
use phpbu\App\Adapter;
5+
use phpbu\App\Configuration;
6+
use phpbu\App\Exception;
7+
use phpbu\App\Util as AppUtil;
8+
9+
/**
10+
* WordPress Adapter by @planetahuevo based on https://rokaweb.ir/read-wpconfig-php/
11+
* It reads the wordpress database variables and return them
12+
*
13+
* @package phpbu
14+
* @subpackage App
15+
* @author Sebastian Feldmann <sebastian@phpbu.de>
16+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
17+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
18+
* @link http://phpbu.de/
19+
* @since Class available since Release 6.0.11
20+
*/
21+
class WordPress implements Adapter
22+
{
23+
/**
24+
* Path to the config file
25+
*
26+
* @var string
27+
*/
28+
private $file;
29+
30+
/**
31+
* Contents of wordpress configuration
32+
*
33+
* @var string
34+
*/
35+
private $config;
36+
37+
/**
38+
* Setup the adapter.
39+
*
40+
* @param array $conf
41+
* @return void
42+
* @throws Exception
43+
*/
44+
public function setup(array $conf)
45+
{
46+
$path = AppUtil\Arr::getValue($conf, 'file', 'wp-config.php');
47+
$this->file = AppUtil\Path::toAbsolutePath($path, Configuration::getWorkingDirectory());
48+
$this->load();
49+
}
50+
51+
/**
52+
* Load config file to local file.
53+
*
54+
* @throws Exception
55+
*/
56+
private function load()
57+
{
58+
if (!file_exists($this->file)) {
59+
throw new Exception('config file not found');
60+
}
61+
// read contents of wordpress config
62+
// we can not require the configuration because it requires a shit load of other files
63+
$this->config = file_get_contents($this->file);
64+
}
65+
66+
/**
67+
* Return value a the constant with the given name.
68+
*
69+
* @param string $path
70+
* @return string
71+
* @throws Exception
72+
*/
73+
public function getValue(string $path) : string
74+
{
75+
$regex = "/define.+?'" . $path . "'.+?'(.*?)'.+/";
76+
$match = [];
77+
if (!preg_match($regex, $this->config, $match)) {
78+
throw new Exception('constant ' . $path . ' not found');
79+
}
80+
return (string) $match[1];
81+
}
82+
}

src/Factory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class Factory
3232
// alias => fqcn
3333
'adapter' => [
3434
'array' => '\\phpbu\\App\\Adapter\\PHPArray',
35-
'constants' => '\\phpbu\\App\\Adapter\\PHPConstant',
35+
'constant' => '\\phpbu\\App\\Adapter\\PHPConstant',
3636
'dotenv' => '\\phpbu\\App\\Adapter\\Dotenv',
3737
'env' => '\\phpbu\\App\\Adapter\\Env',
38+
'wordpress' => '\\phpbu\\App\\Adapter\\WordPress',
3839
],
3940
'logger' => [
4041
'json' => '\\phpbu\\App\\Log\\Json',

tests/_files/misc/wp-config.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
define('FS_METHOD', 'direct');
3+
/**
4+
* The base configuration for WordPress
5+
*
6+
* The wp-config.php creation script uses this file during the
7+
* installation. You don't have to use the web site, you can
8+
* copy this file to "wp-config.php" and fill in the values.
9+
*
10+
* This file contains the following configurations:
11+
*
12+
* * MySQL settings
13+
* * Secret keys
14+
* * Database table prefix
15+
* * ABSPATH
16+
*
17+
* @link https://codex.wordpress.org/Editing_wp-config.php
18+
*
19+
* @package WordPress
20+
*/
21+
22+
// ** MySQL settings - You can get this info from your web host ** //
23+
/** The name of the database for WordPress */
24+
define('DB_NAME', 'phpbu');
25+
26+
/** MySQL database username */
27+
define('DB_USER', 'sebastian');
28+
29+
/** MySQL database password */
30+
define('DB_PASSWORD', 'secret');
31+
32+
/** MySQL hostname */
33+
define('DB_HOST', 'localhost');
34+
35+
/** Database Charset to use in creating database tables. */
36+
define('DB_CHARSET', 'utf8mb4');
37+
38+
/** The Database Collate type. Don't change this if in doubt. */
39+
define('DB_COLLATE', '');
40+
41+
/**#@+
42+
* Authentication Unique Keys and Salts.
43+
*
44+
* Change these to different unique phrases!
45+
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
46+
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
47+
*
48+
* @since 2.6.0
49+
*/
50+
define('AUTH_KEY', '1234');
51+
define('SECURE_AUTH_KEY', '1234');
52+
define('LOGGED_IN_KEY', '1234');
53+
define('NONCE_KEY', '1234');
54+
define('AUTH_SALT', '1234');
55+
define('SECURE_AUTH_SALT', '1234');
56+
define('LOGGED_IN_SALT', '1234');
57+
define('NONCE_SALT', '1234');
58+
59+
/**#@-*/
60+
61+
/**
62+
* WordPress Database Table prefix.
63+
*
64+
* You can have multiple installations in one database if you give each
65+
* a unique prefix. Only numbers, letters, and underscores please!
66+
*/
67+
$table_prefix = 'wp_';
68+
69+
/**
70+
* For developers: WordPress debugging mode.
71+
*
72+
* Change this to true to enable the display of notices during development.
73+
* It is strongly recommended that plugin and theme developers use WP_DEBUG
74+
* in their development environments.
75+
*
76+
* For information on other constants that can be used for debugging,
77+
* visit the Codex.
78+
*
79+
* @link https://codex.wordpress.org/Debugging_in_WordPress
80+
*/
81+
define('WP_DEBUG', false);
82+
83+
/* That's all, stop editing! Happy blogging. */
84+
85+
/** Absolute path to the WordPress directory. */
86+
if ( !defined('ABSPATH') )
87+
define('ABSPATH', dirname(__FILE__) . '/');
88+
89+
/** Sets up WordPress vars and included files. */
90+
require_once(ABSPATH . 'wp-settings.php');
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
namespace phpbu\App\Adapter;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
/**
7+
* Wordpress test
8+
*
9+
* @package phpbu
10+
* @subpackage tests
11+
* @author Sebastian Feldmann <sebastian@phpbu.de>
12+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
13+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
14+
* @link http://www.phpbu.de/
15+
* @since Class available since Release 6.0.12
16+
*/
17+
class WordpressTest extends TestCase
18+
{
19+
/**
20+
* Tests Wordpress::setup
21+
*/
22+
public function testSetup()
23+
{
24+
$arr = new Wordpress();
25+
$arr->setup(['file' => PHPBU_TEST_FILES . '/misc/wp-config.php']);
26+
27+
$this->assertTrue(true);
28+
}
29+
30+
/**
31+
* Tests Wordpress::setup
32+
*/
33+
public function testSetupFail()
34+
{
35+
$this->expectException('phpbu\App\Exception');
36+
$arr = new Wordpress();
37+
$arr->setup(['file' => 'wp-config.php']);
38+
}
39+
40+
/**
41+
* Tests Wordpress::getValue
42+
*/
43+
public function testGetValue()
44+
{
45+
$arr = new Wordpress();
46+
$arr->setup(['file' => PHPBU_TEST_FILES . '/misc/wp-config.php']);
47+
48+
$dbName = $arr->getValue('DB_NAME');
49+
50+
$this->assertEquals('phpbu', $dbName);
51+
}
52+
53+
54+
/**
55+
* Tests Wordpress::getValue
56+
*/
57+
public function testGetValueFail()
58+
{
59+
$this->expectException('phpbu\App\Exception');
60+
$arr = new Wordpress();
61+
$arr->setup(['file' => PHPBU_TEST_FILES . '/misc/wp-config.php']);
62+
63+
$arr->getValue('DB_FAIL');
64+
}
65+
}

0 commit comments

Comments
 (0)