Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Rareloop\Lumberjack\Http\ResponseEmitter;
use Symfony\Component\Filesystem\Filesystem;
use Rareloop\Lumberjack\Autodiscovery\AutodiscoveredPackages;
use Rareloop\Lumberjack\Autodiscovery\ManifestCache;
use Rareloop\Lumberjack\Autodiscovery\PackageManifest;

class Application implements ContainerInterface
{
Expand Down Expand Up @@ -37,13 +41,30 @@ public function setBasePath(string $basePath)
{
$this->basePath = $basePath;

$this->bootstrapContainer();
}

protected function bootstrapContainer()
{
$this->bindPathsInContainer();

$this->registerAutodiscoveryBindings();
}

protected function bindPathsInContainer()
{
$this->bind('path.base', $this->basePath());
$this->bind('path.config', $this->configPath());
$this->bind('path.bootstrap', $this->bootstrapPath());
$this->bind('path.vendor', $this->vendorPath());
}

protected function registerAutodiscoveryBindings()
{
$this->singleton(ManifestCache::class, \DI\autowire()
->constructorParameter('cachePath', $this->bootstrapPath('cache' . DIRECTORY_SEPARATOR . 'packages.php')));

$this->singleton(AutodiscoveredPackages::class, \DI\autowire());
}

public function basePath()
Expand All @@ -56,6 +77,16 @@ public function configPath()
return $this->basePath . DIRECTORY_SEPARATOR . 'config';
}

public function vendorPath()
{
return $this->basePath . DIRECTORY_SEPARATOR . 'vendor';
}

public function bootstrapPath(string $path = '')
{
return $this->basePath . DIRECTORY_SEPARATOR . 'bootstrap' . ($path ? DIRECTORY_SEPARATOR . $path : '');
}

