Skip to content

Commit 362f048

Browse files
committed
Development snapshot.
1 parent 9920628 commit 362f048

File tree

6 files changed

+123
-24
lines changed

6 files changed

+123
-24
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"Academe\\Laravel\\ContextualNotes\\ServiceProvider"
3030
],
3131
"aliases": {
32-
"AcademeContextualNotes": "Academe\\Laravel\\ContextualNotes\\Facade"
32+
"ContextualNotes": "Academe\\Laravel\\ContextualNotes\\Facade"
3333
}
3434
}
3535
}

config/academe-contextual-notes.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* academe-contextual-log
4+
* academe-contextual-notes
55
*/
66

77
return [
@@ -11,4 +11,16 @@
1111
'notables' => [
1212
'table' => 'contextual_notables',
1313
],
14+
15+
/**
16+
* Array of relationship names and classes for the relationship from
17+
* a note to a model.
18+
* With the example below configured, this will give you a collection of
19+
* users that are attached to a note (usually zero or one):
20+
*
21+
* $note->users;
22+
*/
23+
'models' => [
24+
'users' => App\User::class,
25+
],
1426
];

database/migrations/2018_02_08_002524_create_notes.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ public function up()
1919
$table->bigIncrements('id');
2020
$table->timestamps();
2121

22+
// Note level.
23+
$table->string('level', 60)->nullable();
24+
2225
// Just a simple message for now.
23-
$table->string('message', 255);
26+
$table->text('message');
2427
});
2528
}
2629

src/Models/Note.php

+47-16
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,96 @@
44

55
/**
66
* TODO: author
7-
* TODO: severity
7+
* TODO: severity/level
88
*/
99

1010
use Illuminate\Database\Eloquent\Model;
11+
use Log;
1112

1213
class Note extends Model
1314
{
1415
protected $fillable = [
16+
'level',
1517
'message',
1618
];
1719

20+
protected $attributes = [
21+
'message' => '',
22+
];
23+
1824
public function getTable()
1925
{
2026
return config('academe-contextual-notes.notes.table', 'notes');
2127
}
2228

29+
/**
30+
* Relationship.
31+
*/
32+
public function models($model)
33+
{
34+
return $this->morphedByMany(
35+
$model instanceof Model ? get_class($model) : $model,
36+
'notable',
37+
config('academe-contextual-notes.notables.table', 'notables')
38+
);
39+
}
40+
2341
/**
2442
* Add this note to a single model.
2543
*
2644
* @param Model $model any eloquent mndel
2745
* @return TBC
2846
*/
29-
public function addToModel(Model $model)
47+
public function attachModel(Model $model)
3048
{
31-
return $this->morphedByMany(
32-
$model,
33-
'notable',
34-
config('academe-contextual-notes.notables.table', 'notables')
35-
)->save($model);
49+
return $this->models($model)->save($model);
3650
}
3751

3852
/**
3953
* Add this note to a list of models.
40-
* TODO: more flexibility of the types of lists that can be provided
54+
* TODO: more flexibility of the types of lists that can be provided, or
55+
* even support varidiac (?) parameters.
4156
*
4257
* @param array $models eloquent mndels, of the same or different types
43-
* @return null
58+
* @return self
4459
*/
45-
public function addToModels(array $models)
60+
public function attach($models)
4661
{
62+
if (! is_array($models) && ! $models instanceof \Traversable) {
63+
$models = [$models];
64+
}
65+
4766
foreach ($models as $model) {
48-
$this->addToModel($model);
67+
if ($model instanceof Model) {
68+
$this->attachModel($model);
69+
}
4970
}
71+
72+
return $this;
5073
}
5174

75+
/**
76+
* Reverse relationship, defined by config.
77+
*/
5278
public function __call($method, $arguments)
5379
{
54-
// Just trying this out.
55-
// The method name and model mapping would be set by configuration.
56-
// The same functionaility would be in the __get() magic method too.
80+
$mapping = config('academe-contextual-notes.models', []);
5781

58-
if ($method === 'bankTransactions') {
82+
if (array_key_exists($method, $mapping)) {
5983
return $this->morphedByMany(
60-
'Consilience\Bluejay\Ledger\Models\BankTransaction',
84+
$mapping[$method],
6185
'notable',
6286
config('academe-contextual-notes.notables.table', 'notables')
6387
);
6488
}
6589

6690
return parent::__call($method, $arguments);
6791
}
92+
93+
/**
94+
* Write a log entry for this note, to the default log.
95+
*/
96+
public function withLog()
97+
{
98+
}
6899
}

src/Service.php

+54-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,63 @@
66
*
77
*/
88

9+
use Academe\Laravel\ContextualNotes\Models\Note;
10+
911
class Service
1012
{
11-
//protected $helper;
13+
public function createLevel($level, $message, array $context = [])
14+
{
15+
$note = new Note([
16+
'level' => $level,
17+
'message' => $message,
18+
]);
19+
20+
$note->save();
21+
22+
if (! empty($context)) {
23+
$note->attach($context);
24+
}
25+
26+
return $note;
27+
}
28+
29+
public function emergency($message, array $context = [])
30+
{
31+
return $this->createLevel('emergency', $message, $context);
32+
}
33+
34+
public function alert($message, array $context = [])
35+
{
36+
return $this->createLevel('alert', $message, $context);
37+
}
38+
39+
public function critical($message, array $context = [])
40+
{
41+
return $this->createLevel('critical', $message, $context);
42+
}
43+
44+
public function error($message, array $context = [])
45+
{
46+
return $this->createLevel('error', $message, $context);
47+
}
48+
49+
public function warning($message, array $context = [])
50+
{
51+
return $this->createLevel('warning', $message, $context);
52+
}
53+
54+
public function notice($message, array $context = [])
55+
{
56+
return $this->createLevel('notice', $message, $context);
57+
}
58+
59+
public function info($message, array $context = [])
60+
{
61+
return $this->createLevel('info', $message, $context);
62+
}
1263

13-
public function __construct()
64+
public function debug($message, array $context = [])
1465
{
15-
//$this->helper = new Helper();
66+
return $this->createLevel('debug', $message, $context);
1667
}
1768
}

src/Traits/HasNotes.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
*
77
*/
88

9+
use Academe\Laravel\ContextualNotes\Models\Note;
10+
911
trait HasNotes
1012
{
11-
public function notes()
13+
public function contextualNotes()
1214
{
1315
return $this->morphToMany(
14-
'Academe\Laravel\ContextualNotes\Models\Note',
16+
Note::class,
1517
'notable',
1618
config('academe-contextual-notes.notables.table', 'notables')
1719
);

0 commit comments

Comments
 (0)