Skip to content

Commit

Permalink
Merge branch 'roots:main' into fse
Browse files Browse the repository at this point in the history
  • Loading branch information
dsturm authored Dec 11, 2023
2 parents c795a29 + 0b3c66e commit efaea0f
Show file tree
Hide file tree
Showing 19 changed files with 268 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
Acorn is an open source project and completely free to use. If you've benefited from our projects and would like to support our future endeavors, please consider [sponsoring Roots](https://github.com/sponsors/roots).

<div align="center">
<a href="https://k-m.com/"><img src="https://cdn.roots.io/app/uploads/km-digital.svg" alt="KM Digital" width="120" height="90"></a> <a href="https://carrot.com/"><img src="https://cdn.roots.io/app/uploads/carrot.svg" alt="Carrot" width="120" height="90"></a> <a href="https://wordpress.com/"><img src="https://cdn.roots.io/app/uploads/wordpress.svg" alt="WordPress.com" width="120" height="90"></a> <a href="https://worksitesafety.ca/careers/"><img src="https://cdn.roots.io/app/uploads/worksite-safety.svg" alt="Worksite Safety" width="120" height="90"></a> <a href="https://www.copiadigital.com/"><img src="https://cdn.roots.io/app/uploads/copia-digital.svg" alt="Copia Digital" width="120" height="90"></a> <a href="https://www.freave.com/"><img src="https://cdn.roots.io/app/uploads/freave.svg" alt="Freave" width="120" height="90"></a>
<a href="https://k-m.com/"><img src="https://cdn.roots.io/app/uploads/km-digital.svg" alt="KM Digital" width="120" height="90"></a> <a href="https://carrot.com/"><img src="https://cdn.roots.io/app/uploads/carrot.svg" alt="Carrot" width="120" height="90"></a> <a href="https://wordpress.com/"><img src="https://cdn.roots.io/app/uploads/wordpress.svg" alt="WordPress.com" width="120" height="90"></a> <a href="https://worksitesafety.ca/careers/"><img src="https://cdn.roots.io/app/uploads/worksite-safety.svg" alt="Worksite Safety" width="120" height="90"></a> <a href="https://www.freave.com/"><img src="https://cdn.roots.io/app/uploads/freave.svg" alt="Freave" width="120" height="90"></a>
</div>