public function bind($key, $value)
{
// Prevent PHP-DI from creating singletons from class binds or closure factories
Expand Down
37 changes: 37 additions & 0 deletions src/Autodiscovery/AutodiscoveredPackages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rareloop\Lumberjack\Autodiscovery;

use Illuminate\Support\Arr;

class AutodiscoveredPackages
{
protected ?array $manifest = null;

public function __construct(protected ManifestCache $cache)
{
}

public function providers(): array
{
return (array) Arr::get($this->getManifest(), 'providers', []);
}

public function aliases(): array
{
return (array) Arr::get($this->getManifest(), 'aliases', []);
}

protected function getManifest(): array
{
if ($this->manifest !== null) {
return $this->manifest;
}

if ($this->cache->exists()) {
return $this->manifest = (array) $this->cache->read();
}

return $this->manifest = ['providers' => [], 'aliases' => []];
}
}
72 changes: 72 additions & 0 deletions src/Autodiscovery/DiscoveryRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Rareloop\Lumberjack\Autodiscovery;

use Composer\Script\Event;
use Illuminate\Support\Arr;

class DiscoveryRunner
{
/**
* Run the discovery process.
*
* @return void
*/
public function __invoke(Event $event): void
{
$composer = $event->getComposer();
$io = $event->getIO();
$vendorPath = $composer->getConfig()->get('vendor-dir');
$projectPath = dirname($vendorPath);
$extra = $composer->getPackage()->getExtra();

try {
$themePath = $this->resolveThemeDirectory($projectPath, $extra);

$builder = new PackageManifest($projectPath, $vendorPath);
$cachePath = $themePath . DIRECTORY_SEPARATOR . 'bootstrap' . DIRECTORY_SEPARATOR . 'cache'
. DIRECTORY_SEPARATOR . 'packages.php';
$cache = new ManifestCache($cachePath);

$cache->write($builder->build());
} catch (\RuntimeException $e) {
$io->writeError(
"<warning>Lumberjack: {$e->getMessage()} Package auto-discovery won't work as expected.</warning>"
);
}
}

/**
* Resolve the theme directory for discovery.
*
* @param string $projectPath
* @param array $extra
* @return string
* @throws \RuntimeException
*/
protected function resolveThemeDirectory(string $projectPath, array $extra): string
{
$themeDir = Arr::get($extra, 'lumberjack.theme-dir');

if ($themeDir) {
$path = $projectPath . DIRECTORY_SEPARATOR . $themeDir;

if (is_dir($path)) {
return $path;
}

throw new \RuntimeException("The configured theme directory \"{$path}\" does not exist.");
}

$defaultPath = $projectPath . DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . 'app'
. DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . 'lumberjack';

if (is_dir($defaultPath)) {
return $defaultPath;
}

throw new \RuntimeException(
'"extra.lumberjack.theme-dir" is not set in composer.json and the default path was not found.'
);
}
}
46 changes: 46 additions & 0 deletions src/Autodiscovery/ManifestCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Rareloop\Lumberjack\Autodiscovery;

class ManifestCache
{
public function __construct(protected string $cachePath)
{
}

public function exists(): bool
{
return file_exists($this->cachePath);
}

public function read(): array
{
$data = require $this->cachePath;

return is_array($data) ? $data : [];
}

public function write(array $manifest): void
{
$directory = dirname($this->cachePath);

if (!is_dir($directory)) {
mkdir($directory, 0755, true);
}

file_put_contents(
$this->cachePath,
'<?php return ' . var_export($manifest, true) . ';'
);
}

public function mtime(): int
{
return $this->exists() ? filemtime($this->cachePath) : 0;
}

public function getPath(): string
{
return $this->cachePath;
}
}
123 changes: 123 additions & 0 deletions src/Autodiscovery/PackageManifest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace Rareloop\Lumberjack\Autodiscovery;

use Illuminate\Support\Arr;

class PackageManifest
{
public function __construct(
protected string $basePath,
protected string $vendorPath
) {
}

/**
* Build the manifest of autodiscovered packages.
*
* @return array
*/
public function build(): array
{
$packages = $this->getInstalledPackages();
$ignore = $this->getPackagesToIgnore();

$providers = [];
$aliases = [];

foreach ($packages as $package) {
$name = Arr::get($package, 'name');

if ($this->shouldIgnore($name, $ignore)) {
continue;
}

$extra = Arr::get($package, 'extra.lumberjack', []);

if (!is_array($extra) || empty($extra)) {
continue;
}

$packageProviders = Arr::get($extra, 'providers', []);
$packageAliases = Arr::get($extra, 'aliases', []);

if (is_array($packageProviders)) {
foreach ($packageProviders as $provider) {
$providers[] = $this->formatClassName($provider);
}
}

if (is_array($packageAliases)) {
foreach ($packageAliases as $alias => $className) {
$aliases[$alias] = $this->formatClassName($className);
}
}
}

return [
'providers' => array_values(array_unique($providers)),
'aliases' => $aliases,
];
}

/**
* Format a class name, stripping accidental '::class' suffixes.
*
* @param mixed $className
* @return string
*/
protected function formatClassName(mixed $className): string
{
return str_replace('::class', '', (string) $className);
}

public function mtime(): int
{
$path = $this->vendorPath . DIRECTORY_SEPARATOR . 'composer' . DIRECTORY_SEPARATOR . 'installed.json';

return file_exists($path) ? filemtime($path) : 0;
}

protected function getInstalledPackages(): array
{
$path = $this->vendorPath . DIRECTORY_SEPARATOR . 'composer' . DIRECTORY_SEPARATOR . 'installed.json';

if (!file_exists($path)) {
return [];
}

$installed = json_decode(file_get_contents($path), true);

if (!is_array($installed)) {
return [];
}

$packages = $installed['packages'] ?? $installed;

return is_array($packages) ? $packages : [];
}

protected function getPackagesToIgnore(): array
{
$path = $this->basePath . DIRECTORY_SEPARATOR . 'composer.json';

if (!file_exists($path)) {
return [];
}

$composer = json_decode(file_get_contents($path), true);

if (!is_array($composer)) {
return [];
}

$ignore = Arr::get($composer, 'extra.lumberjack.dont-discover', []);

return is_array($ignore) ? $ignore : [];
}

protected function shouldIgnore(?string $name, array $ignore): bool
{
return $name !== null && (in_array($name, $ignore) || in_array('*', $ignore));
}
}
9 changes: 8 additions & 1 deletion src/Bootstrappers/RegisterAliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
namespace Rareloop\Lumberjack\Bootstrappers;

use Rareloop\Lumberjack\Application;
use Rareloop\Lumberjack\Autodiscovery\AutodiscoveredPackages;

class RegisterAliases
{
public function bootstrap(Application $app)
{
$config = $app->get('config');
$manifest = $app->get(AutodiscoveredPackages::class);

foreach ($config->get('app.aliases', []) as $alias => $realClassname) {
$aliases = array_merge(
$manifest->aliases(),
$config->get('app.aliases', [])
);

foreach ($aliases as $alias => $realClassname) {
class_alias($realClassname, $alias);
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/Bootstrappers/RegisterProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Rareloop\Lumberjack\Bootstrappers;

use Rareloop\Lumberjack\Application;
use Rareloop\Lumberjack\Autodiscovery\AutodiscoveredPackages;
use Rareloop\Lumberjack\Providers\LogServiceProvider;
use Rareloop\Lumberjack\Providers\IgnitionServiceProvider;
use Illuminate\Support\Collection;

class RegisterProviders
{
Expand All @@ -14,7 +16,15 @@ public function bootstrap(Application $app)

$this->registerBaseProviders($app);

$providers = $config->get('app.providers', []);
$manifest = $app->get(AutodiscoveredPackages::class);

$providers = Collection::make($manifest->providers())
->concat($config->get('app.providers', []))
->mapWithKeys(function ($provider) {
return [
(is_string($provider) ? $provider : $provider::class) => $provider,
];
});

foreach ($providers as $provider) {
$app->register($provider);
Expand Down
Loading