Skip to content

Commit 84367ff

Browse files
committed
Allow to use PCA as embeded package
1 parent ab7cc50 commit 84367ff

File tree

10 files changed

+34
-21
lines changed

10 files changed

+34
-21
lines changed

config.dist.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,6 @@
140140
'metricsrefresh' => 60, // In seconds, refresh interval for metrics - default 60
141141
'metricstab' => 1440, // Default tab in metrics, 60 - Last hour, 1440 - Last day, 10080 - Last week, 43200 - Last month - default 1440
142142
'hash' => 'pca', // Any random string to secure metrics DB file
143+
'tmpdir' => __DIR__.'/tmp',
144+
//'pcapath' => 'vendor/robinn/phpcacheadmin/', // Path to the package when installed via composer. User for assets.
143145
];

index.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
declare(strict_types=1);
88

9-
use Composer\InstalledVersions;
10-
use RobiNN\Pca\Admin;
11-
use RobiNN\Pca\Config;
12-
use RobiNN\Pca\Template;
13-
149
// Always display errors
1510
ini_set('display_errors', 'On');
1611
ini_set('display_startup_errors', 'On');
@@ -26,7 +21,7 @@
2621
require_once __DIR__.'/vendor/autoload.php';
2722

2823
if (!extension_loaded('redis') &&
29-
InstalledVersions::isInstalled('predis/predis') === false &&
24+
Composer\InstalledVersions::isInstalled('predis/predis') === false &&
3025
is_file($path.'predis.phar')
3126
) {
3227
require_once 'phar://'.$path.'predis.phar/vendor/autoload.php';
@@ -38,10 +33,9 @@
3833

3934
$auth = false;
4035

41-
if (is_callable(Config::get('auth'))) {
42-
Config::get('auth')();
36+
if (is_callable(RobiNN\Pca\Config::get('auth'))) {
37+
RobiNN\Pca\Config::get('auth')();
4338
$auth = true;
4439
}
4540

46-
$template = new Template();
47-
echo (new Admin($template))->render($auth);
41+
echo (new RobiNN\Pca\Admin())->render($auth);

src/Admin.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
class Admin {
1414
public const VERSION = '2.3.1';
1515

16+
private readonly Template $template;
17+
1618
/**
1719
* @var array<string, DashboardInterface>
1820
*/
1921
private array $dashboards = [];
2022

21-
public function __construct(private readonly Template $template) {
23+
public function __construct() {
24+
$this->template = new Template();
25+
2226
foreach (Config::get('dashboards', []) as $class) {
2327
if (is_subclass_of($class, DashboardInterface::class) && $class::check()) {
24-
$dashboard = new $class($template);
28+
$dashboard = new $class($this->template);
2529
$info = $dashboard->dashboardInfo();
2630
$this->dashboards[$info['key']] = $dashboard;
2731
}

src/Config.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ class Config {
1616
*/
1717
private static ?array $config = null;
1818

19+
private static ?string $config_path = null;
20+
21+
public static function setConfigPath(string $path): void {
22+
self::$config_path = $path;
23+
self::$config = null;
24+
}
25+
1926
/**
2027
* This is intended for use in tests.
2128
*/
2229
public static function reset(): void {
2330
self::$config = null;
31+
self::$config_path = null;
2432
}
2533

2634
/**
@@ -35,7 +43,9 @@ public static function get(string $key, $default = null) {
3543
return self::$config[$key] ?? $default;
3644
}
3745

38-
if (is_file(__DIR__.'/../config.php')) {
46+
if (self::$config_path !== null && is_file(self::$config_path)) {
47+
$config = (array) require self::$config_path;
48+
} elseif (is_file(__DIR__.'/../config.php')) {
3949
$config = (array) require __DIR__.'/../config.php';
4050
} elseif (is_file(__DIR__.'/../config.dist.php')) {
4151
$config = (array) require __DIR__.'/../config.dist.php';

src/Dashboards/Memcached/MemcachedMetrics.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public function __construct(
3333
) {
3434
$server_name = Helpers::getServerTitle($servers[$selected]);
3535
$hash = md5($server_name.Config::get('hash', 'pca'));
36-
$db = __DIR__.'/../../../tmp/memcached_metrics_'.$hash.'.db';
36+
$tmp = Config::get('tmpdir', __DIR__.'/../../../tmp');
37+
$db = $tmp.'/memcached_metrics_'.$hash.'.db';
3738

3839
$this->pdo = new PDO('sqlite:'.$db);
3940
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

src/Dashboards/Redis/RedisMetrics.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public function __construct(
3333
) {
3434
$server_name = Helpers::getServerTitle($servers[$selected]);
3535
$hash = md5($server_name.Config::get('hash', 'pca'));
36-
$db = __DIR__.'/../../../tmp/redis_metrics_'.$hash.'.db';
36+
$tmp = Config::get('tmpdir', __DIR__.'/../../../tmp');
37+
$db = $tmp.'/redis_metrics_'.$hash.'.db';
3738

3839
$this->pdo = new PDO('sqlite:'.$db);
3940
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

src/Template.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public function addPath(string $namespace, string $path): void {
4949

5050
private function initTwig(): Environment {
5151
$loader = new FilesystemLoader(__DIR__.'/../templates');
52+
$tmp = Config::get('tmpdir', __DIR__.'/../tmp');
5253
$twig = new Environment($loader, [
53-
'cache' => Config::get('twig-cache', __DIR__.'/../tmp/twig'),
54+
'cache' => Config::get('twig-cache', $tmp.'/twig'),
5455
'debug' => Config::get('twig-debug', false),
5556
]);
5657

templates/dashboards/memcached/metrics.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</div>
3434
</div>
3535

36-
<script src="assets/js/echarts.min.js"></script>
36+
<script src="{{ config('pcapath', '') }}assets/js/echarts.min.js"></script>
3737
<script>
3838
document.addEventListener('DOMContentLoaded', () => {
3939
const initial_theme = document.documentElement.classList.contains('dark') ? 'dark' : null;

templates/dashboards/redis/metrics.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</div>
2828
</div>
2929

30-
<script src="assets/js/echarts.min.js"></script>
30+
<script src="{{ config('pcapath', '') }}assets/js/echarts.min.js"></script>
3131
<script>
3232
document.addEventListener('DOMContentLoaded', () => {
3333
const initial_theme = document.documentElement.classList.contains('dark') ? 'dark' : null;

templates/layout.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1">
66
<title>{{ site_title }} - phpCacheAdmin</title>
7-
<link rel="icon" type="image/png" sizes="32x32" href="assets/favicon.png">
7+
<link rel="icon" type="image/png" sizes="32x32" href="{{ config('pcapath', '') }}assets/favicon.png">
88
<meta name="theme-color" content="#ffffff">
9-
<link rel="stylesheet" href="assets/css/styles.css?v={{ version }}">
9+
<link rel="stylesheet" href="{{ config('pcapath', '') }}assets/css/styles.css?v={{ version }}">
1010

1111
{%- if colors -%}
1212
<style>
@@ -97,6 +97,6 @@
9797
</div>
9898
</footer>
9999
{{ modals|raw }}
100-
<script src="assets/js/scripts.js?v={{ version }}"></script>
100+
<script src="{{ config('pcapath', '') }}assets/js/scripts.js?v={{ version }}"></script>
101101
</body>
102102
</html>

0 commit comments

Comments
 (0)