Skip to content

Commit

Permalink
Fix support for Laravel 8 (#26)
Browse files Browse the repository at this point in the history
Credit to @tschallacka 

Upgraded symfony/yaml to ^5.1
Updated requirement to php 7.4
Changed Exception to Throwable to make the methods compatible with parent class
Transport manager was changed in Mail Manager in laravel 7.
Removed backports of compileUpsert in SQL grammars
Added unit test for dispatcher on changed events that are accepted on the listen() method.
Modified Dispatcher so it accepts closures and QueuedClosures and passes handling off to the parent class
Added priority sorting for all accepted event types
Added serializable protections for emitter and extendable
added Serialisation helper for wrapping closures
Added constants to get rid of the magic strings in the traits.

Co-authored-by: Luke Towers <github@luketowers.ca>
Co-authored-by: Michael Dibbets <michael@exit-reizen.nl>
  • Loading branch information
3 people authored Aug 9, 2021
1 parent d0cb705 commit 04230d5
Show file tree
Hide file tree
Showing 24 changed files with 542 additions and 202 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
max-parallel: 6
matrix:
operatingSystem: [ubuntu-latest, windows-latest]
phpVersion: ['7.2', '7.3', '7.4', '8.0']
phpVersion: ['7.4', '8.0']
fail-fast: false
runs-on: ${{ matrix.operatingSystem }}
name: ${{ matrix.operatingSystem }} / PHP ${{ matrix.phpVersion }}
Expand Down
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ composer.lock

# Other files
.DS_Store
php_errors.log
php_errors.log

#eclipse
/.buildpath
/.project
/.settings/

#phpunit
tests/.phpunit.result.cache
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
],
"require": {
"php": ">=7.2.9",
"php": ">=7.4",
"ext-ctype": "*",
"ext-curl": "*",
"ext-dom": "*",
Expand All @@ -39,12 +39,12 @@
"linkorb/jsmin-php": "~1.0",
"wikimedia/less.php": "~3.0",
"scssphp/scssphp": "~1.0",
"symfony/yaml": "^4.4",
"symfony/yaml": "^5.1",
"twig/twig": "~2.0",
"league/csv": "~9.1",
"nesbot/carbon": "^2.0",
"laravel/framework": "~6.0",
"laravel/tinker": "~2.0"
"laravel/framework": "9.x-dev",
"laravel/tinker": "dev-develop"
},
"require-dev": {
"phpunit/phpunit": "^8.5.12|^9.3.3",
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Pivot.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public function __construct(ModelBase $parent, $attributes, $table, $exists = fa
/**
* Set the keys for a save update query.
*
* @param \Illuminate\Database\Eloquent\Builder
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function setKeysForSaveQuery(BuilderBase $query)
protected function setKeysForSaveQuery($query)
{
$query->where($this->foreignKey, $this->getAttribute($this->foreignKey));

Expand Down
23 changes: 0 additions & 23 deletions src/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
<?php namespace Winter\Storm\Database\Query\Grammars;

use Winter\Storm\Database\QueryBuilder;
use Illuminate\Database\Query\Grammars\MySqlGrammar as BaseMysqlGrammer;
use Winter\Storm\Database\Query\Grammars\Concerns\SelectConcatenations;

class MySqlGrammar extends BaseMysqlGrammer
{
use SelectConcatenations;

/**
* Compile an "upsert" statement into SQL.
*
* @param \Winter\Storm\Database\QueryBuilder $query
* @param array $values
* @param array $uniqueBy
* @param array $update
* @return string
*/
public function compileUpsert(QueryBuilder $query, array $values, array $uniqueBy, array $update)
{
$sql = $this->compileInsert($query, $values) . ' on duplicate key update ';

$columns = collect($update)->map(function ($value, $key) {
return is_numeric($key)
? $this->wrap($value) . ' = values(' . $this->wrap($value) . ')'
: $this->wrap($key) . ' = ' . $this->parameter($value);
})->implode(', ');

return $sql . $columns;
}
}
25 changes: 0 additions & 25 deletions src/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
<?php namespace Winter\Storm\Database\Query\Grammars;

use Winter\Storm\Database\QueryBuilder;
use Illuminate\Database\Query\Grammars\PostgresGrammar as BasePostgresGrammer;
use Winter\Storm\Database\Query\Grammars\Concerns\SelectConcatenations;

class PostgresGrammar extends BasePostgresGrammer
{
use SelectConcatenations;

/**
* Compile an "upsert" statement into SQL.
*
* @param \Winter\Storm\Database\QueryBuilder $query
* @param array $values
* @param array $uniqueBy
* @param array $update
* @return string
*/
public function compileUpsert(QueryBuilder $query, array $values, array $uniqueBy, array $update)
{
$sql = $this->compileInsert($query, $values);

$sql .= ' on conflict (' . $this->columnize($uniqueBy) . ') do update set ';

$columns = collect($update)->map(function ($value, $key) {
return is_numeric($key)
? $this->wrap($value) . ' = ' . $this->wrapValue('excluded') . '.' . $this->wrap($value)
: $this->wrap($key) . ' = ' . $this->parameter($value);
})->implode(', ');

return $sql . $columns;
}
}
25 changes: 0 additions & 25 deletions src/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php namespace Winter\Storm\Database\Query\Grammars;

use Winter\Storm\Database\QueryBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\Grammars\SQLiteGrammar as BaseSQLiteGrammar;
use Winter\Storm\Database\Query\Grammars\Concerns\SelectConcatenations;
Expand Down Expand Up @@ -32,28 +31,4 @@ protected function compileConcat(array $parts, string $as)

return implode(' || ', $compileParts) . ' as ' . $this->wrap($as);
}

