Skip to content
This repository was archived by the owner on Dec 11, 2020. It is now read-only.

Commit 9d49888

Browse files
committed
add foundation files
1 parent afa01ea commit 9d49888

14 files changed

+454
-1
lines changed

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,10 @@
99
"email": "[email protected]"
1010
}
1111
],
12-
"require": {}
12+
"require": {},
13+
"autoload": {
14+
"psr-4": {
15+
"Lucid\\Foundation": "src/"
16+
}
17+
}
1318
}

src/Feature.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Lucid\Foundation;
4+
5+
use ReflectionClass;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Collection;
8+
use App\Domains\Queue\DefaultQueue;
9+
use Illuminate\Contracts\Bus\SelfHandling;
10+
use Illuminate\Foundation\Bus\DispatchesJobs;
11+
12+
abstract class Feature implements SelfHandling
13+
{
14+
use MarshalTrait;
15+
use DispatchesJobs;
16+
17+
/**
18+
* beautifier function to be called instead of the
19+
* laravel function dispatchFromArray.
20+
* When the $arguments is an instance of Request
21+
* it will call dispatchFrom instead.
22+
*
23+
* @param string $job
24+
* @param array|\Illuminate\Http\Request $arguments
25+
* @param array $extra
26+
*
27+
* @return mixed
28+
*/
29+
public function run($job, $arguments = [], $extra = [])
30+
{
31+
if ($arguments instanceof Request) {
32+
$result = $this->dispatch($this->marshal($job, $arguments, $extra));
33+
} else {
34+
if (!is_object($job)) {
35+
$job = $this->marshal($job, new Collection(), $arguments);
36+
}
37+
38+
$result = $this->dispatch($job, $arguments);
39+
}
40+
41+
return $result;
42+
}
43+
44+
/**
45+
* Run the given job in the given queue.
46+
*
47+
* @param string $job
48+
* @param array $arguments
49+
* @param Queue|null $queue
50+
*
51+
* @return mixed
52+
*/
53+
public function runInQueue($job, $arguments, Queue $queue = null)
54+
{
55+
if (!$queue) {
56+
$queue = DefaultQueue::class;
57+
}
58+
59+
// instantiate and queue the job
60+
$reflection = new ReflectionClass($job);
61+
$jobInstance = $reflection->newInstanceArgs($arguments);
62+
$jobInstance->onQueue((string) $queue);
63+
64+
return $this->dispatch($jobInstance);
65+
}
66+
}

src/Http/Controller.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Lucid\Foundation\Http;
4+
5+
use Lucid\Foundation\ServesFeaturesTrait;
6+
use Illuminate\Routing\Controller as BaseController;
7+
use Illuminate\Foundation\Validation\ValidatesRequests;
8+
9+
/**
10+
* Base controller.
11+
*/
12+
class Controller extends BaseController
13+
{
14+
use ValidatesRequests;
15+
use ServesFeaturesTrait;
16+
}

src/InvalidInputException.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Lucid\Foundation;
4+
5+
use Exception;
6+
use Illuminate\Validation\Validator as IlluminateValidator;
7+
8+
/**
9+
* An exception class that supports validators by extracting their messages
10+
* when given, or an array of messages as strings.
11+
*/
12+
class InvalidInputException extends Exception
13+
{
14+
public function __construct($message = '', $code = 0, Exception $previous = null)
15+
{
16+
if ($message instanceof IlluminateValidator) {
17+
$message = $message->messages()->all();
18+
}
19+
20+
if (is_array($message)) {
21+
$message = implode("\n", $message);
22+
}
23+
24+
parent::__construct($message, $code, $previous);
25+
}
26+
}

src/Job.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Lucid\Foundation;
4+
5+
use Illuminate\Contracts\Bus\SelfHandling;
6+
7+
/**
8+
* An abstract Job to be extended by every job.
9+
* Note that this job is self-handling which
10+
* means it will NOT be queued, rather
11+
* will have the "handle()" method
12+
* called instead.
13+
*/
14+
abstract class Job implements SelfHandling
15+
{
16+
}

