Files
Jump/jumpapp/classes/Background.php

47 lines
1.5 KiB
PHP
Raw Normal View History

2022-02-04 09:53:55 +00:00
<?php
namespace Jump;
2022-02-04 11:52:57 +00:00
/**
* Return a random background image path selected from the list of files
* found in the /assets/backgrounds directory.
*
* @author Dale Davies <dale@daledavies.co.uk>
* @license MIT
*/
2022-02-04 09:53:55 +00:00
class Background {
private string $backgroundsdirectory;
private array $backgroundfiles;
public function __construct(Config $config) {
$this->config = $config;
$this->backgroundsdirectory = $config->get('backgroundsdir');
$this->webaccessibledir = str_replace($config->get('wwwroot'), '', $config->get('backgroundsdir'));
$this->enumerate_files();
}
2022-02-04 11:52:57 +00:00
/**
* Enumerate a list of background filenames from backgrounds directory.
*
* @return void
*/
2022-02-04 09:53:55 +00:00
private function enumerate_files(): void {
$this->backgroundfiles = array_diff(scandir($this->backgroundsdirectory), array('..', '.'));
}
2022-02-04 11:52:57 +00:00
/**
* Select a random file from the enumerated list in $this->backgroundfiles
* and optionally prefix with a web accessible path for the backgrounds
* directory.
*
* @param boolean $includepath Should the backgrounds directory path be prefixed?
* @return string The selected background image filename/, optionally including path.
*/
2022-02-04 09:53:55 +00:00
public function get_random_background_file(bool $includepath = true): string {
return ($includepath ? $this->webaccessibledir : '')
. '/'. $this->backgroundfiles[array_rand($this->backgroundfiles)];
}
}