Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change usage of "base_path" and "public_path" #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
154 changes: 145 additions & 9 deletions src/Charcoal/App/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use InvalidArgumentException;
use UnexpectedValueException;

// From PSR-7
use Psr\Http\Message\UriInterface;
Expand Down Expand Up @@ -35,6 +36,13 @@ class AppConfig extends AbstractConfig
*/
private $projectName;

/**
* The base URL (public) for the Charcoal installation.
*
* @var UriInterface|null
*/
private $baseUrl;

/**
* The base path for the Charcoal installation.
*
Expand All @@ -43,18 +51,25 @@ class AppConfig extends AbstractConfig
private $basePath;

/**
* The base URL (public) for the Charcoal installation.
* The path to the public / web directory.
*
* @var UriInterface|null
* @var string|null
*/
private $baseUrl;
private $publicPath;

/**
* The path to the public / web directory.
* The path to the cache directory.
*
* @var string|null
*/
private $publicPath;
private $cachePath;

/**
* The path to the logs directory.
*
* @var string|null
*/
private $logsPath;

/**
* Whether the debug mode is enabled (TRUE) or not (FALSE).
Expand Down Expand Up @@ -176,6 +191,51 @@ public function defaults()
];
}

/**
* Replaces placeholders (%app.key%) by their values in the config.
*
* @param mixed $value A value to resolve.
* @throws UnexpectedValueException If the resolved value is not a string or number.
* @return mixed
*/
public function resolveValue($value)
{
$tags = [
'app.base_path' => $this->basePath(),
'app.public_path' => $this->publicPath(),
'app.cache_path' => $this->cachePath(),
'app.logs_path' => $this->logsPath(),
];

if (is_string($value)) {
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($tags, $value) {
// skip escaped %%
if (!isset($match[1])) {
return '%%';
}

$tag = $match[1];

$resolved = ($tags[$tag] ?? null);

if (!is_string($resolved) && !is_numeric($resolved)) {
$resolvedType = (is_object($resolved) ? get_class($resolved) : gettype($resolved));

throw new UnexpectedValueException(sprintf(
'Invalid config parameter "%s" inside string value "%s"; must be a string or number, received %s',
$key,
$value,
$resolvedType
));
}

return $resolved;
}, $value);
}

return $value;
}

