1: <?php
2:
3: 4: 5: 6: 7: 8:
9:
10: namespace rsanchez\Deep;
11:
12: use Illuminate\Container\Container;
13: use Illuminate\CodeIgniter\CodeIgniterConnectionResolver;
14: use Illuminate\Database\Eloquent\Model;
15: use rsanchez\Deep\Model\Field;
16: use rsanchez\Deep\Model\Channel;
17: use rsanchez\Deep\Model\Entry;
18: use rsanchez\Deep\Model\Title;
19: use rsanchez\Deep\Model\Site;
20: use rsanchez\Deep\Model\Category;
21: use rsanchez\Deep\Model\Member;
22: use rsanchez\Deep\Model\CategoryField;
23: use rsanchez\Deep\Model\MemberField;
24: use rsanchez\Deep\Model\UploadPref;
25: use rsanchez\Deep\Model\Asset;
26: use rsanchez\Deep\Model\File;
27: use rsanchez\Deep\Model\GridCol;
28: use rsanchez\Deep\Model\GridRow;
29: use rsanchez\Deep\Model\MatrixCol;
30: use rsanchez\Deep\Model\MatrixRow;
31: use rsanchez\Deep\Model\PlayaEntry;
32: use rsanchez\Deep\Model\RelationshipEntry;
33: use rsanchez\Deep\Repository\FieldRepository;
34: use rsanchez\Deep\Repository\ChannelRepository;
35: use rsanchez\Deep\Repository\SiteRepository;
36: use rsanchez\Deep\Repository\UploadPrefRepository;
37: use rsanchez\Deep\Repository\ConfigUploadPrefRepository;
38: use rsanchez\Deep\Repository\CategoryFieldRepository;
39: use rsanchez\Deep\Repository\MemberFieldRepository;
40: use rsanchez\Deep\Hydrator\HydratorFactory;
41: use Carbon\Carbon;
42: use CI_Controller;
43: use Closure;
44:
45: 46: 47:
48: class Deep extends Container
49: {
50: 51: 52: 53: 54: 55: 56:
57: public function __construct($config = array())
58: {
59: Carbon::setToStringFormat(Carbon::ISO8601);
60:
61: $this->singleton('config', function ($app) use ($config) {
62: return $config;
63: });
64:
65: $this->singleton('Field', function ($app) {
66: return new Field();
67: });
68:
69: $this->singleton('FieldRepository', function ($app) {
70: return new FieldRepository($app->make('Field'));
71: });
72:
73: $this->singleton('Channel', function ($app) {
74: $channel = new Channel();
75:
76: $channel->setFieldRepository($app->make('FieldRepository'));
77:
78: return $channel;
79: });
80:
81: $this->singleton('Site', function ($app) {
82: return new Site();
83: });
84:
85: $this->singleton('UploadPref', function ($app) {
86: return new UploadPref();
87: });
88:
89: $this->singleton('CategoryField', function ($app) {
90: return new CategoryField();
91: });
92:
93: $this->singleton('MemberField', function ($app) {
94: return new MemberField();
95: });
96:
97: $this->singleton('CategoryFieldRepository', function ($app) {
98: return new CategoryFieldRepository($app->make('CategoryField'));
99: });
100:
101: $this->singleton('MemberFieldRepository', function ($app) {
102: return new MemberFieldRepository($app->make('MemberField'));
103: });
104:
105: $this->singleton('ChannelRepository', function ($app) {
106: return new ChannelRepository($app->make('Channel'));
107: });
108:
109: $this->singleton('SiteRepository', function ($app) {
110: return new SiteRepository($app->make('Site'));
111: });
112:
113: $this->singleton('UploadPrefRepository', function ($app) {
114: if (isset($app['config']['upload_prefs'])) {
115: return new ConfigUploadPrefRepository($app['config']['upload_prefs']);
116: }
117:
118: return new UploadPrefRepository($app->make('UploadPref'));
119: });
120:
121: $this->singleton('Asset', function ($app) {
122: return new Asset();
123: });
124:
125: $this->singleton('File', function ($app) {
126: return new File();
127: });
128:
129: $this->singleton('GridCol', function ($app) {
130: return new GridCol();
131: });
132:
133: $this->singleton('GridRow', function ($app) {
134: return new GridRow();
135: });
136:
137: $this->singleton('MatrixCol', function ($app) {
138: return new MatrixCol();
139: });
140:
141: $this->singleton('MatrixRow', function ($app) {
142: return new MatrixRow();
143: });
144:
145: $this->singleton('PlayaEntry', function ($app) {
146: return new PlayaEntry();
147: });
148:
149: $this->singleton('RelationshipEntry', function ($app) {
150: return new RelationshipEntry();
151: });
152:
153: $this->singleton('HydratorFactory', function ($app) {
154: return new HydratorFactory(
155: $app->make('SiteRepository'),
156: $app->make('UploadPrefRepository'),
157: $app->make('Asset'),
158: $app->make('File'),
159: $app->make('GridCol'),
160: $app->make('GridRow'),
161: $app->make('MatrixCol'),
162: $app->make('MatrixRow'),
163: $app->make('PlayaEntry'),
164: $app->make('RelationshipEntry')
165: );
166: });
167:
168: $this->singleton('Category', function ($app) {
169: $category = new Category();
170:
171: $category->setCategoryFieldRepository($app->make('CategoryFieldRepository'));
172: $category->setChannelRepository($app->make('ChannelRepository'));
173:
174: return $category;
175: });
176:
177: $this->singleton('Member', function ($app) {
178: $member = new Member();
179:
180: $member->setMemberFieldRepository($app->make('MemberFieldRepository'));
181:
182: return $member;
183: });
184:
185: $this->singleton('Title', function ($app) {
186: $app->make('Category');
187: $app->make('Member');
188:
189: $title = new Title();
190:
191: $title->setChannelRepository($app->make('ChannelRepository'));
192: $title->setSiteRepository($app->make('SiteRepository'));
193: $title->setHydratorFactory($app->make('HydratorFactory'));
194:
195: return $title;
196: });
197:
198: $this->singleton('Entry', function ($app) {
199: $app->make('Title');
200:
201: $entry = new Entry();
202:
203: $entry->setFieldRepository($app->make('FieldRepository'));
204:
205: return $entry;
206: });
207: }
208:
209: 210: 211: 212: 213:
214: public static function bootEloquent(CI_Controller $ee)
215: {
216: if (Model::getConnectionResolver() instanceof CodeIgniterConnectionResolver) {
217: return;
218: }
219:
220: Model::setConnectionResolver(new CodeIgniterConnectionResolver($ee));
221: }
222:
223: 224: 225: 226: 227: 228: 229: 230:
231: public static function bootEE(CI_Controller $ee)
232: {
233: static $booted = false;
234:
235: if ($booted) {
236: return;
237: }
238:
239: if (! Model::getConnectionResolver() instanceof CodeIgniterConnectionResolver) {
240: Model::setConnectionResolver(new CodeIgniterConnectionResolver($ee));
241: }
242:
243: self::extendInstance('config', function ($app) use ($ee) {
244: return $ee->config->config;
245: });
246:
247: $uploadPrefs = $ee->config->item('upload_preferences');
248:
249: if ($uploadPrefs) {
250: self::extendInstance('UploadPrefRepository', function ($app) use ($uploadPrefs) {
251: return new ConfigUploadPrefRepository($uploadPrefs);
252: });
253: }
254:
255: $booted = true;
256: }
257:
258: 259: 260: 261: 262: 263:
264: public static function extendInstance($abstract, Closure $closure)
265: {
266: self::getInstance()->extend($abstract, $closure);
267: }
268:
269: 270: 271: 272:
273: public static function getInstance()
274: {
275: static $app;
276:
277: if (is_null($app)) {
278: $app = new self();
279: }
280:
281: return $app;
282: }
283: }
284: