Skip to content
This repository was archived by the owner on Dec 14, 2019. It is now read-only.

Commit e00880a

Browse files
authored
Merge pull request #61 from furey/enable-shortcut-for-empty-tables
Patched various "tableless_models" issues.
2 parents ddcc280 + 69a760b commit e00880a

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

resources/views/includes.blade.php

+25-6
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,39 @@ function tinx_query($class, ...$args)
130130
try {
131131
${!! $name !!} = {!! $class !!}::first() ?: app('{!! $class !!}');
132132
${!! $name !!}_ = {!! $class !!}::latest($latestColumn)->first() ?: app('{!! $class !!}');
133-
if (!function_exists('{!! $name !!}')) {
134-
function {!! $name !!}(...$args) {
135-
return tinx_query('{!! $class !!}', ...$args);
136-
}
137-
}
133+
array_set($GLOBALS, 'tinx.shortcuts.{!! $name !!}', ${!! $name !!});
134+
array_set($GLOBALS, 'tinx.shortcuts.{!! $name !!}_', ${!! $name !!}_);
138135
} catch (Throwable $e) {
139136
@include('tinx::on-name-error')
140137
} catch (Exception $e) {
141138
@include('tinx::on-name-error')
142139
}
140+
if (!function_exists('{!! $name !!}')) {
141+
function {!! $name !!}(...$args) {
142+
return tinx_query('{!! $class !!}', ...$args);
143+
}
144+
}
143145
@endforeach
144146
unset($latestColumn);
145147

146148
/**
147-
* Quick reference array.
149+
* Quick names reference array.
148150
* */
149151
$names = array_get($GLOBALS, 'tinx.names');
152+
153+
/**
154+
* Define shortcuts for "names()" table, and also set quick shortcuts reference array.
155+
* */
156+
$shortcuts = collect($names)->map(function ($name, $class) {
157+
$shortcuts = [];
158+
if (array_has($GLOBALS, "tinx.shortcuts.$name")) $shortcuts[] = "\${$name}";
159+
if (array_has($GLOBALS, "tinx.shortcuts.{$name}_")) $shortcuts[] = "\${$name}_";
160+
if (function_exists($name)) $shortcuts[] = "{$name}()";
161+
return implode(', ', $shortcuts);
162+
})->all();
163+
array_set($GLOBALS, 'tinx.names', $shortcuts);
164+
165+
/**
166+
* Conditionally render the "Class/Shortcuts" names table.
167+
* */
168+
event('tinx.names.conditional');
+8-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
@if ((bool) array_get($config, 'tableless_models'))
1+
@if ((bool) array_get($config, 'tableless_models') === true)
22
try {
33
${!! $name !!} = app('{!! $class !!}');
44
${!! $name !!}_ = app('{!! $class !!}');
5-
if (!function_exists('{!! $name !!}')) {
6-
function {!! $name !!}(...$args) {
7-
return '{!! $class !!}';
8-
}
9-
}
5+
array_set($GLOBALS, 'tinx.shortcuts.{!! $name !!}', ${!! $name !!});
6+
array_set($GLOBALS, 'tinx.shortcuts.{!! $name !!}_', ${!! $name !!}_);
107
} catch (Throwable $e) {
118
tinx_forget_name('{!! $class !!}');
129
} catch (Exception $e) {
1310
tinx_forget_name('{!! $class !!}');
1411
}
12+
if (!function_exists('{!! $name !!}')) {
13+
function {!! $name !!}(...$args) {
14+
return '{!! $class !!}';
15+
}
16+
}
1517
@else
1618
tinx_forget_name('{!! $class !!}');
1719
@endif

src/Console/NamesTable.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function make(TinxCommand $command)
2020
private function __construct(TinxCommand $command)
2121
{
2222
$this->command = $command;
23-
$this->names = $this->command->getNames();
23+
$this->names = array_get($GLOBALS, 'tinx.names');
2424
}
2525

2626
/**
@@ -102,8 +102,8 @@ private function getHeaders()
102102
* */
103103
private function getRows()
104104
{
105-
return collect($this->names)->map(function ($name, $class) {
106-
return [$class, "\${$name}, \${$name}_, {$name}()"];
105+
return collect($this->names)->map(function ($shortcuts, $class) {
106+
return [$class, $shortcuts];
107107
});
108108
}
109109

src/Console/TinxCommand.php

+4-17
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public function handle()
4747
$this->rebootConfig();
4848
$this->setNames();
4949
$this->createTinxIncludes();
50-
$this->conditionallyRenderNamesTable();
5150
$this->callTinker();
5251
} while (State::shouldRestart() && !$this->info("Reloading your tinker session..."));
5352

@@ -70,20 +69,12 @@ private function setNames()
7069
$this->names = StrategyFactory::makeDefault()->getNames();
7170
}
7271

73-
/**
74-
* @return array
75-
* */
76-
public function getNames()
77-
{
78-
return $this->names;
79-
}
80-
8172
/**
8273
* @return void
8374
* */
8475
private function createTinxIncludes()
8576
{
86-
with(new IncludeManager)->generateIncludesFile($this->getNames());
77+
with(new IncludeManager)->generateIncludesFile($this->names);
8778
}
8879

8980
/**
@@ -94,14 +85,10 @@ private function listenForNamesTable()
9485
app('events')->listen('tinx.names', function (...$args) {
9586
NamesTable::make($this)->render(...$args);
9687
});
97-
}
9888

99-
/**
100-
* @return void
101-
* */
102-
private function conditionallyRenderNamesTable()
103-
{
104-
NamesTable::make($this)->conditionallyRender();
89+
app('events')->listen('tinx.names.conditional', function (...$args) {
90+
NamesTable::make($this)->conditionallyRender();
91+
});
10592
}
10693

10794
/**

0 commit comments

Comments
 (0)