Skip to content

Commit

Permalink
Merge pull request #28647 from owncloud/stable10-extend-theme-service
Browse files Browse the repository at this point in the history
[stable10] extended theme service
  • Loading branch information
Vincent Petry authored Aug 11, 2017
2 parents 2aea943 + 380bd4d commit 32096d5
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 68 deletions.
1 change: 1 addition & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ public function __construct($webRoot, \OC\Config $config) {
$this->registerService('ThemeService', function ($c) {
return new ThemeService($this->getSystemConfig()->getValue('theme'));
});
$this->registerAlias('OCP\Theme\IThemeService', 'ThemeService');
$this->registerAlias('OCP\IUserSession', 'UserSession');
$this->registerAlias('OCP\Security\ICrypto', 'Crypto');
}
Expand Down
10 changes: 5 additions & 5 deletions lib/private/Template/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

namespace OC\Template;

use OC\Theme\Theme;
use OCP\Theme\ITheme;

class Base {
/**
Expand All @@ -49,7 +49,7 @@ class Base {
private $l10n;

/**
* @var Theme
* @var ITheme
*/
protected $theme;

Expand All @@ -62,7 +62,7 @@ class Base {
* @param string $template
* @param string $requestToken
* @param \OCP\IL10N $l10n
* @param Theme $theme
* @param ITheme $theme
* @param \OC_Defaults $themeDefaults
*/
public function __construct($template, $requestToken, $l10n, $theme, $themeDefaults) {
Expand All @@ -77,7 +77,7 @@ public function __construct($template, $requestToken, $l10n, $theme, $themeDefau
/**
* @param string $serverRoot
* @param string|false $app_dir
* @param Theme $theme
* @param ITheme $theme
* @param string $app
* @return string[]
*/
Expand All @@ -97,7 +97,7 @@ protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {

/**
* @param string $serverRoot
* @param Theme $theme
* @param ITheme $theme
* @return string[]
*/
protected function getCoreTemplateDirs($theme, $serverRoot) {
Expand Down
6 changes: 3 additions & 3 deletions lib/private/Template/ResourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@

namespace OC\Template;

use OC\Theme\Theme;
use OCP\Theme\ITheme;

abstract class ResourceLocator {
/**
* @var Theme
* @var ITheme
*/
protected $theme;

Expand All @@ -48,7 +48,7 @@ abstract class ResourceLocator {

/**
* @param \OCP\ILogger $logger
* @param Theme $theme
* @param ITheme $theme
* @param array $core_map
* @param array $party_map
*/
Expand Down
34 changes: 17 additions & 17 deletions lib/private/Theme/Theme.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
/**
* @author Philipp Schaffrath <github@philippschaffrath.de>
* @author Philipp Schaffrath <github@philipp.schaffrath.email>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
Expand All @@ -17,11 +16,12 @@
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Theme;

class Theme {
use OCP\Theme\ITheme;

class Theme implements ITheme {

/**
* @var string
Expand All @@ -39,14 +39,14 @@ class Theme {
private $webPath;

/**
* Theme constructor.
*
* @param string $name
* @param string $directory
* @param string $webPath
*/
public function __construct($name = '', $directory = '') {
public function __construct($name = '', $directory = '', $webPath = '') {
$this->name = $name;
$this->directory = $directory;
$this->webPath = $webPath;
}

/**
Expand All @@ -57,31 +57,31 @@ public function getName() {
}

/**
* @param $name
* @return string
*/
public function setName($name) {
$this->name = $name;
public function getDirectory() {
return $this->directory;
}

/**
* @return string
*/
public function getDirectory() {
return $this->directory;
public function getWebPath() {
return $this->webPath;
}

/**
* @param string $directory
* @param string $name
*/
public function setDirectory($directory) {
$this->directory = $directory;
public function setName($name) {
$this->name = $name;
}

/**
* @return string
* @param string $directory
*/
public function getWebPath() {
return $this->webPath;
public function setDirectory($directory) {
$this->directory = $directory;
}

/**
Expand Down
143 changes: 114 additions & 29 deletions lib/private/Theme/ThemeService.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
/**
* @author Philipp Schaffrath <github@philippschaffrath.de>
* @author Philipp Schaffrath <github@philipp.schaffrath.email>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
Expand All @@ -17,11 +16,12 @@
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Theme;

class ThemeService {
use OCP\Theme\IThemeService;

class ThemeService implements IThemeService {

/**
* @var Theme
Expand All @@ -39,7 +39,12 @@ class ThemeService {
*/
public function __construct($themeName = '', $defaultThemeDirectory = '') {
$this->setDefaultThemeDirectory($defaultThemeDirectory);
$this->createTheme($themeName);

if ($themeName === '' && $this->defaultThemeExists()) {
$themeName = 'default';
}

$this->theme = $this->makeTheme($themeName, false);
}

/**
Expand All @@ -53,58 +58,138 @@ private function setDefaultThemeDirectory($defaultThemeDirectory = '') {
}
}

/**
* @return bool
*/
private function defaultThemeExists() {
if (is_dir($this->defaultThemeDirectory)) {
return true;
}

return false;
}

/**
* @return Theme
*/
public function getTheme() {
return $this->theme;
}

/**
* @param string $themeName
*/
private function createTheme($themeName = '') {
if ($themeName === '' && $this->defaultThemeExists()) {
$themeName = 'default';
public function setAppTheme($themeName = '') {
$this->theme = $this->makeTheme($themeName, true, $this->getTheme());
}

/**
* @param string $themeName
* @param bool $appTheme
* @param Theme $theme
* @return Theme
*/
private function makeTheme($themeName, $appTheme = true, Theme $theme = null) {
$directory = $this->getDirectory($themeName, $appTheme);
$webPath = $this->getWebPath($themeName, $appTheme);

if (is_null($theme)) {
$theme = new Theme(
$themeName,
$directory,
$webPath
);
} else {
$theme->setName($themeName);
$theme->setDirectory($directory);
$theme->setWebPath($webPath);
}

$this->theme = new Theme($themeName, $this->getThemeDirectory($themeName));
return $theme;
}

/**
* @param string $themeName
* @param bool $appTheme
* @return string
*/
private function getThemeDirectory($themeName) {
private function getDirectory($themeName, $appTheme = true) {
if ($themeName !== '') {
return 'themes/' . $themeName . '/';
} else {
return '';
if ($appTheme) {
return substr(\OC_App::getAppPath($themeName), strlen(\OC::$SERVERROOT) + 1);
}
return 'themes/' . $themeName;
}

return '';
}

/**
* @return bool
* @param $themeName
* @param bool $appTheme
* @return false|string
*/
private function defaultThemeExists() {
if (is_dir($this->defaultThemeDirectory)) {
return true;
private function getWebPath($themeName, $appTheme = true) {
if ($themeName !== '') {
if ($appTheme) {
$appWebPath = \OC_App::getAppWebPath($themeName);
return $appWebPath ? $appWebPath : '';
}
return '/themes/' . $themeName;
}

return false;
return '';
}

/**
* @return Theme
* @return Theme[]
*/
public function getTheme() {
return $this->theme;
public function getAllThemes() {
return array_merge($this->getAllAppThemes(), $this->getAllLegacyThemes());
}

/**
* @param string $appName
* @return Theme[]
*/
public function setAppTheme($appName = '') {
if ($appName !== '') {
$this->theme->setDirectory(
substr(\OC_App::getAppPath($appName), strlen(\OC::$SERVERROOT) + 1)
);
$appWebPath = \OC_App::getAppWebPath($appName);
$this->theme->setWebPath($appWebPath ? $appWebPath : '');
$this->theme->setName($appName);
private function getAllAppThemes() {
$themes = [];
foreach (\OC::$server->getAppManager()->getAllApps() as $app) {
if (\OC_App::isType($app, 'theme')) {
$themes[$app] = $this->makeTheme($app);
}
}
return $themes;
}

/**
* @return Theme[]
*/
private function getAllLegacyThemes() {
$themes = [];
if ($handle = opendir(\OC::$SERVERROOT . '/themes')) {
while (false !== ($entry = readdir($handle))) {
if ($entry === '.' || $entry === '..') {
continue;
}
if (is_dir(\OC::$SERVERROOT . '/themes/' . $entry)) {
$themes[$entry] = $this->makeTheme($entry, false);
}
}
closedir($handle);
return $themes;
}
return $themes;
}

/**
* @param string $themeName
* @return Theme|false
*/
public function findTheme($themeName) {
$allThemes = $this->getAllThemes();
if (array_key_exists($themeName, $allThemes)) {
return $allThemes[$themeName];
}
return false;
}
}
6 changes: 3 additions & 3 deletions lib/private/URLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/

namespace OC;
use OC\Theme\Theme;
use OCP\Theme\ITheme;
use OC_Defaults;
use OCP\ICacheFactory;
use OCP\IConfig;
Expand All @@ -48,7 +48,7 @@ class URLGenerator implements IURLGenerator {
private $cacheFactory;
/** @var IRouter */
private $router;
/** @var Theme */
/** @var ITheme */
private $theme;

/**
Expand Down Expand Up @@ -187,7 +187,7 @@ private function getImagePath($app, $imageName) {
$file = $directory . $imageName;

if (!empty($themeDirectory)) {
if ($imagePath = $this->getImagePathOrFallback('/' . $this->theme->getDirectory() . '/' . $file)) {
if ($imagePath = $this->getImagePathOrFallback('/' . $this->theme->getDirectory() . $file)) {
return $imagePath;
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/private/legacy/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ public static function loadApp($app, $checkUpgrade = true) {
*/
private static function enableThemeIfApplicable($app) {
if (self::isType($app, 'theme')) {
/** @var \OC\Theme\ThemeService $themeManager */
$themeManager = \OC::$server->query('ThemeService');
$themeManager->setAppTheme($app);
/** @var \OCP\Theme\IThemeService $themeService */
$themeService = \OC::$server->query('ThemeService');
$themeService->setAppTheme($app);
}
}

Expand Down
Loading

0 comments on commit 32096d5

Please sign in to comment.