1: <?php
2:
3: /**
4: * Deep
5: *
6: * @package rsanchez\Deep
7: * @author Rob Sanchez <info@robsanchez.com>
8: */
9:
10: namespace rsanchez\Deep\Repository;
11:
12: use Illuminate\Database\Eloquent\Collection;
13: use Illuminate\Database\Eloquent\Model;
14: use rsanchez\Deep\Repository\RepositoryInterface;
15:
16: /**
17: * Repository of all Sites
18: */
19: abstract class AbstractDeferredRepository implements RepositoryInterface
20: {
21: /**
22: * Repository Model
23: * @var \Illuminate\Database\Eloquent\Model
24: */
25: protected $model;
26:
27: /**
28: * Collection of all records
29: * @var \Illuminate\Database\Eloquent\Collection
30: */
31: protected $collection;
32:
33: /**
34: * Constructor
35: *
36: * @param \Illuminate\Database\Eloquent\Model $model
37: */
38: public function __construct(Model $model)
39: {
40: $this->model = $model;
41: }
42:
43: /**
44: * Defer loading of Collection until needed
45: * @return void
46: */
47: protected function boot()
48: {
49: if (! is_null($this->collection)) {
50: return;
51: }
52:
53: $this->collection = $this->model->all();
54: }
55:
56: /**
57: * {@inheritdoc}
58: */
59: public function find($id)
60: {
61: $this->boot();
62:
63: return $this->collection->find($id);
64: }
65:
66: /**
67: * Get the repository's model instance
68: * @return \Illuminate\Database\Eloquent\Model
69: */
70: public function getModel()
71: {
72: return $this->model;
73: }
74: }
75: