Skip to content

Commit 0b4cf78

Browse files
committed
first release for the Run Framework
0 parents  commit 0b4cf78

File tree

12 files changed

+482
-0
lines changed

12 files changed

+482
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "php-run/framework",
3+
"description": "Run is a nano-API framework for building awesome small productive and scalable web apps.",
4+
"license": "MIT",
5+
"autoload": {
6+
"psr-4": {
7+
"Run\\": "src/"
8+
},
9+
"files": [
10+
"src/Steps/Helper/helpers.php"
11+
]
12+
},
13+
"authors": [
14+
{
15+
"name": "baselrabia",
16+
"email": "[email protected]"
17+
}
18+
],
19+
"minimum-stability": "stable",
20+
"require": {
21+
"symfony/console": "^5.4",
22+
"symfony/var-dumper": "^5.4",
23+
"illuminate/events": "^9.30",
24+
"illuminate/routing": "^9.30",
25+
"illuminate/config": "^9.30",
26+
"illuminate/container": "^9.30",
27+
"laravel-zero/foundation": "^9.26",
28+
"filp/whoops": "^2.14",
29+
"vlucas/phpdotenv": "^5.4",
30+
"illuminate/filesystem": "^9.30",
31+
"illuminate/log": "^9.30"
32+
}
33+
}

