rougin /
credo
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Rougin\Credo; |
||
| 4 | |||
| 5 | use CI_DB_query_builder as Builder; |
||
| 6 | use Doctrine\Common\Util\Inflector; |
||
| 7 | use Doctrine\ORM\EntityManager; |
||
| 8 | use Doctrine\ORM\Tools\Setup; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Credo |
||
| 12 | * |
||
| 13 | * @method \Doctrine\ORM\EntityRepository getRepository(string $entityName) |
||
| 14 | * @method \Doctrine\ORM\Mapping\ClassMetadataFactory getMetadataFactory() |
||
| 15 | * @method \Doctrine\ORM\Query\QueryBuilder createQueryBuilder() |
||
| 16 | * @method object refresh($entity) |
||
| 17 | * @method void flush(object|array $entity = null) |
||
| 18 | * @method void remove(object $entity) |
||
| 19 | * |
||
| 20 | * @package Credo |
||
| 21 | * @author Rougin Gutib <[email protected]> |
||
| 22 | */ |
||
| 23 | class Credo |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $criteria = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var \Doctrine\ORM\EntityManager |
||
| 32 | */ |
||
| 33 | protected $manager; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Calls methods from the specified object in underscore case. |
||
| 37 | * |
||
| 38 | * @param string $method |
||
| 39 | * @param mixed $parameters |
||
| 40 | * @return mixed |
||
| 41 | */ |
||
| 42 | 6 | public function __call($method, $parameters) |
|
| 43 | { |
||
| 44 | 6 | $method = (string) Inflector::camelize($method); |
|
| 45 | |||
| 46 | 6 | $instance = array($this->manager, (string) $method); |
|
| 47 | |||
| 48 | 6 | return call_user_func_array($instance, $parameters); |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Initializes the EntityManager instance. |
||
| 53 | * |
||
| 54 | * @param \CI_DB_query_builder $builder |
||
| 55 | */ |
||
| 56 | 15 | public function __construct(Builder $builder) |
|
| 57 | { |
||
| 58 | 15 | $connection = (array) $this->connection($builder); |
|
| 59 | |||
| 60 | 15 | $dsn = strpos($connection['dsn'], ':'); |
|
| 61 | |||
| 62 | 15 | $driver = (string) $connection['driver']; |
|
| 63 | |||
| 64 | 15 | if ($driver === 'pdo' && $dsn !== false) { |
|
| 65 | 15 | $keys = (array) explode(':', $connection['dsn']); |
|
| 66 | |||
| 67 | 15 | $connection['driver'] .= (string) '_' . $keys[0]; |
|
| 68 | 10 | } |
|
| 69 | |||
| 70 | 15 | if ($connection['driver'] === 'pdo_sqlite') { |
|
| 71 | 15 | $connection['path'] = str_replace('sqlite:', '', $connection['dsn']); |
|
| 72 | 10 | } |
|
| 73 | |||
| 74 | 15 | $this->manager = $this->boot($connection, $builder->db_debug); |
|
| 75 | 10 | } |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Finds the row from storage based on given identifier. |
||
| 79 | * |
||
| 80 | * @param string $class |
||
| 81 | * @param integer $id |
||
| 82 | * @param integer|null $mode |
||
| 83 | * @param integer|null $version |
||
| 84 | * @return mixed |
||
| 85 | */ |
||
| 86 | 6 | public function find($class, $id, $mode = null, $version = null) |
|
| 87 | { |
||
| 88 | 6 | return $this->manager->getRepository($class)->find($id, $mode, $version); |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Finds models by a set of criteria. |
||
| 93 | * |
||
| 94 | * @param string $class |
||
| 95 | * @param array $criteria |
||
| 96 | * @param integer|null $offset |
||
| 97 | * @param array|null $order |
||
| 98 | * @param integer|null $limit |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | 8 | public function findBy($class, array $criteria = array(), array $order = null, $limit = null, $offset = null) |
|
| 102 | { |
||
| 103 | 8 | $repository = $this->manager->getRepository((string) $class); |
|
| 104 | |||
| 105 | 8 | return $repository->findBy($criteria, $order, $limit, $offset); |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns an array of rows from a specified class. |
||
| 110 | * |
||
| 111 | * @param string $class |
||
| 112 | * @param integer|null $limit |
||
| 113 | * @param integer|null $offset |
||
| 114 | * @param array|null $order |
||
| 115 | * @return mixed |
||
| 116 | */ |
||
| 117 | 6 | public function get($class, $limit = null, $offset = null, array $order = null) |
|
| 118 | { |
||
| 119 | 6 | ($criteria = (array) $this->criteria) && $this->criteria = array(); |
|
| 120 | |||
| 121 | 6 | return $this->findBy($class, $criteria, $order, $limit, $offset); |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Sets the "WHERE" criteria. |
||
| 126 | * |
||
| 127 | * @param array|string $key |
||
| 128 | * @param mixed|null $value |
||
| 129 | * @return self |
||
| 130 | */ |
||
| 131 | 4 | public function where($key, $value = null) |
|
| 132 | { |
||
| 133 | 4 | if (is_array($key) === true) { |
|
| 134 | 2 | $this->criteria = (array) $key; |
|
| 135 | 2 | } else { |
|
| 136 | 2 | $this->criteria[$key] = $value; |
|
| 137 | } |
||
| 138 | |||
| 139 | 4 | return $this; |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Bootstraps the EntityManager instance. |
||
| 144 | * |
||
| 145 | * @param array $connection |
||
| 146 | * @param boolean $debug |
||
| 147 | * @return \Doctrine\ORM\EntityManager |
||
| 148 | */ |
||
| 149 | 15 | protected function boot(array $connection, $debug = false) |
|
| 150 | { |
||
| 151 | 15 | $proxies = (string) APPPATH . 'models/proxies'; |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 152 | |||
| 153 | 15 | $folders = array((string) APPPATH . 'models'); |
|
| 154 | |||
| 155 | 15 | array_push($folders, APPPATH . 'repositories'); |
|
| 156 | |||
| 157 | // Set $debug to TRUE to disable caching while you develop |
||
| 158 | 15 | $config = Setup::createAnnotationMetadataConfiguration($folders, $debug, $proxies); |
|
| 159 | |||
| 160 | 15 | return EntityManager::create($connection, $config); |
|
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Sets up the database configuration from CodeIgniter. |
||
| 165 | * |
||
| 166 | * @param \CI_DB_query_builder $builder |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 15 | protected function connection(Builder $builder) |
|
| 170 | { |
||
| 171 | 15 | $connection = array('dsn' => $builder->dsn); |
|
| 172 | |||
| 173 | 15 | $connection['driver'] = $builder->dbdriver; |
|
| 174 | |||
| 175 | 15 | $connection['user'] = $builder->username; |
|
| 176 | |||
| 177 | 15 | $connection['password'] = $builder->password; |
|
| 178 | |||
| 179 | 15 | $connection['host'] = $builder->hostname; |
|
| 180 | |||
| 181 | 15 | $connection['dbname'] = $builder->database; |
|
| 182 | |||
| 183 | 15 | $connection['charset'] = $builder->char_set; |
|
| 184 | |||
| 185 | 15 | return $connection; |
|
| 186 | } |
||
| 187 | } |
||
| 188 |