Skip to content

Commit

Permalink
WIP : Support Traits
Browse files Browse the repository at this point in the history
Changes:
- Added Support namespace;
- Added AdminTrait to handle debug mode, admin config, and app config;
- Added BaseUrlTrait to handle site URLs and admin URLs;
- Added AssetsTrait to help widgets and templates queue styles, scripts, and localize the latter;
- Modified usage of Base URL and Admin URL across the codebase;
- Modified usage of App Config and Admin Config across the codebase;
  • Loading branch information
mcaskill committed Mar 9, 2017
1 parent 9a4c009 commit b3cb5ff
Show file tree
Hide file tree
Showing 20 changed files with 536 additions and 328 deletions.
2 changes: 1 addition & 1 deletion src/Charcoal/Admin/Action/Account/LostPasswordAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private function sendLostPasswordEmail(User $user, $token)
'token' => $token->id(),
'siteName' => $siteName,
'adminUrl' => $this->adminUrl(),
'urlResetPassword' => $this->adminUrl().'account/reset-password/'.$token->id(),
'urlResetPassword' => $this->withAdminUrl('account/reset-password/'.$token->id()),
'expiry' => $token->expiry()->format('Y-m-d H:i:s'),
'ipAddress' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''
]
Expand Down
97 changes: 72 additions & 25 deletions src/Charcoal/Admin/Action/ElfinderConnectorAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Charcoal\Admin\Action;

use ArrayAccess;
use ArrayObject;
use RuntimeException;
use InvalidArgumentException;

// From PSR-7
use Psr\Http\Message\RequestInterface;
Expand All @@ -17,6 +20,7 @@
use elFinder;

// From 'charcoal-config'
use Charcoal\Config\ConfigInterface;
use Charcoal\Config\GenericConfig as Config;