src/Application.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Run;
4+
5+
use Illuminate\Container\Container;
6+
use Illuminate\Events\EventServiceProvider;
7+
use Illuminate\Filesystem\Filesystem;
8+
use Illuminate\Foundation\Application as BaseApplication;
9+
use Illuminate\Foundation\PackageManifest;
10+
use Illuminate\Log\LogServiceProvider;
11+
use Illuminate\Routing\RoutingServiceProvider;
12+
13+
class Application extends BaseApplication
14+
{
15+
16+
17+
/**
18+
* Create a new Run application instance.
19+
*
20+
* @param string|null $basePath
21+
* @return void
22+
*/
23+
public function __construct($basePath = null)
24+
{
25+
26+
if ($basePath) {
27+
$this->setBasePath($basePath);
28+
}
29+
30+
$this->registerBaseBindings();
31+
$this->registerBaseServiceProviders();
32+
$this->registerCoreContainerAliases();
33+
34+
$this->registerErrorHandling();
35+
}
36+
37+
/**
38+
* Bootstrap the application container.
39+
*
40+
* @return void
41+
*/
42+
protected function registerBaseBindings()
43+
{
44+
static::setInstance($this);
45+
46+
$this->instance('app', $this);
47+
$this->instance(self::class, $this);
48+
$this->instance(Container::class, $this);
49+
50+
$this->instance('files', new \Illuminate\Filesystem\Filesystem);
51+
52+
$this->singleton(PackageManifest::class, function () {
53+
return new PackageManifest(
54+
new Filesystem, $this->basePath(), $this->getCachedPackagesPath()
55+
);
56+
});
57+
58+
}
59+
60+
61+
62+
63+
64+
private function registerErrorHandling()
65+
{
66+
error_reporting(-1);
67+
68+
$whoops = new \Whoops\Run;
69+
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
70+
$whoops->register();
71+
72+
}
73+
74+
protected function registerBaseServiceProviders()
75+
{
76+
$this->register(new EventServiceProvider($this));
77+
$this->register(new LogServiceProvider($this));
78+
$this->register(new RoutingServiceProvider($this));
79+
80+
}
81+
82+
83+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Run\Exceptions\Container;
4+
5+
use Exception;
6+
use Psr\Container\ContainerExceptionInterface;
7+
8+
class BindingResolutionException extends Exception implements ContainerExceptionInterface
9+
{
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Run\Exceptions\Container;
4+
5+
use Exception;
6+
use Psr\Container\ContainerExceptionInterface;
7+
8+
class ContainerException extends Exception implements ContainerExceptionInterface
9+
{
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Run\Exceptions\Container;
4+
5+
use Exception;
6+
use Psr\Container\NotFoundExceptionInterface;
7+
8+
class NotFoundException extends Exception implements NotFoundExceptionInterface
9+
{
10+
11+
}

src/Exceptions/Handler.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Run\Exceptions;
4+
5+
use Illuminate\Contracts\Debug\ExceptionHandler;
6+
use Throwable;
7+
8+
class Handler implements ExceptionHandler
9+
{
10+
/**
11+
* Report or log an exception.
12+
*
13+
* @param \Throwable $e
14+
* @return void
15+
*
16+
* @throws \Throwable
17+
*/
18+
public function report(Throwable $e)
19+
{
20+
// TODO: Implement report() method.
21+
}
22+
23+
/**
24+
* Determine if the exception should be reported.
25+
*
26+
* @param \Throwable $e
27+
* @return bool
28+
*/
29+
public function shouldReport(Throwable $e)
30+
{
31+
// TODO: Implement shouldReport() method.
32+
}
33+
34+
/**
35+
* Render an exception into an HTTP response.
36+
*
37+
* @param \Illuminate\Http\Request $request
38+
* @param \Throwable $e
39+
* @return \Symfony\Component\HttpFoundation\Response
40+
*
41+
* @throws \Throwable
42+
*/
43+
public function render($request, Throwable $e)
44+
{
45+
// TODO: Implement render() method.
46+
throw $e ;
47+
48+
}
49+
50+
/**
51+
* Render an exception to the console.
52+
*
53+
* @param \Symfony\Component\Console\Output\OutputInterface $output
54+
* @param \Throwable $e
55+
* @return void
56+
*
57+
* @internal This method is not meant to be used or overwritten outside the framework.
58+
*/
59+
public function renderForConsole($output, Throwable $e)
60+
{
61+
// TODO: Implement renderForConsole() method.
62+
}
63+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Run\Exceptions;
4+
use Exception;
5+
6+
class RouteNotFoundException extends Exception
7+
{
8+
9+
protected $message = '404 not found route';
10+
}

src/Steps/Container/Container.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Run\Steps\Container;
4+
5+
use Psr\Container\ContainerInterface;
6+
use Run\Exceptions\Container\ContainerException;
7+
8+
class Container implements ContainerInterface
9+
{
10+
/**
11+
* The current globally available container (if any).
12+
*
13+
* @var static
14+
*/
15+
protected static $instance;
16+
17+
private array $entries = [];
18+
19+
/**
20+
* Get the globally available instance of the container.
21+
*
22+
* @return static
23+
*/
24+
public static function getInstance()
25+
{
26+
if (is_null(static::$instance)) {
27+
static::$instance = new static;
28+
}
29+
30+
return static::$instance;
31+
}
32+
33+
/**
34+
* Set the shared instance of the container.
35+
*
36+
*/
37+
public static function setInstance(ContainerInterface $container = null)
38+
{
39+
return static::$instance = $container;
40+
}
41+
42+
43+
44+
/**
45+
* Resolve the given type from the container.
46+
*
47+
* @param string|callable $abstract
48+
* @param array $parameters
49+
* @return mixed
50+
*
51+
* @throws \Run\Exceptions\Container\BindingResolutionException
52+
*/
53+
public function make($abstract, array $parameters = [])
54+
{
55+
return $this->resolve($abstract, $parameters);
56+
}
57+
58+
public function get(string $id)
59+
{
60+
if ($this->has($id)) {
61+
$entry = $this->entries[$id];
62+
63+
return $entry($this);
64+
}
65+
66+
return $this->resolve($id);
67+
}
68+
69+
public function has(string $id): bool
70+
{
71+
return isset($this->entries[$id]);
72+
}
73+
74+
public function set(string $id, callable $concrete): void
75+
{
76+
$this->entries[$id] = $concrete;
77+
}
78+
79+
80+
81+
protected function resolve(string $id ,$parameters = [])
82+
{
83+
// 1. Inspect the class that we are trying to get from the container
84+
$reflectionClass = new \ReflectionClass($id);
85+
86+
if (! $reflectionClass->isInstantiable()) {
87+
throw new ContainerException('Class "' . $id . '" is not instantiable');
88+
}
89+
90+
// 2. Inspect the constructor of the class
91+
$constructor = $reflectionClass->getConstructor();
92+
93+
if (! $constructor) {
94+
return new $id;
95+
}
96+
97+
// 3. Inspect the constructor parameters (dependencies)
98+
$parameters = $constructor->getParameters();
99+
100+
if (! $parameters) {
101+
return new $id;
102+
}
103+
104+
// 4. If the constructor parameter is a class then try to resolve that class using the container
105+
$dependencies = array_map(function (\ReflectionParameter $param) use ($id) {
106+
$type = $param->getType();
107+
$name = $param->getName();
108+
109+
if (! $type) throw new ContainerException(
110+
'Failed to resolve class "' . $id . '" because param "' . $name . '" is missing a type hint'
111+
);
112+
113+
114+
if ($type instanceof \ReflectionUnionType) throw new ContainerException(
115+
'Failed to resolve class "' . $id . '" because of union type for param "' . $name . '"'
116+
);
117+
118+
119+
if ($type instanceof \ReflectionNamedType && ! $type->isBuiltin()) {
120+
return $this->get($type->getName());
121+
}
122+
123+
throw new ContainerException(
124+
'Failed to resolve class "' . $id . '" because invalid param "' . $name . '"'
125+
);
126+
}, $parameters
127+
);
128+
129+
return $reflectionClass->newInstanceArgs($dependencies);
130+
}
131+
}

0 commit comments

Comments
 (0)