Skip to content
This repository has been archived by the owner on Jun 6, 2020. It is now read-only.

Commit

Permalink
Add new method to get translation domain for each theme
Browse files Browse the repository at this point in the history
  • Loading branch information
emulienfou committed Apr 23, 2019
1 parent 5be6d52 commit 5bd879d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
58 changes: 44 additions & 14 deletions Theme/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

namespace Harmony\Sdk\Theme;

use Exception;
use ReflectionClassConstant;
use ReflectionObject;
use SplFileInfo;
use function array_slice;
use function dirname;
use function explode;
use function file_exists;
use function file_get_contents;
use function glob;
use function implode;
use function json_decode;
use function sprintf;
use function strrpos;
use function substr;

/**
* Class Theme
*
Expand Down Expand Up @@ -37,15 +53,19 @@ abstract class Theme implements ThemeInterface
/** @var null|ThemeInterface $parent */
protected $parent;

/** @var string $translationDomain */
protected $translationDomain;

/**
* Theme constructor.
*/
public function __construct()
{
$pos = \strrpos(static::class, '\\');
$this->identifier = false === $pos ? static::class : \substr(static::class, $pos + 1);
$this->path = \dirname((new \ReflectionObject($this))->getFileName());
$this->shortName = implode(DIRECTORY_SEPARATOR,
$pos = strrpos(static::class, '\\');
$this->identifier = false === $pos ? static::class : substr(static::class, $pos + 1);
$this->translationDomain = $this->identifier;
$this->path = dirname((new ReflectionObject($this))->getFileName());
$this->shortName = implode(DIRECTORY_SEPARATOR,
array_slice(explode(DIRECTORY_SEPARATOR, $this->path), - 2, 2));

$composer = $this->_parseComposer();
Expand All @@ -57,11 +77,11 @@ public function __construct()
$this->settingPath = $this->path . DIRECTORY_SEPARATOR . 'settings.yaml';

try {
$reflexionConstant = new \ReflectionClassConstant($this, 'PARENT');
$reflexionConstant = new ReflectionClassConstant($this, 'PARENT');
$value = $reflexionConstant->getValue();
$this->parent = new $value();
}
catch (\Exception $e) {
catch (Exception $e) {
}
}

Expand Down Expand Up @@ -93,10 +113,10 @@ final public function getShortName(): string
final public function getName(): string
{
try {
$reflexionConstant = new \ReflectionClassConstant($this, 'NAME');
$reflexionConstant = new ReflectionClassConstant($this, 'NAME');
$this->name = $reflexionConstant->getValue();
}
catch (\Exception $e) {
catch (Exception $e) {
}

return $this->name;
Expand All @@ -110,10 +130,10 @@ final public function getName(): string
final public function getDescription(): string
{
try {
$reflexionConstant = new \ReflectionClassConstant($this, 'DESCRIPTION');
$reflexionConstant = new ReflectionClassConstant($this, 'DESCRIPTION');
$this->description = $reflexionConstant->getValue();
}
catch (\Exception $e) {
catch (Exception $e) {
}

return $this->description;
Expand Down Expand Up @@ -146,7 +166,7 @@ final public function getAuthors(): array
*/
final public function hasSettings(): bool
{
return \file_exists($this->settingPath);
return file_exists($this->settingPath);
}

/**
Expand Down Expand Up @@ -180,16 +200,26 @@ final public function getParent(): ?ThemeInterface
return $this->parent;
}

/**
* Get the theme translation domain.
*
* @return string
*/
final public function getTransDomain(): string
{
return $this->translationDomain;
}

/**
* Returns the theme preview image.
*
* @return null|string The theme preview image
*/
public function getPreview(): ?string
{
$array = \glob($this->getPath() . '/assets/images/preview.{jpg,jpeg,png,gif}', GLOB_BRACE);
$array = glob($this->getPath() . '/assets/images/preview.{jpg,jpeg,png,gif}', GLOB_BRACE);
if (isset($array[0])) {
return sprintf('/themes/%s/images/%s', $this->shortName, (new \SplFileInfo($array[0]))->getBasename());
return sprintf('/themes/%s/images/%s', $this->shortName, (new SplFileInfo($array[0]))->getBasename());
}

return null;
Expand All @@ -203,6 +233,6 @@ public function getPreview(): ?string
*/
private function _parseComposer(): array
{
return \json_decode(\file_get_contents($this->path . DIRECTORY_SEPARATOR . 'composer.json'), true);
return json_decode(file_get_contents($this->path . DIRECTORY_SEPARATOR . 'composer.json'), true);
}
}
7 changes: 7 additions & 0 deletions Theme/ThemeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ public function getSettingPath(): string;
* @return null|ThemeInterface
*/
public function getParent(): ?ThemeInterface;

/**
* Get the theme translation domain.
*
* @return string
*/
public function getTransDomain(): string;
}

0 comments on commit 5bd879d

Please sign in to comment.