Skip to content

Commit

Permalink
Merge pull request #262 from bowphp/fix-many-issues
Browse files Browse the repository at this point in the history
Fix many issues
  • Loading branch information
papac authored Sep 22, 2023
2 parents 7f55818 + d38f749 commit c4220ad
Show file tree
Hide file tree
Showing 31 changed files with 720 additions and 159 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- run: docker run -p 1080:1080 -p 1025:1025 -d --name maildev soulteary/maildev
- run: docker run -p 6379:6379 -d --name redis redis
- run: docker run -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -e POSTGRES_PASSWORD=postgres -d postgis/postgis
- run: docker run -d -p 11301:11300 schickling/beanstalkd
- run: docker run -d -p 11300:11300 schickling/beanstalkd

- name: Cache Composer packages
id: composer-cache
Expand Down
9 changes: 8 additions & 1 deletion src/Auth/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Bow\Auth\Guards\SessionGuard;
use Bow\Auth\Guards\GuardContract;
use Bow\Auth\Exception\AuthenticationException;
use ErrorException;

class Auth
{
Expand Down Expand Up @@ -100,10 +101,16 @@ public static function guard(?string $guard = null): GuardContract
*
* @param string $method
* @param array $params
* @return GuardContract
* @return ?GuardContract
*/
public static function __callStatic(string $method, array $params)
{
if (is_null(static::$instance)) {
throw new ErrorException(
"Unable to get auth instance before configuration"
);
}

if (method_exists(static::$instance, $method)) {
return call_user_func_array([static::$instance, $method], $params);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Auth/Guards/JwtGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ public function attempts(array $credentials): bool
*/
public function check(): bool
{
$policier = $this->getPolicier();

if (is_null($this->token)) {
try {
$this->token = $policier->getParsedToken();
} catch (\Exception $e) {
return false;
}
}

if (is_null($this->token)) {
return false;
}
Expand Down
11 changes: 10 additions & 1 deletion src/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,22 @@ public static function addAdapters(array $adapters): void
* @param array $arguments
* @return mixed
* @throws BadMethodCallException
* @throws ErrorException
*/
public static function __callStatic(string $name, array $arguments)
{
if (is_null(static::$instance)) {
throw new ErrorException(
"Unable to get cache instance before configuration"
);
}

if (method_exists(static::$instance, $name)) {
return call_user_func_array([static::$instance, $name], $arguments);
}

throw new BadMethodCallException("The $name method does not exist");
throw new BadMethodCallException(
"The $name method does not exist"
);
}
}
5 changes: 5 additions & 0 deletions src/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class Command extends AbstractCommand
"server" => \Bow\Console\Command\ServerCommand::class,
"worker" => \Bow\Console\Command\WorkerCommand::class,
],
"flush" => [
"worker" => \Bow\Console\Command\WorkerCommand::class,
],
];

/**
Expand Down Expand Up @@ -79,5 +82,7 @@ public function call(string $command, string $action, ...$rest): mixed
if (method_exists($instance, $method)) {
return call_user_func_array([$instance, $method], $rest);
}

return null;
}
}
39 changes: 36 additions & 3 deletions src/Console/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,50 @@ class WorkerCommand extends AbstractCommand
*/
public function run(?string $connection = null): void
{
$retry = (int) $this->arg->getParameter('--retry', 3);
$tries = (int) $this->arg->getParameter('--tries', 3);
$default = $this->arg->getParameter('--queue', "default");
$memory = $this->arg->getParameter('--memory', 126);
$timout = $this->arg->getParameter('--timout', 60);
$sleep = $this->arg->getParameter('--sleep', 60);

$queue = app("queue");

if (!is_null($connection)) {
$queue->setConnection($connection);
}

$worker = new WorkerService();
$worker = $this->getWorderService();
$worker->setConnection($queue->getAdapter());
$worker->run($default, $retry);
$worker->run($default, $tries, $sleep, $timout, $memory);
}

/**
* Flush the queue
*
* @param ?string $connection
* @return void
*/
public function flush(?string $connection = null)
{
$connection_queue = $this->arg->getParameter('--queue');

$queue = app("queue");

if (!is_null($connection)) {
$queue->setConnection($connection);
}

$adapter = $queue->getAdapter();
$adapter->flush($connection_queue);
}

/**
* Get the worker service
*
* @return WorkerService
*/
private function getWorderService()
{
return new WorkerService();
}
}
39 changes: 33 additions & 6 deletions src/Console/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Console
* @var array
*/
private const COMMAND = [
'add', 'migration', 'migrate', 'run', 'generate', 'gen', 'seed', 'help', 'launch', 'clear'
'add', 'migration', 'migrate', 'run', 'generate', 'gen', 'seed', 'help', 'launch', 'clear', 'flush'
];

/**
Expand Down Expand Up @@ -144,7 +144,7 @@ public function bind(Loader $kernel): void
/**
* Launch Bow task runner
*
* @return void
* @return mixed
* @throws
*/
public function run(): mixed
Expand Down Expand Up @@ -436,6 +436,23 @@ private function clear(): void
$this->command->call('clear', "make", $action);
}

/**
* Flush the connections
*
* @return void
* @throws \ErrorException
*/
private function flush(): void
{
$action = $this->arg->getAction();

if (!in_array($action, ['worker'])) {
$this->throwFailsCommand('This action is not exists', 'help flush');
}

$this->command->call('flush', $action);
}

