This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathFactory.php
More file actions
60 lines (53 loc) · 1.54 KB
/
Copy pathFactory.php
File metadata and controls
60 lines (53 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* This file is part of the SgDatatablesBundle package.
*
* (c) stwe <https://github.com/stwe/DatatablesBundle>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sg\DatatablesBundle\Datatable;
use Exception;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class Factory
*
* @package Sg\DatatablesBundle\Datatable
*/
class Factory
{
/**
* Create.
*
* @param ContainerInterface $container
* @param mixed $class
* @param mixed $interface
*
* @return mixed
* @throws Exception
*/
public static function create(ContainerInterface $container, $class, $interface)
{
if (empty($class) || !is_string($class) && !$class instanceof $interface) {
throw new Exception("Factory::create(): String or $interface expected.");
}
if ($class instanceof $interface) {
return $class;
}
if (is_string($class) && class_exists($class)) {
if ($container->has($class)) {
$instance = $container->get($class);
} else {
$instance = new $class($container);
}
if (!$instance instanceof $interface) {
throw new Exception("Factory::create(): String or $interface expected.");
} else {
return $instance;
}
} else {
throw new Exception("Factory::create(): $class is not callable.");
}
}
}