-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymfony.php
More file actions
104 lines (87 loc) · 2.59 KB
/
Symfony.php
File metadata and controls
104 lines (87 loc) · 2.59 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace App;
use Exception;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\ErrorHandler\Debug;
class Symfony
{
private $container;
public $entity;
private static $entityNamespace = "App\\Entity\\";
private static $repositoryNamespace = "App\\Repository\\";
private $manager;
public function __construct()
{
$dotEnvFilePath = dirname(__DIR__) . '/../panel/.env';
$this->kernel = $this->implementSymfony($dotEnvFilePath);
$this->kernel->boot();
$this->setKernelAttributes();
}
public function setKernelAttributes()
{
$this->container = $this->kernel->getContainer();
$this->manager = $this->container->get('doctrine.orm.entity_manager');
}
private function implementSymfony($envFilePath)
{
(new Dotenv())->bootEnv($envFilePath);
if ($_SERVER['APP_DEBUG']) {
umask(0000);
Debug::enable();
}
return new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
}
public function find(string $entityName, int $id)
{
return $this->getRepository($entityName)->find($id);
}
public function findAll(string $entityName)
{
return $this->getRepository($entityName)->findAll();
}
public function findBy(string $entityName, array $attributes)
{
return $this->getRepository($entityName)->findBy($attributes);
}
public function findOneBy(string $entityName, array $attributes)
{
return $this->getRepository($entityName)->findOneBy($attributes);
}
public function getRepository(string $entityName)
{
$repoName = self::$entityNamespace . $entityName;
return $this->manager->getRepository($repoName);
}
public function sendItemToDatabase($item = null, $flush = true, $return_response = true)
{
if ($item) {
try {
$this->manager->persist($item);
if ($flush) {
$this->manager->flush();
return $item->getId();
}
} catch (\Throwable $th) {
return $th;
}
} else {
throw new Exception('Błąd podczas dodawania do bazy danych');
return false;
}
}
/**
* Get the Symfony of container
*/
public function getContainer()
{
return $this->container;
}
public function getServiceByAlias(string $alias)
{
return $this->container->get($alias);
}
public function getKernel()
{
return $this->kernel;
}
}