/**
* Display global help or helper command.
*
Expand All @@ -458,9 +475,9 @@ private function help(?string $command = null): int
\033[0;32mGENERATE\033[00m create a new app key and resources
\033[0;33mgenerate:resource\033[00m Create new REST controller
\033[0;33mgenerate:session\033[00m For generate session table
\033[0;33mgenerate:cache\033[00m For generate cache table
\033[0;33mgenerate:table\033[00m For generate the preset table for session, cache, queue
\033[0;33mgenerate:key\033[00m Create new app key
\033[0;33mflush:worker\033[00m Flush all queues
\033[0;32mADD\033[00m Create a user class
\033[0;33madd:middleware\033[00m Create new middleware
Expand Down Expand Up @@ -545,7 +562,7 @@ private function help(?string $command = null): int
--model=[model_name] Define the usable model
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:resource name [option] For create a new REST controller
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:session For generate session table
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:table For generate the table for session, cache, queue
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:key For generate a new APP KEY
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate help For display this
Expand Down Expand Up @@ -597,13 +614,23 @@ private function help(?string $command = null): int
\033[0;33m$\033[00m php \033[0;34mbow\033[00m seed:all\033[00m Make seeding for all
\033[0;33m$\033[00m php \033[0;34mbow\033[00m seed:table\033[00m table_name Make seeding for one table
U;
break;

case 'flush':
echo <<<U
\n\033[0;32mMFlush all queues content\033[00m\n
[option]
flush:worker [connection] [--queue=queue_name]
\033[0;33m$\033[00m php \033[0;34mbow\033[00m flush:worker\033[00m Flush all queues
U;
break;

default:
$this->throwFailsCommand("Please make php bow help for show whole docs !");
exit(1);
break;
}

exit(0);
Expand Down
19 changes: 16 additions & 3 deletions src/Database/Barry/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,23 @@ public static function findBy(string $column, mixed $value): Collection
* @param mixed $id
* @param array $select
*
* @return Collection|static|null
* @return Collection|Model|null
*/
public static function findAndDelete(
int | string | array $id,
array $select = ['*']
): Model {
): Collection|Model|null {
$model = static::find($id, $select);

if (is_null($model)) {
return $model;
}

if ($model instanceof Collection) {
$model->dropAll();
return $model;
}

$model->delete();

return $model;
Expand Down Expand Up @@ -876,7 +885,11 @@ public function __get(string $name): mixed
$attribute_exists = isset($this->attributes[$name]);

if (!$attribute_exists && method_exists($this, $name)) {
return $this->$name()->getResults();
$result = $this->$name();
if ($result instanceof Relation) {
return $result->getResults();
}
return $result;
}

if (!$attribute_exists) {
Expand Down
18 changes: 12 additions & 6 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Bow\Database\Connection\Adapter\MysqlAdapter;
use Bow\Database\Connection\Adapter\SqliteAdapter;
use Bow\Database\Connection\Adapter\PostgreSQLAdapter;
use ErrorException;

class Database
{
Expand Down Expand Up @@ -76,9 +77,8 @@ public static function getInstance(): Database
/**
* Connection, starts the connection on the DB
*
* @param null $name
* @return null|Database
*
* @param ?string $name
* @return ?Database
* @throws ConnectionException
*/
public static function connection(?string $name = null): ?Database
Expand Down Expand Up @@ -398,7 +398,7 @@ public static function transaction(callable $callback): mixed
} catch (DatabaseException $e) {
static::rollback();

throw $e;
throw $e;
}
}

Expand All @@ -418,9 +418,9 @@ private static function verifyConnection(): void
* Retrieves the identifier of the last record.
*
* @param ?string $name
* @return int|string
* @return int|string|PDO
*/
public static function lastInsertId(?string $name = null): int|string
public static function lastInsertId(?string $name = null): int|string|PDO
{
static::verifyConnection();

Expand Down Expand Up @@ -488,6 +488,12 @@ public static function setPdo(PDO $pdo)
*/
public function __call(string $method, array $arguments)
{
if (is_null(static::$instance)) {
throw new ErrorException(
"Unable to get database instance before configuration"
);
}

if (method_exists(static::$instance, $method)) {
return call_user_func_array(
[static::$instance, $method],
Expand Down
19 changes: 19 additions & 0 deletions src/Database/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,25 @@ public function whereRaw(string $where): QueryBuilder
return $this;
}

/**
* Add orWhere clause into the request
*
* WHERE column1 $comparator $value|column
*
* @param string $where
* @return QueryBuilder
*/
public function orWhereRaw(string $where): QueryBuilder
{
if ($this->where == null) {
$this->where = $where;
} else {
$this->where .= ' or ' . $where;
}

return $this;
}

/**
* orWhere, add a condition of type:
*
Expand Down
10 changes: 7 additions & 3 deletions src/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace Bow\Event;

use Bow\Session\Session;
use Bow\Container\Action;
use Bow\Support\Collection;
use Bow\Event\Contracts\AppEvent;
use ErrorException;

class Event
{
Expand Down Expand Up @@ -143,6 +141,12 @@ public static function bound(string $event): bool
*/
public function __call(string $name, array $arguments)
{
if (is_null(static::$instance)) {
throw new ErrorException(
"Unable to get event instance before configuration"
);
}

if (method_exists(static::$instance, $name)) {
return call_user_func_array([static::$instance, $name], $arguments);
}
Expand Down
Loading

0 comments on commit c4220ad

Please sign in to comment.