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

Commit

Permalink
Updated package for Laravel 6.
Browse files Browse the repository at this point in the history
  • Loading branch information
furey committed Aug 29, 2019
1 parent d1d1379 commit d91b126
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"require": {
"laravel/tinker": "~1.0",
"illuminate/container": "^5.2"
"illuminate/container": "^5.2|^6.0"
},
"require-dev": {
"phpunit/phpunit": "^6.4|^7.0"
Expand Down
19 changes: 10 additions & 9 deletions resources/views/includes.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<php

use Ajthinking\Tinx\Console\State;
use Illuminate\Support\Arr;

/**
* Restarts Tinker.
Expand Down Expand Up @@ -52,7 +53,7 @@ function names(...$args) {
* @return void
* */
function tinx_forget_name($class) {
array_forget($GLOBALS, "tinx.names.$class");
Arr::forget($GLOBALS, "tinx.names.$class");
}
/**
Expand Down Expand Up @@ -124,14 +125,14 @@ function tinx_query($class, ...$args)
* For "first" variable, returns "::first()" if class DB table exists, otherwise "new" (if 'tableless_models' set to true).
* For "last" variable, returns "::latest()->first()" if class DB table exists, otherwise "new" (if 'tableless_models' set to true).
* */
array_set($GLOBALS, 'tinx.names', {!! var_export($names); !!});
$latestColumn = '{{ array_get($config, 'latest_column', 'created_at') }}';
Arr::set($GLOBALS, 'tinx.names', {!! var_export($names); !!});
$latestColumn = '{{ Arr::get($config, 'latest_column', 'created_at') }}';
@foreach ($names as $class => $name)
try {
${!! $name !!} = {!! $class !!}::first() ?: app('{!! $class !!}');
${!! $name !!}_ = {!! $class !!}::latest($latestColumn)->first() ?: app('{!! $class !!}');
array_set($GLOBALS, 'tinx.shortcuts.{!! $name !!}', ${!! $name !!});
array_set($GLOBALS, 'tinx.shortcuts.{!! $name !!}_', ${!! $name !!}_);
Arr::set($GLOBALS, 'tinx.shortcuts.{!! $name !!}', ${!! $name !!});
Arr::set($GLOBALS, 'tinx.shortcuts.{!! $name !!}_', ${!! $name !!}_);
if (!function_exists('{!! $name !!}')) {
function {!! $name !!}(...$args) {
return tinx_query('{!! $class !!}', ...$args);
Expand All @@ -148,19 +149,19 @@ function {!! $name !!}(...$args) {
/**
* Quick names reference array.
* */
$names = array_get($GLOBALS, 'tinx.names');
$names = Arr::get($GLOBALS, 'tinx.names');

/**
* Define shortcuts for "names()" table, and also set quick shortcuts reference array.
* */
$shortcuts = collect($names)->map(function ($name, $class) {
$shortcuts = [];
if (array_has($GLOBALS, "tinx.shortcuts.$name")) $shortcuts[] = "\${$name}";
if (array_has($GLOBALS, "tinx.shortcuts.{$name}_")) $shortcuts[] = "\${$name}_";
if (Arr::has($GLOBALS, "tinx.shortcuts.$name")) $shortcuts[] = "\${$name}";
if (Arr::has($GLOBALS, "tinx.shortcuts.{$name}_")) $shortcuts[] = "\${$name}_";
if (function_exists($name)) $shortcuts[] = "{$name}()";
return implode(', ', $shortcuts);
})->all();
array_set($GLOBALS, 'tinx.names', $shortcuts);
Arr::set($GLOBALS, 'tinx.names', $shortcuts);

/**
* Conditionally render the "Class/Shortcuts" names table.
Expand Down
12 changes: 9 additions & 3 deletions resources/views/on-name-error.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
@if ((bool) array_get($config, 'tableless_models') === true)
<?php
use Illuminate\Support\Arr;
?>

@if ((bool) Arr::get($config, 'tableless_models') === true)
try {
${!! $name !!} = app('{!! $class !!}');
${!! $name !!}_ = app('{!! $class !!}');
array_set($GLOBALS, 'tinx.shortcuts.{!! $name !!}', ${!! $name !!});
array_set($GLOBALS, 'tinx.shortcuts.{!! $name !!}_', ${!! $name !!}_);
Arr::set($GLOBALS, 'tinx.shortcuts.{!! $name !!}', ${!! $name !!});
Arr::set($GLOBALS, 'tinx.shortcuts.{!! $name !!}_', ${!! $name !!}_);
if (!function_exists('{!! $name !!}')) {
function {!! $name !!}(...$args) {
return '{!! $class !!}';
Expand Down
4 changes: 3 additions & 1 deletion src/Console/NamesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Ajthinking\Tinx\Console;

use Illuminate\Support\Arr;

class NamesTable
{
/**
Expand All @@ -20,7 +22,7 @@ public static function make(TinxCommand $command)
private function __construct(TinxCommand $command)
{
$this->command = $command;
$this->names = array_get($GLOBALS, 'tinx.names');
$this->names = Arr::get($GLOBALS, 'tinx.names');
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Includes/IncludeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Ajthinking\Tinx\Includes;

use Illuminate\Support\Str;

class IncludeManager
{
/**
Expand All @@ -14,7 +16,7 @@ public function generateIncludesFile($names)

$contents = view('tinx::includes', compact('names', 'config'))->render();

$contents = str_replace_first('<php', '<?php', $contents);
$contents = Str::replaceFirst('<php', '<?php', $contents);

app('tinx.storage')->put('includes.php', $contents);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Ajthinking\Tinx\Models;

use Illuminate\Support\Str;

class Model
{
/**
Expand All @@ -13,7 +15,7 @@ public function __construct($fullClassName)
$this->fullClassName = $fullClassName;
$this->shortClassName = class_basename($this->fullClassName);
$this->namespace = trim(preg_replace("/{$this->shortClassName}$/", '', $this->fullClassName), '\\');
$this->shortClassNameSlug = str_slug($this->shortClassName);
$this->shortClassNameSlug = Str::slug($this->shortClassName);
$this->shortClassNameWords = $this->getWordsFromString($this->shortClassName);
}

Expand All @@ -23,6 +25,6 @@ public function __construct($fullClassName)
* */
private function getWordsFromString($string)
{
return preg_split('/_|(\d+)/u', snake_case($string), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
return preg_split('/_|(\d+)/u', Str::snake($string), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
}
}
6 changes: 4 additions & 2 deletions src/Models/Models.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Ajthinking\Tinx\Models;

use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class Models extends Collection
{
Expand Down Expand Up @@ -51,7 +53,7 @@ private function getModels()
foreach ($modelFilePaths as $modelFilePath) {
$absoluteFilePath = $this->getAbsoluteFilePath($modelFilePath);
$recursive = false;
if (ends_with($absoluteFilePath, '*')) {
if (Str::endsWith($absoluteFilePath, '*')) {
$absoluteFilePath = rtrim($absoluteFilePath, '/*');
$recursive = true;
}
Expand Down Expand Up @@ -107,7 +109,7 @@ private function getFullClassName($path)
// Fail silently…
}

$namespace = array_get($matches, 1);
$namespace = Arr::get($matches, 1);

if (null === $namespace) {
return null;
Expand Down
4 changes: 3 additions & 1 deletion src/Naming/PascalStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Ajthinking\Tinx\Naming;

use Illuminate\Support\Arr;

class PascalStrategy implements Strategy
{
/**
Expand Down Expand Up @@ -126,7 +128,7 @@ private function getConflictingModel($conflictingName, $causalModel)
$conflictingModel = $causalModel;
$this->dump("Can't set [{$causalModel->fullClassName}] due to forbidden name [$conflictingName].");
} else {
$conflictingModel = array_get($this->names, $conflictingName, $causalModel);
$conflictingModel = Arr::get($this->names, $conflictingName, $causalModel);
$this->dump("Can't set [{$causalModel->fullClassName}] as [$conflictingName] due to [{$conflictingModel->fullClassName}].");
}

Expand Down

0 comments on commit d91b126

Please sign in to comment.