// From 'charcoal-factory'
Expand All @@ -38,13 +42,6 @@ class ElfinderConnectorAction extends AdminAction
{
use CallableResolverAwareTrait;

/**
* The base URI for the Charcoal application.
*
* @var UriInterface|string
*/
protected $baseUrl;

/**
* The relative path (from filesystem's root) to the storage directory.
*
Expand Down Expand Up @@ -91,8 +88,7 @@ public function setDependencies(Container $container)
{
parent::setDependencies($container);

$this->baseUrl = $container['base-url'];
$this->elfinderConfig = $container['elfinder/config'];
$this->setElfinderConfig($container['elfinder/config']);
$this->setPropertyFactory($container['property/factory']);
$this->setCallableResolver($container['callableResolver']);

Expand All @@ -101,6 +97,52 @@ public function setDependencies(Container $container)
$this->filesystems = $container['filesystems'];
}

/**
* Set the elFinder configset.
*
* @param array|ArrayAccess $config A configset.
* @throws InvalidArgumentException If the configset is invalid.
* @return self
*/
protected function setElfinderConfig($config)
{
if (is_array($config)) {
$config = new Config($config);
}

if (!$config instanceof ConfigInterface) {
throw new InvalidArgumentException('The configset must be array-accessible.');
}

$this->elfinderConfig = $config;

return $this;
}

/**
* Retrieve the elFinder configset.
*
* @param string|null $key Optional data key to retrieve from the configset.
* @param mixed|null $default The default value to return if data key does not exist.
* @return mixed|ArrayAccess
*/
protected function elfinderConfig($key = null, $default = null)
{
if ($key) {
if (isset($this->elfinderConfig[$key])) {
return $this->elfinderConfig[$key];
} else {
if (!is_string($default) && is_callable($default)) {
return $default();
} else {
return $default;
}
}
}

return $this->elfinderConfig;
}

/**
* Set a property factory.
*
Expand Down Expand Up @@ -182,15 +224,15 @@ public function connectorOptions(array $extraOptions = [])
if ($this->elfinderOptions === null || !empty($extraOptions)) {
$options = $this->defaultConnectorOptions();

if (isset($this->elfinderConfig['connector'])) {
if ($this->elfinderConfig('connector')) {
$options = array_replace_recursive(
$options,
$this->elfinderConfig['connector'],
$this->elfinderConfig('connector'),
$extraOptions
);
} else {
/** @todo Remove this deprecation notice for the next release */
$keys = $this->elfinderConfig->keys();
$keys = $this->elfinderConfig()->keys();
if (!empty($keys) && array_intersect([ 'bind', 'plugin', 'roots' ], $keys)) {
trigger_error(
'elFinder connector settings must be nested under [admin.elfinder.connector].',
Expand Down Expand Up @@ -231,19 +273,22 @@ public function defaultConnectorOptions()
{
$uploadPath = $this->uploadPath();

$defaultBaseUrl = rtrim((string)$this->baseUrl, '/');

$filesystemConfig = $this->filesystemConfig;
$filesystems = $this->filesystems;

$roots = [];
$currentFileSystem = $this->formProperty() ? $this->formProperty()->fileSystem() : false;

$siteUrl = $this->baseUrl();

if ($currentFileSystem && isset($filesystems[$currentFileSystem])) {
$config = $filesystemConfig['connections'][$currentFileSystem];
$label = isset($config['label']) ?
$this->translator()->translation($config['label']) : ucfirst($currentFileSystem);
$baseUrl = isset($config['base_url']) ? $config['base_url'] : $defaultBaseUrl;
$label = isset($config['label'])
? $this->translator()->translation($config['label'])
: ucfirst($currentFileSystem);
$baseUrl = isset($config['base_url'])
? $siteUrl::createFromString($config['base_url'])
: $siteUrl;

$startPath = $this->formProperty()->uploadPath();

Expand All @@ -269,8 +314,8 @@ public function defaultConnectorOptions()
// Enable localized folder names
'i18nFolderName' => true,
// URL to files (REQUIRED)
'URL' => $baseUrl.'/'.$uploadPath,
'tmbURL' => $defaultBaseUrl.'/'.$uploadPath.'/.tmb',
'URL' => $baseUrl->withPath($uploadPath),
'tmbURL' => $siteUrl->withPath($uploadPath.'/.tmb'),
'tmbPath' => $uploadPath.'/.tmb',
'tmbSize' => 200,
'tmbBgColor' => 'transparent',
Expand Down Expand Up @@ -298,9 +343,12 @@ public function defaultConnectorOptions()
if (isset($config['public']) && !$config['public']) {
continue;
}
$label = isset($config['label']) ?
$this->translator()->translation($config['label']) : ucfirst($filesystem);
$baseUrl = isset($config['base_url']) ? $config['base_url'] : $defaultBaseUrl;
$label = isset($config['label'])
? $this->translator()->translation($config['label'])
: ucfirst($filesystem);
$baseUrl = isset($config['base_url'])
? $siteUrl::createFromString($config['base_url'])
: $siteUrl;

$roots[$filesystem] = [
'driver' => 'Flysystem',
Expand All @@ -318,8 +366,8 @@ public function defaultConnectorOptions()
// Enable localized folder names
'i18nFolderName' => true,
// URL to files (REQUIRED)
'URL' => $baseUrl.'/'.$uploadPath,
'tmbURL' => $defaultBaseUrl.'/'.$uploadPath.'/.tmb',
'URL' => $baseUrl->withPath($uploadPath),
'tmbURL' => $siteUrl->withPath($uploadPath.'/.tmb'),
'tmbPath' => $uploadPath.'/.tmb',
'tmbSize' => 200,
'tmbBgColor' => 'transparent',
Expand All @@ -336,7 +384,6 @@ public function defaultConnectorOptions()
'attributes' => [
$this->attributesForHiddenFiles()
]

];
}

Expand Down
63 changes: 11 additions & 52 deletions src/Charcoal/Admin/AdminAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,25 @@

// From 'charcoal-admin'
use Charcoal\Admin\Ui\FeedbackContainerTrait;
use Charcoal\Admin\Support\AdminTrait;
use Charcoal\Admin\Support\BaseUrlTrait;

/**
* The base class for the `admin` Actions.
*/
abstract class AdminAction extends AbstractAction implements
AuthAwareInterface
{
use AdminTrait;
use BaseUrlTrait;
use AuthAwareTrait;
use FeedbackContainerTrait;
use TranslatorAwareTrait;

/**
* Store a reference to the admin configuration.
*
* @var \Charcoal\Admin\Config
*/
protected $adminConfig;

/**
* Store a reference to the application configuration.
*
* @var \Charcoal\App\Config
*/
protected $appConfig;

/**
* The name of the project.
*
* @var \Charcoal\Translator\Translation|string|null
* @var Translation|string|null
*/
private $siteName;

Expand Down Expand Up @@ -86,12 +76,14 @@ public function setDependencies(Container $container)
{
parent::setDependencies($container);

// Satisfies AdminTrait dependencies
$this->setDebug($container['debug']);
$this->setBaseUrl($container['base-url']);
$this->setAdminConfig($container['admin/config']);

$this->setModelFactory($container['model/factory']);
$this->setTranslator($container['translator']);

$this->appConfig = $container['config'];
$this->adminConfig = $container['admin/config'];
$this->setBaseUrl($container['base-url']);
$this->setSiteName($container['config']['project_name']);

// Satisfies AuthAwareInterface dependencies
Expand Down Expand Up @@ -215,7 +207,7 @@ public function getAuthenticatedUser()
protected function validateCaptcha($response)
{
$validationUrl = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha = $this->appConfig['apis.google.recaptcha'];
$recaptcha = $this->appConfig('apis.google.recaptcha');

if (isset($recaptcha['private_key'])) {
$secret = $recaptcha['private_key'];
Expand Down Expand Up @@ -248,37 +240,4 @@ public function results()
];
return $results;
}

/**
* Retrieve the base URI of the administration area.
*
* @return UriInterface|string
*/
public function adminUrl()
{
$adminPath = $this->adminConfig['base_path'];
return rtrim($this->baseUrl(), '/').'/'.rtrim($adminPath, '/').'/';
}

/**
* Set the base URI of the application.
*
* @param UriInterface|string $uri The base URI.
* @return self
*/
public function setBaseUrl($uri)
{
$this->baseUrl = $uri;
return $this;
}

/**
* Retrieve the base URI of the application.
*
* @return UriInterface|string
*/
public function baseUrl()
{
return rtrim($this->baseUrl, '/').'/';
}
}

0 comments on commit b3cb5ff

Please sign in to comment.