Skip to content

Commit

Permalink
Addons
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Thompson committed Jan 26, 2021
1 parent 2e8235f commit 6d4a519
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 70 deletions.
1 change: 1 addition & 0 deletions composer.json
@@ -1,5 +1,6 @@
{
"name": "streams/core",
"type": "streams-addon",
"description": "A domain-driven, code-configured, flat-file application platform for Laravel.",
"authors": [
{
Expand Down
25 changes: 1 addition & 24 deletions src/Addon/Addon.php
Expand Up @@ -21,9 +21,8 @@
class Addon implements Arrayable, Jsonable
{

use Macroable;
use HasMemory;
use Prototype;
use HasMemory;
use FiresCallbacks;

/**
Expand All @@ -46,26 +45,4 @@ public function toJson($options = 0)
{
return json_encode($this->toArray(), $options);
}

/**
* Dynamically retrieve attributes.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->getPrototypeAttribute($key);
}

/**
* Dynamically set attributes.
*
* @param string $key
* @param mixed $value
*/
public function __set($key, $value)
{
$this->setPrototypeAttribute($key, $value);
}
}
17 changes: 9 additions & 8 deletions src/Addon/AddonCollection.php
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Streams\Core\Addon\Addon;
use Illuminate\Support\Collection;

/**
Expand All @@ -25,8 +26,8 @@ class AddonCollection extends Collection
public function core()
{
return $this->filter(
function (array $addon) {
return is_dir(base_path('vendor/' . $addon['name']));
function (Addon $addon) {
return Str::startsWith($addon->path, base_path('vendor'));
}
);
}
Expand All @@ -39,8 +40,8 @@ function (array $addon) {
public function nonCore()
{
return $this->filter(
function (array $addon) {
return !is_dir(base_path('vendor/' . $addon['name']));
function (Addon $addon) {
return !Str::startsWith($addon->path, base_path('vendor'));
}
);
}
Expand All @@ -53,8 +54,8 @@ function (array $addon) {
public function enabled()
{
return $this->filter(
function (array $addon) {
return Arr::get($addon, 'enabled');
function (Addon $addon) {
return $addon->enabled;
}
);
}
Expand All @@ -67,8 +68,8 @@ function (array $addon) {
public function disabled()
{
return $this->filter(
function (array $addon) {
return Arr::get($addon, 'enabled') == false;
function (Addon $addon) {
return !$addon->enabled;
}
);
}
Expand Down
101 changes: 101 additions & 0 deletions src/Addon/AddonManager.php
@@ -0,0 +1,101 @@
<?php

namespace Streams\Core\Addon;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;

/**
* Class AddonManager
*
* @link http://pyrocms.com/
* @author PyroCMS, Inc. <support@pyrocms.com>
* @author Ryan Thompson <ryan@pyrocms.com>
*/
class AddonManager
{
/**
* The addon collection.
*
* @var Collection
*/
protected $collection;

/**
* The disabled addons.
*
* @var array
*/
protected $disabled;

/**
* Create a new class instance.
*/
public function __construct()
{
$this->collection = new AddonCollection;
$this->disabled = Config::get('streams.addons.disabled', []);;
}

/**
* Load an addon by path.
*
* @param string $path
*/
public function load($path)
{
$path = rtrim($path, '/\\');

$composer = json_decode(file_get_contents($path . '/composer.json'), true);

$addon = [
'name' => $composer['name'],
'path' => $path,
'composer' => $composer,
];

return $this->register($addon);
}

/**
* Register an addon.
*/
public function register($addon)
{
$addon['enabled'] = Arr::get($addon, 'enabled', !in_array($addon['name'], $this->disabled));

$addon = $this->build($addon);

App::instance('streams.addons.' . str_replace('/', '.', $addon->name), $addon);

$this->collection->put($addon->name, $addon);

return $addon;
}

public function make($name)
{
return App::make('streams.addons.' . str_replace('/', '.', $name));
}

/**
* Build an addon instance.
*
* @param $addon
* @return Addon
*/
public function build($addon)
{
return new Addon($addon);
}

/**
* Return the addon collection.
*/
public function collection()
{
return $this->collection;
}
}
57 changes: 19 additions & 38 deletions src/StreamsServiceProvider.php
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\View\Factory;
use Streams\Core\Addon\Addon;
use Collective\Html\HtmlFacade;
use Streams\Core\Stream\Stream;
use Illuminate\Support\Collection;
Expand All @@ -23,14 +22,13 @@
use Illuminate\Translation\Translator;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\ServiceProvider;
use Streams\Core\Addon\AddonCollection;
use Streams\Core\Support\Facades\Addons;
use Streams\Core\Support\Facades\Assets;
use Streams\Core\Support\Facades\Images;
use Streams\Core\Application\Application;
use Streams\Core\Support\Facades\Streams;
use Streams\Core\Support\Facades\Hydrator;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request as RequestObject;
use Illuminate\Support\Collection as SupportCollection;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;

Expand Down Expand Up @@ -58,26 +56,17 @@ class StreamsServiceProvider extends ServiceProvider
'Application' => \Streams\Core\Support\Facades\Application::class,
];

/**
* The class bindings.
*
* @var array
*/
public $bindings = [
'streams.addons' => \Streams\Core\Addon\AddonCollection::class,
];

/**
* The singleton bindings.
*
* @var array
*/
public $singletons = [
'addons' => \Streams\Core\Addon\AddonManager::class,
'assets' => \Streams\Core\Asset\AssetManager::class,
'images' => \Streams\Core\Image\ImageManager::class,
'includes' => \Streams\Core\View\ViewIncludes::class,
'streams' => \Streams\Core\Stream\StreamManager::class,
ViewOverrides::class => \Streams\Core\View\ViewOverrides::class,
'messages' => \Streams\Core\Message\MessageManager::class,
'applications' => \Streams\Core\Application\ApplicationManager::class,

Expand All @@ -87,6 +76,8 @@ class StreamsServiceProvider extends ServiceProvider
'decorator' => \Streams\Core\Support\Decorator::class,
'evaluator' => \Streams\Core\Support\Evaluator::class,
'transformer' => \Streams\Core\Support\Transformer::class,

ViewOverrides::class => \Streams\Core\View\ViewOverrides::class,
];

/**
Expand Down Expand Up @@ -196,7 +187,7 @@ function ($parameter) {
});

// Setup and preparing utilities.
$this->registerAddonCollection();
$this->registerAddons();
$this->configureFileCacheStore();
$this->addAssetNamespaces();
$this->addImageNamespaces();
Expand Down Expand Up @@ -350,7 +341,7 @@ protected function registerFieldTypes()
$this->app->bind('streams.field_types.hash', \Streams\Core\Field\Type\Hash::class);
$this->app->bind('streams.field_types.slug', \Streams\Core\Field\Type\Slug::class);
$this->app->bind('streams.field_types.email', \Streams\Core\Field\Type\Email::class);

$this->app->bind('streams.field_types.markdown', \Streams\Core\Field\Type\Markdown::class);
$this->app->bind('streams.field_types.template', \Streams\Core\Field\Type\Template::class);

Expand Down Expand Up @@ -511,34 +502,24 @@ protected function registerStreams()
}

/**
* Register addon collections.
* Register addons.
*/
protected function registerAddonCollection()
protected function registerAddons()
{
$this->app->singleton(AddonCollection::class, function () {
$lock = json_decode(file_get_contents(base_path('composer.lock')), true);

$disabled = Config::get('streams.addons.disabled');

$lock = json_decode(file_get_contents(base_path('composer.lock')), true);

$addons = array_filter(
array_merge($lock['packages'], $lock['packages-dev']),
function (array $package) {
return Arr::get($package, 'type') == 'streams-addon';
}
);

ksort($addons);

$addons = array_map(function ($addon) use ($disabled) {

$addon['enabled'] = in_array($addon['name'], $disabled);
$addons = array_filter(
array_merge($lock['packages'], $lock['packages-dev']),
function (array $package) {
return Arr::get($package, 'type') == 'streams-addon';
}
);

return new Addon($addon);
}, $addons);
ksort($addons);

return new AddonCollection($addons);
});
$addons = array_map(function ($addon) {
$addon = Addons::load(base_path('vendor/' . $addon['name']));
}, $addons);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/Support/Facades/Addons.php
@@ -0,0 +1,28 @@
<?php

namespace Streams\Core\Support\Facades;

use Illuminate\Support\Facades\Facade;

/**
* Class Addons
*
* @link http://pyrocms.com/
* @author PyroCMS, Inc. <support@pyrocms.com>
* @author Ryan Thompson <ryan@pyrocms.com>
* @method static void register($addon)
*/
class Addons extends Facade
{

/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'addons';
}
}

0 comments on commit 6d4a519

Please sign in to comment.