src/MarshalTrait.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Lucid\Foundation;
4+
5+
use Exception;
6+
use ArrayAccess;
7+
use ReflectionClass;
8+
use ReflectionParameter;
9+
10+
trait MarshalTrait
11+
{
12+
/**
13+
* Marshal a command from the given array accessible object.
14+
*
15+
* @param string $command
16+
* @param \ArrayAccess $source
17+
* @param array $extras
18+
*
19+
* @return mixed
20+
*/
21+
protected function marshal($command, ArrayAccess $source, array $extras = [])
22+
{
23+
$injected = [];
24+
25+
$reflection = new ReflectionClass($command);
26+
27+
if ($constructor = $reflection->getConstructor()) {
28+
$injected = array_map(function ($parameter) use ($command, $source, $extras) {
29+
return $this->getParameterValueForCommand($command, $source, $parameter, $extras);
30+
}, $constructor->getParameters());
31+
}
32+
33+
return $reflection->newInstanceArgs($injected);
34+
}
35+
36+
/**
37+
* Get a parameter value for a marshaled command.
38+
*
39+
* @param string $command
40+
* @param \ArrayAccess $source
41+
* @param \ReflectionParameter $parameter
42+
* @param array $extras
43+
*
44+
* @return mixed
45+
*/
46+
protected function getParameterValueForCommand($command, ArrayAccess $source,
47+
ReflectionParameter $parameter, array $extras = [])
48+
{
49+
if (array_key_exists($parameter->name, $extras)) {
50+
return $extras[$parameter->name];
51+
}
52+
53+
if (isset($source[$parameter->name])) {
54+
return $source[$parameter->name];
55+
}
56+
57+
if ($parameter->isDefaultValueAvailable()) {
58+
return $parameter->getDefaultValue();
59+
}
60+
61+
throw new Exception("Unable to map parameter [{$parameter->name}] to command [{$command}]");
62+
}
63+
}

src/Model.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Lucid\Foundation;
4+
5+
use Eloquent;
6+
7+
/**
8+
* Base Model.
9+
*/
10+
class Model extends Eloquent
11+
{
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Lucid\Foundation\Providers;
4+
5+
use Illuminate\Routing\Router;
6+
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as BaseServiceProvider;
7+
8+
abstract class RouteServiceProvider extends BaseServiceProvider
9+
{
10+
/**
11+
* Read the routes from the "routes.php" file of this Service
12+
*
13+
* @param \Illuminate\Routing\Router $router
14+
*/
15+
abstract public function map(Router $router);
16+
17+
public function loadRoutesFile($router, $namespace, $path)
18+
{
19+
$router->group(['namespace' => $namespace], function ($router) use ($path) {
20+
if (file_exists($path)) {
21+
require $path;
22+
}
23+
});
24+
}
25+
}

src/QueueableJob.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Lucid\Foundation;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
use Illuminate\Queue\InteractsWithQueue;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
9+
/**
10+
* An abstract Job that can be managed with a queue
11+
* when extended the job will be queued by default.
12+
*/
13+
class QueueableJob extends Job implements ShouldQueue
14+
{
15+
use SerializesModels;
16+
use InteractsWithQueue;
17+
}

src/ServesFeaturesTrait.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Lucid\Foundation;
4+
5+
use Illuminate\Support\Collection;
6+
use Illuminate\Foundation\Bus\DispatchesJobs;
7+
8+
trait ServesFeaturesTrait
9+
{
10+
use MarshalTrait;
11+
use DispatchesJobs;
12+
13+
/**
14+
* Serve the given feature with the given arguments.
15+
*
16+
* @param \Lucid\Foundation\AbstractFeature $feature
17+
* @param array $arguments
18+
*
19+
* @return mixed
20+
*/
21+
public function serve($feature, $arguments = [])
22+
{
23+
return $this->dispatch($this->marshal($feature, new Collection(), $arguments));
24+
}
25+
}

0 commit comments

Comments
 (0)