/**
* Compile an "upsert" statement into SQL.
*
* @param \Winter\Storm\Database\QueryBuilder $query
* @param array $values
* @param array $uniqueBy
* @param array $update
* @return string
*/
public function compileUpsert(QueryBuilder $query, array $values, array $uniqueBy, array $update)
{
$sql = $this->compileInsert($query, $values);

$sql .= ' on conflict (' . $this->columnize($uniqueBy) . ') do update set ';

$columns = collect($update)->map(function ($value, $key) {
return is_numeric($key)
? $this->wrap($value) . ' = ' . $this->wrapValue('excluded') . '.' . $this->wrap($value)
: $this->wrap($key) . ' = ' . $this->parameter($value);
})->implode(', ');

return $sql . $columns;
}
}
43 changes: 0 additions & 43 deletions src/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,9 @@
<?php namespace Winter\Storm\Database\Query\Grammars;

use Winter\Storm\Database\QueryBuilder;
use Illuminate\Database\Query\Grammars\SqlServerGrammar as BaseSqlServerGrammar;
use Winter\Storm\Database\Query\Grammars\Concerns\SelectConcatenations;

class SqlServerGrammar extends BaseSqlServerGrammar
{
use SelectConcatenations;

/**
* Compile an "upsert" statement into SQL.
*
* @param \Winter\Storm\Database\QueryBuilder $query
* @param array $values
* @param array $uniqueBy
* @param array $update
* @return string
*/
public function compileUpsert(QueryBuilder $query, array $values, array $uniqueBy, array $update)
{
$columns = $this->columnize(array_keys(reset($values)));

$sql = 'merge ' . $this->wrapTable($query->from) . ' ';

$parameters = collect($values)->map(function ($record) {
return '(' . $this->parameterize($record) . ')';
})->implode(', ');

$sql .= 'using (values ' . $parameters . ') ' . $this->wrapTable('laravel_source') . ' (' . $columns . ') ';

$on = collect($uniqueBy)->map(function ($column) use ($query) {
return $this->wrap('laravel_source.' . $column) . ' = ' . $this->wrap($query->from . '.' . $column);
})->implode(' and ');

$sql .= 'on ' . $on . ' ';

if ($update) {
$update = collect($update)->map(function ($value, $key) {
return is_numeric($key)
? $this->wrap($value) . ' = ' . $this->wrap('laravel_source.' . $value)
: $this->wrap($key) . ' = ' . $this->parameter($value);
})->implode(', ');

$sql .= 'when matched then update set ' . $update . ' ';
}

$sql .= 'when not matched then insert (' . $columns . ') values (' . $columns . ')';

return $sql;
}
}
6 changes: 3 additions & 3 deletions src/Database/Traits/Encryptable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait Encryptable
/**
* @var \Illuminate\Contracts\Encryption\Encrypter Encrypter instance.
*/
protected $encrypter;
protected $encrypterInstance;

/**
* @var array List of original attribute values before they were encrypted.
Expand Down Expand Up @@ -108,7 +108,7 @@ public function getOriginalEncryptableValue($attribute)
*/
public function getEncrypter()
{
return (!is_null($this->encrypter)) ? $this->encrypter : App::make('encrypter');
return (!is_null($this->encrypterInstance)) ? $this->encrypterInstance : App::make('encrypter');
}

/**
Expand All @@ -119,6 +119,6 @@ public function getEncrypter()
*/
public function setEncrypter(\Illuminate\Contracts\Encryption\Encrypter $encrypter)
{
$this->encrypter = $encrypter;
$this->encrypterInstance = $encrypter;
}
}
32 changes: 27 additions & 5 deletions src/Events/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php namespace Winter\Storm\Events;

use Closure;
use Opis\Closure\SerializableClosure;
use ReflectionClass;
use Winter\Storm\Support\Arr;
use Winter\Storm\Support\Serialisation;
use Winter\Storm\Support\Str;
use Illuminate\Events\Dispatcher as BaseDispatcher;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Events\QueuedClosure;