## Overview
Expand Down
4 changes: 2 additions & 2 deletions src/Roots/Acorn/Assets/Asset/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function base64()
*
* @param string $mediatype MIME content type
*/
public function dataUrl(string $mediatype = null): string
public function dataUrl(?string $mediatype = null): string
{
if ($this->dataUrl) {
return $this->dataUrl;
Expand All @@ -132,7 +132,7 @@ public function dataUrl(string $mediatype = null): string
*
* @param string $mediatype MIME content type
*/
public function dataUri(string $mediatype = null): string
public function dataUri(?string $mediatype = null): string
{
return $this->dataUrl($mediatype);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Roots/Acorn/Assets/Asset/TextAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function charset($fallback = 'UTF-8'): string
* @param string $charset Character encoding
* @param string $urlencode List of characters to be percent-encoded
*/
public function dataUrl(string $mediatype = null, string $charset = null, string $urlencode = '%\'"'): string
public function dataUrl(?string $mediatype = null, ?string $charset = null, string $urlencode = '%\'"'): string
{
if ($this->dataUrl) {
return $this->dataUrl;
Expand Down Expand Up @@ -72,7 +72,7 @@ public function dataUrl(string $mediatype = null, string $charset = null, string
* @param string $charset Character encoding
* @param string $urlencode List of characters to be percent-encoded
*/
public function dataUri(string $mediatype = null, string $charset = null, string $urlencode = '%\'"'): string
public function dataUri(?string $mediatype = null, ?string $charset = null, string $urlencode = '%\'"'): string
{
return $this->dataUrl($mediatype, $charset, $urlencode);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Roots/Acorn/Assets/AssetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AssetFactory
* @param string $uri Remote URI
* @param string $type Asset type
*/
public static function create(string $path, string $uri, string $type = null): AssetContract
public static function create(string $path, string $uri, ?string $type = null): AssetContract
{
if (! $type) {
$type = pathinfo($path, PATHINFO_EXTENSION);
Expand Down
85 changes: 66 additions & 19 deletions src/Roots/Acorn/Assets/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,48 @@

class Bundle implements BundleContract
{
use Conditional;
use Enqueuable;
use Conditional, Enqueuable;

/**
* The bundle ID.
*
* @var string
*/
protected $id;

/**
* The bundle path.
*
* @var string
*/
protected $path;

/**
* The bundle URI.
*
* @var string
*/
protected $uri;

/**
* The bundle runtime.
*
* @var string|null
*/
protected $runtime;

/**
* The bundle contents.
*
* @var array
*/
protected $bundle;

/**
* The bundle runtimes.
*
* @var array
*/
protected static $runtimes = [];

/**
Expand All @@ -44,7 +73,7 @@ public function __construct(string $id, array $bundle, string $path, string $uri
*
* @return Collection|$this
*/
public function css(callable $callable = null)
public function css(?callable $callable = null)
{
$styles = $this->conditional ? $this->bundle['css'] : [];

Expand All @@ -67,7 +96,7 @@ public function css(callable $callable = null)
*
* @return Collection|$this
*/
public function js(callable $callable = null)
public function js(?callable $callable = null)
{
$scripts = $this->conditional ? array_merge($this->bundle['js'], $this->bundle['mjs']) : [];

Expand Down Expand Up @@ -125,10 +154,9 @@ public function runtimeSource()
/**
* Get the bundle URL.
*
* @param string $path
* @return string
*/
protected function getUrl($path)
protected function getUrl(string $path)
{
if (parse_url($path, PHP_URL_HOST)) {
return $path;
Expand All @@ -148,20 +176,39 @@ protected function getUrl($path)
protected function setRuntime()
{
if (Arr::isAssoc($this->bundle['js'])) {
$this->runtime = $this->bundle['js']['runtime'] ?? $this->bundle['js']["runtime~{$this->id}"] ?? null;
$this->runtime = $this->bundle['js']['runtime']
?? $this->bundle['js']["runtime~{$this->id}"]
?? null;

unset($this->bundle['js']['runtime'], $this->bundle['js']["runtime~{$this->id}"]);
} elseif (isset($this->bundle['js'][0]) && strpos($this->bundle['js'][0], 'runtime') === 0) {
$this->runtime = $this->bundle['js'][0];
unset($this->bundle['js'][0]);
} elseif (isset($this->bundle['js'][0]) && strpos($this->bundle['js'][0], 'js/runtime') === 0) {
$this->runtime = $this->bundle['js'][0];
unset($this->bundle['js'][0]);
} elseif (isset($this->bundle['mjs'][0]) && strpos($this->bundle['mjs'][0], 'runtime') === 0) {
$this->runtime = $this->bundle['mjs'][0];
unset($this->bundle['mjs'][0]);
} elseif (isset($this->bundle['mjs'][0]) && strpos($this->bundle['mjs'][0], 'js/runtime') === 0) {
$this->runtime = $this->bundle['mjs'][0];
unset($this->bundle['mjs'][0]);

return;
}

$this->runtime = $this->getBundleRuntime() ?? $this->getBundleRuntime('mjs');
}

/**
* Retrieve the runtime in a bundle.
*
* @return string|null
*/
protected function getBundleRuntime(string $type = 'js')
{
if (! $this->bundle[$type]) {
return null;
}

foreach ($this->bundle[$type] as $key => $value) {
if (! str_contains($value, 'runtime')) {
continue;
}

unset($this->bundle[$type][$key]);

return $value;
}

return null;
}
}
4 changes: 2 additions & 2 deletions src/Roots/Acorn/Assets/Concerns/Enqueuable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait Enqueuable
*
* @return Collection|$this
*/
abstract public function js(callable $callable = null);
abstract public function js(?callable $callable = null);

/**
* Get CSS files in bundle.
Expand All @@ -27,7 +27,7 @@ abstract public function js(callable $callable = null);
*
* @return Collection|$this
*/
abstract public function css(callable $callable = null);
abstract public function css(?callable $callable = null);

abstract public function runtime();

Expand Down
2 changes: 1 addition & 1 deletion src/Roots/Acorn/Assets/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function register(string $name, ManifestContract $manifest): self
/**
* Get a Manifest
*/
public function manifest(string $name, array $config = null): ManifestContract
public function manifest(string $name, ?array $config = null): ManifestContract
{
$manifest = $this->manifests[$name] ?? $this->resolve($name, $config);

Expand Down
2 changes: 1 addition & 1 deletion src/Roots/Acorn/Assets/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Manifest implements ManifestContract

protected $uri;

public function __construct(string $path, string $uri, array $assets = [], array $bundles = null)
public function __construct(string $path, string $uri, array $assets = [], ?array $bundles = null)
{
$this->path = $path;
$this->uri = $uri;
Expand Down
2 changes: 1 addition & 1 deletion src/Roots/Acorn/Assets/Middleware/RootsBudMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RootsBudMiddleware
*/
protected $dev_origin;

public function __construct(string $dev_origin = null)
public function __construct(?string $dev_origin = null)
{
$this->dev_origin = $dev_origin;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Roots/Acorn/Bootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public static function setInstance(?self $bootloader)
*
* @return static
*/
public static function getInstance(ApplicationContract $app = null)
public static function getInstance(?ApplicationContract $app = null)
{
return static::$instance ??= new static($app);
}

/**
* Create a new bootloader instance.
*/
public function __construct(ApplicationContract $app = null, Filesystem $files = null)
public function __construct(?ApplicationContract $app = null, ?Filesystem $files = null)
{
$this->app = $app;
$this->files = $files ?? new Filesystem;
Expand Down
2 changes: 1 addition & 1 deletion src/Roots/Acorn/DefaultProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DefaultProviders extends DefaultProvidersBase
*
* @return void
*/
public function __construct(array $providers = null)
public function __construct(?array $providers = null)
{
parent::__construct($providers);

Expand Down
2 changes: 1 addition & 1 deletion src/Roots/Acorn/Exceptions/SkipProviderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SkipProviderException extends InvalidArgumentException
*
* @return void
*/
public function __construct(string $message = '', int $code = 0, Throwable $previous = null, string $package = '')
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null, string $package = '')
{
parent::__construct($message, $code, $previous);

Expand Down
72 changes: 63 additions & 9 deletions src/Roots/Acorn/View/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,56 @@
use Illuminate\Support\Fluent;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Roots\Acorn\View\Composers\Concerns\Extractable;

abstract class Composer
{
use Extractable;

/**
* List of views to receive data by this composer
* The list of views served by this composer.
*
* @var string[]
*/
protected static $views;

/**
* Current view
* The current view instance.
*
* @var View
* @var \Illuminate\View\View
*/
protected $view;

/**
* Current view data
* The current view data.
*
* @var Fluent
* @var \Illuminate\Support\Fluent
*/
protected $data;

/**
* List of views served by this composer
* The properties / methods that should not be exposed.
*
* @var array
*/
protected $except = [];

/**
* The default properties / methods that should not be exposed.
*
* @var array
*/
protected $defaultExcept = [
'cache',
'compose',
'override',
'toArray',
'views',
'with',
];

/**
* The list of views served by this composer.
*
* @return string|string[]
*/
Expand Down Expand Up @@ -60,12 +84,20 @@ public function compose(View $view)
}

/**
* Data to be merged and passed to the view before rendering.
* The merged data to be passed to view before rendering.
*
* @return array
*/
protected function merge()
{
if (! $this->with() && ! $this->override()) {
return array_merge(
$this->extractPublicProperties(),
$this->extractPublicMethods(),
$this->view->getData()
);
}

return array_merge(
$this->with(),
$this->view->getData(),
Expand All @@ -74,7 +106,7 @@ protected function merge()
}

/**
* Data to be passed to view before rendering
* The data passed to the view before rendering.
*
* @return array
*/
Expand All @@ -84,12 +116,34 @@ protected function with()
}

/**
* Data to be passed to view before rendering
* The override data passed to the view before rendering.
*
* @return array
*/
protected function override()
{
return [];
}

/**
* Determine if the given property / method should be ignored.
*
* @param string $name
* @return bool
*/
protected function shouldIgnore($name)
{
return str_starts_with($name, '__') ||
in_array($name, $this->ignoredMethods());
}

/**
* Get the methods that should be ignored.
*
* @return array
*/
protected function ignoredMethods()
{
return array_merge($this->defaultExcept, $this->except);
}
}
Loading

0 comments on commit efaea0f

Please sign in to comment.