/**
* Set the application's absolute root path.
*
Expand All @@ -199,7 +259,7 @@ public function setBasePath($path)
);
}

$this->basePath = rtrim(realpath($path), '\\/').DIRECTORY_SEPARATOR;
$this->basePath = rtrim(realpath($path), '\\/');
return $this;
}

Expand Down Expand Up @@ -233,7 +293,7 @@ public function setPublicPath($path)
);
}

$this->publicPath = rtrim(realpath($path), '\\/').DIRECTORY_SEPARATOR;
$this->publicPath = rtrim(realpath($path), '\\/');
return $this;
}

Expand All @@ -244,13 +304,89 @@ public function setPublicPath($path)
*/
public function publicPath()
{
if (!isset($this->publicPath)) {
$this->publicPath = $this->basePath().'www'.DIRECTORY_SEPARATOR;
if ($this->publicPath === null) {
$this->publicPath = $this->basePath().DIRECTORY_SEPARATOR.'www';
}

return $this->publicPath;
}

/**
* Set the application's absolute path to the cache directory.
*
* @param string $path The path to the application's cache directory.
* @throws InvalidArgumentException If the argument is not a string.
* @return self
*/
public function setCachePath($path)
{
if ($path === null) {
$this->cachePath = null;
return $this;
}

if (!is_string($path)) {
throw new InvalidArgumentException(
'The cache path must be a string'
);
}

$this->cachePath = rtrim(realpath($path), '\\/');
return $this;
}

/**
* Retrieve the application's absolute path to the cache directory.
*
* @return string The absolute path to the application's cache directory.
*/
public function cachePath()
{
if ($this->cachePath === null) {
$this->cachePath = $this->basePath().DIRECTORY_SEPARATOR.'var'.DIRECTORY_SEPARATOR.'cache';
}

return $this->cachePath;
}

/**
* Set the application's absolute path to the logs directory.
*
* @param string $path The path to the application's logs directory.
* @throws InvalidArgumentException If the argument is not a string.
* @return self
*/
public function setLogsPath($path)
{
if ($path === null) {
$this->logsPath = null;
return $this;
}

if (!is_string($path)) {
throw new InvalidArgumentException(
'The logs path must be a string'
);
}

$this->logsPath = rtrim(realpath($path), '\\/');
return $this;
}

/**
* Retrieve the application's absolute path to the logs directory.
*
* @return string The absolute path to the application's logs directory.
*/
public function logsPath()
{
if ($this->logsPath === null) {
$this->logsPath = $this->basePath().DIRECTORY_SEPARATOR.'var'.DIRECTORY_SEPARATOR.'logs';
}

return $this->logsPath;
}

/**
* Set the application's fully qualified base URL to the public web directory.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Charcoal/App/Config/FilesystemConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public function defaultConnections()
'public' => [
'public' => true,
'type' => 'local',
'path' => './',
'path' => '%app.public_path%',
'label' => 'Public',
],
'private' => [
'public' => false,
'type' => 'local',
'path' => '../',
'path' => '%app.base_path%',
'label' => 'Private',
],
];
Expand Down
2 changes: 1 addition & 1 deletion src/Charcoal/App/Config/LoggerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function defaults()
'handlers' => [
'stream' => [
'type' => 'stream',
'stream' => '../logs/charcoal.app.log',
'stream' => '%app.logs_path%/charcoal.app.log',
'level' => null,
'bubble' => true,
'active' => true,
Expand Down
17 changes: 8 additions & 9 deletions src/Charcoal/App/ServiceProvider/FilesystemServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Charcoal\App\ServiceProvider;

use Exception;
use LogicException;
use InvalidArgumentException;
use UnexpectedValueException;

Expand Down Expand Up @@ -37,6 +36,7 @@
use League\Flysystem\Memory\MemoryAdapter;

// From 'charcoal-app'
use Charcoal\App\AppConfig;
use Charcoal\App\Config\FilesystemConfig;

/**
Expand Down Expand Up @@ -142,22 +142,21 @@ private function createConnection(array $config)
/**
* @param array $config The driver (adapter) configuration.
* @throws InvalidArgumentException If the path is not defined.
* @throws LogicException If the path is not accessible.
* @return LocalAdapter
*/
private function createLocalAdapter(array $config)
{
if (!isset($config['path']) || !$config['path']) {
if (empty($config['path'])) {
throw new InvalidArgumentException(
'No "path" configured for local filesystem.'
);
}

$path = realpath($config['path']);
if ($path === false) {
throw new LogicException(
'Filesystem "path" does not exist.'
);
$path = $config['path'];
if (is_string($path)) {
if (isset($container['config']) && ($container['config'] instanceof AppConfig)) {
$path = $container['config']->resolveValue($path);
}
}

$defaults = [
Expand All @@ -167,7 +166,7 @@ private function createLocalAdapter(array $config)
];
$config = array_merge($defaults, $config);

return new LocalAdapter($config['path'], $config['lock'], $config['links'], $config['permissions']);
return new LocalAdapter($path, $config['lock'], $config['links'], $config['permissions']);
}

/**
Expand Down
19 changes: 18 additions & 1 deletion src/Charcoal/App/ServiceProvider/LoggerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Charcoal\App\ServiceProvider;

use InvalidArgumentException;

// From Pimple
use Pimple\ServiceProviderInterface;
use Pimple\Container;
Expand All @@ -22,6 +24,7 @@
use Charcoal\Factory\FactoryInterface;

// From 'charcoal-app'
use Charcoal\App\AppConfig;
use Charcoal\App\Config\LoggerConfig;

/**
Expand Down Expand Up @@ -61,6 +64,7 @@ public function register(Container $container)

/**
* @param Container $container A service container.
* @throws InvalidArgumentException If the path is not defined or invalid.
* @return StreamHandler|null
*/
$container['logger/handler/stream'] = function (Container $container) {
Expand All @@ -71,8 +75,21 @@ public function register(Container $container)
return null;
}

if (empty($handlerConfig['stream'])) {
throw new InvalidArgumentException(
'No "stream" configured for logger stream handler.'
);
}

$stream = $handlerConfig['stream'];
if (is_string($stream)) {
if (isset($container['config']) && ($container['config'] instanceof AppConfig)) {
$stream = $container['config']->resolveValue($stream);
}
}

$level = $handlerConfig['level'] ?: $loggerConfig['level'];
return new StreamHandler($handlerConfig['stream'], $level);
return new StreamHandler($stream, $level);
};

/**
Expand Down