class Dispatcher extends BaseDispatcher
{
Expand All @@ -25,13 +29,28 @@ class Dispatcher extends BaseDispatcher
/**
* Register an event listener with the dispatcher.
*
* @param string|array $events
* @param mixed $listener
* @param int $priority
* @param string|array|Closure|QueuedClosure $events
* @param mixed $listener when the third parameter is omitted and a Closure or QueuedClosure is provided
* this parameter is used as an integer this is used as priority value
* @param int $priority
* @return void
*/
public function listen($events, $listener, $priority = 0)
public function listen($events, $listener = null, $priority = 0)
{
if ($events instanceof Closure || $events instanceof QueuedClosure) {
if ($priority === 0 && (is_int($listener) || filter_var($listener, FILTER_VALIDATE_INT))) {
$priority = (int)$listener;
}
}
if ($events instanceof Closure) {
return $this->listen($this->firstClosureParameterType($events), $events, $priority);
} elseif ($events instanceof QueuedClosure) {
return $this->listen($this->firstClosureParameterType($events->closure), $events->resolve(), $priority);
} elseif ($listener instanceof QueuedClosure) {
$listener = $listener->resolve();
}
$listener = Serialisation::wrapClosure($listener);

foreach ((array) $events as $event) {
if (Str::contains($event, '*')) {
$this->setupWildcardListen($event, $listener);
Expand Down Expand Up @@ -109,6 +128,9 @@ public function dispatch($event, $payload = [], $halt = false)
}

foreach ($this->getListeners($event) as $listener) {
if ($listener instanceof SerializableClosure) {
$listener = $listener->getClosure();
}
$response = $listener($event, $payload);

// If a response is returned from the listener and event halting is enabled
Expand Down Expand Up @@ -171,7 +193,7 @@ protected function sortListeners($eventName)

// If listeners exist for the given event, we will sort them by the priority
// so that we can call them in the correct order. We will cache off these
// sorted event listeners so we do not have to re-sort on every events.
// sorted event listeners so we do not have to re-sort on every event.
if (isset($this->listeners[$eventName])) {
krsort($this->listeners[$eventName]);

Expand Down
12 changes: 6 additions & 6 deletions src/Extension/ExtendableTrait.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php namespace Winter\Storm\Extension;

use Opis\Closure\SerializableClosure;
use ReflectionClass;
use ReflectionMethod;
use BadMethodCallException;
use Exception;
use Winter\Storm\Support\Serialisation;

/**
* This extension trait is used when access to the underlying base class
Expand Down Expand Up @@ -56,7 +58,7 @@ public function extendableConstruct()
foreach ($classes as $class) {
if (isset(self::$extendableCallbacks[$class]) && is_array(self::$extendableCallbacks[$class])) {
foreach (self::$extendableCallbacks[$class] as $callback) {
call_user_func($callback, $this);
call_user_func(Serialisation::unwrapClosure($callback), $this);
}
}
}
Expand Down Expand Up @@ -109,8 +111,7 @@ public static function extendableExtendCallback($callback)
) {
self::$extendableCallbacks[$class] = [];
}

self::$extendableCallbacks[$class][] = $callback;
self::$extendableCallbacks[$class][] = Serialisation::wrapClosure($callback);
}

/**
Expand Down Expand Up @@ -166,8 +167,7 @@ public function addDynamicMethod($dynamicName, $method, $extension = null)
) {
$method = [$extensionObj, $method];
}

$this->extensionData['dynamicMethods'][$dynamicName] = $method;
$this->extensionData['dynamicMethods'][$dynamicName] = Serialisation::wrapClosure($method);
}

/**
Expand Down Expand Up @@ -418,7 +418,7 @@ public function extendableCall($name, $params = null)
$dynamicCallable = $this->extensionData['dynamicMethods'][$name];

if (is_callable($dynamicCallable)) {
return call_user_func_array($dynamicCallable, array_values($params));
return call_user_func_array(Serialisation::unwrapClosure($dynamicCallable), array_values($params));
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/Extension/ExtensionTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php namespace Winter\Storm\Extension;

use Opis\Closure\SerializableClosure;
use Winter\Storm\Support\Serialisation;

/**
* Extension trait
*
Expand Down Expand Up @@ -34,7 +37,7 @@ public function extensionApplyInitCallbacks()
foreach ($classes as $class) {
if (isset(self::$extensionCallbacks[$class]) && is_array(self::$extensionCallbacks[$class])) {
foreach (self::$extensionCallbacks[$class] as $callback) {
call_user_func($callback, $this);
call_user_func(Serialisation::unwrapClosure($callback), $this);
}
}
}
Expand All @@ -54,8 +57,7 @@ public static function extensionExtendCallback($callback)
) {
self::$extensionCallbacks[$class] = [];
}

self::$extensionCallbacks[$class][] = $callback;
self::$extensionCallbacks[$class][] = Serialisation::wrapClosure($callback);
}

protected function extensionHideField($name)
Expand Down
Loading

0 comments on commit 04230d5

Please sign in to comment.