Skip to content

Commit

Permalink
use Str facade instead of str helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
secmohammed committed Sep 15, 2019
1 parent 9e52d4f commit 59457ff
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/Tenant/Database/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Artify\Artify\Contracts\Models\Tenant;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
use Str;

class DatabaseManager
{
Expand Down Expand Up @@ -50,7 +51,7 @@ protected function getTenantConnection(Tenant $tenant)
}
protected function prepareTenantConnection($tenant)
{
return str_contains($this->getConfigConnectionPath(), 'sqlite') ?
return Str::contains($this->getConfigConnectionPath(), 'sqlite') ?
['database' => database_path($tenant->tenantConnection->database . '.sqlite')] : $tenant->tenantConnection->only('database');
}
public function hasDatabase(Tenant $tenant)
Expand Down
5 changes: 3 additions & 2 deletions src/Traits/Roles/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Artify\Artify\Traits\Roles;

use Artify\Artify\Exceptions\InsufficientPermissionsException;
use Str;

trait Roles
{
Expand Down Expand Up @@ -51,12 +52,12 @@ public function hasRole($role)
return true;
}
foreach ($roles as $key => $value) {
if ((str_is($role, $key) || str_is($key, $role)) && $value === true) {
if ((Str::is($role, $key) || Str::is($key, $role)) && $value === true) {
return true;
}
}
foreach ($secondary_roles as $key => $value) {
if ((str_is($role, $key) || str_is($key, $role)) && $value === true) {
if ((Str::is($role, $key) || Str::is($key, $role)) && $value === true) {
return true;
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/artifies/ADRCommandGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Str;

class ADRCommandGenerator extends Command
{
Expand Down Expand Up @@ -42,10 +43,10 @@ public function handle()
$this->setFiles();
foreach ($this->files as $filename) {
if (in_array($filename, $this->requiresDomain)) {
$customName = str_singular($this->domain);
$this->hasOrCreateDirectory($this->domain . '/Domain/' . str_plural(explode('.', $filename)[0]));
$customName = Str::singular($this->domain);
$this->hasOrCreateDirectory($this->domain . '/Domain/' . Str::plural(explode('.', $filename)[0]));
} else {
$this->hasOrCreateDirectory($this->domain . '/' . str_plural(explode('.', $filename)[0]));
$this->hasOrCreateDirectory($this->domain . '/' . Str::plural(explode('.', $filename)[0]));
$customName = null;
}
if ($originalContent = $this->hasOrFetchDummyFile($filename)) {
Expand All @@ -64,9 +65,9 @@ public function handle()
]);
} else {
if ($filename == 'Service.stub') {
$fileContent = str_replace(['DummyDomain', 'DummyName', 'Dummy', 'dummies', 'dummy'], [$this->domain, $this->name, str_singular($this->domain), strtolower($this->domain), strtolower(str_singular($this->domain))], $originalContent);
$fileContent = str_replace(['DummyDomain', 'DummyName', 'Dummy', 'dummies', 'dummy'], [$this->domain, $this->name, Str::singular($this->domain), strtolower($this->domain), strtolower(Str::singular($this->domain))], $originalContent);
} else {
$fileContent = str_replace(['DummyDomain', 'Dummy', 'dummies', 'dummy'], [$this->domain, $customName ?? $this->name, strtolower(str_plural($customName ?? $this->name)), strtolower($customName ?? $this->name)], $originalContent);
$fileContent = str_replace(['DummyDomain', 'Dummy', 'dummies', 'dummy'], [$this->domain, $customName ?? $this->name, strtolower(Str::plural($customName ?? $this->name)), strtolower($customName ?? $this->name)], $originalContent);
}
$this->filesystem->put(__DIR__ . '/stubs/adr/' . $filename, $fileContent);
$this->filesystem->copy(__DIR__ . '/stubs/adr/' . $filename, $this->getMovingFilePath($filename));
Expand All @@ -82,15 +83,15 @@ protected function getMovingFilePath($filename)
if ($filename == 'Service.stub') {
$customName = $this->name . $stubName[0];
} else {
$customName = str_singular($this->domain);
$customName = Str::singular($this->domain);
if ($filename != 'Model.stub') {
$customName .= $stubName[0];
}
}
return (app_path($this->domain . '/Domain/' . str_plural($stubName[0]) . '/' . $customName . '.php'));
return (app_path($this->domain . '/Domain/' . Str::plural($stubName[0]) . '/' . $customName . '.php'));
}
$customName = $this->name . $stubName[0];
return app_path($this->domain . '/' . str_plural($stubName[0]) . '/' . $customName . '.php');
return app_path($this->domain . '/' . Str::plural($stubName[0]) . '/' . $customName . '.php');
}
protected function hasOrFetchDummyFile($file)
{
Expand Down
5 changes: 3 additions & 2 deletions src/artifies/GenerateCrudCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Str;

class GenerateCrudCommand extends Command
{
Expand Down Expand Up @@ -66,11 +67,11 @@ public function handle()
}
$runtimeControllerContent = str_replace(["cache()->forget('dummies');\n", "cache('dummies')", 'cache()->remember(\'dummies\', config(\'artify.cache.duration\'), function () {
$dummies = ' . $layerName . '::latest()->get();
});'], ['', '$dummies', $assignedLayer ?? '$dummies = Dummy::latest()->get();'], $runtimeControllerContent ?? $defaultControllerContent);
});', ], ['', '$dummies', $assignedLayer ?? '$dummies = Dummy::latest()->get();'], $runtimeControllerContent ?? $defaultControllerContent);
}
$runtimeControllerContent = str_replace(
['dummy', 'Dummy', 'dummies'],
[strtolower($model), ucfirst($model), strtolower(str_plural($model))],
[strtolower($model), ucfirst($model), strtolower(Str::plural($model))],
$runtimeControllerContent ?? $defaultControllerContent
);
$this->filesystem->put(artify_path('artifies/stubs/DummyController.stub'), $runtimeControllerContent);
Expand Down
3 changes: 2 additions & 1 deletion src/artifies/ObserverMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Console\ObserverMakeCommand as BasicObserverMakeCommand;
use Str;

class ObserverMakeCommand extends BasicObserverMakeCommand
{
Expand Down Expand Up @@ -52,7 +53,7 @@ public function handle()

if ($this->files->exists(app_path('/Providers/EloquentEventServiceProvider.php')) && $this->option('model')) {
$provider = $this->files->get(app_path('/Providers/EloquentEventServiceProvider.php'));
$replacement = str_replace_last(
$replacement = Str::replaceLast(
'class);',
"class);\n\t\t{$this->option('model')}::observe($name::class);",
$provider
Expand Down
15 changes: 8 additions & 7 deletions src/artifies/RegisterAuthorizationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Console\DetectsApplicationNamespace;
use Illuminate\Filesystem\Filesystem;
use Str;

class RegisterAuthorizationCommand extends Command
{
Expand Down Expand Up @@ -58,7 +59,7 @@ public function handle()
$domains = collect(config('artify.adr.domains'))->mapToDictionary(function ($domain) use ($permissions) {
return [
$domain => $permissions->filter(function ($value, $permission) use ($domain) {
return str_contains($permission, strtolower(str_singular($domain)));
return Str::contains($permission, strtolower(Str::singular($domain)));
}),
];
});
Expand All @@ -79,7 +80,7 @@ protected function preparePermissions($permissions)
{
$this->permissions = $permissions->mapToDictionary(function ($value, $key) {
$key = explode('-', $key);
return [str_plural($key[1]) => $key[0]];
return [Str::plural($key[1]) => $key[0]];
});
}
protected function registerServiceProvider()
Expand Down Expand Up @@ -125,7 +126,7 @@ protected function buildClass()
{
$this->appendPolicies()->appendGates();
foreach ($this->permissions as $permission => $value) {
$model = str_singular(ucfirst($permission));
$model = Str::singular(ucfirst($permission));
$this->replacePolicies($model)->replaceGates($model, $value);
$this->generatePolicyStub($model, $value);
}
Expand Down Expand Up @@ -198,7 +199,7 @@ protected function getDomainPolicyNamespace()
protected function replaceModelNamespace($model)
{
return $this->setContent(
str_replace_first(
Str::replaceFirst(
'App\DummyModel',
$this->getDomainModelNamespace() . ucfirst($model),
$this->getContent()
Expand All @@ -208,7 +209,7 @@ protected function replaceModelNamespace($model)
protected function replacePolicies($model)
{
return $this->setContent(
str_replace_first(
Str::replaceFirst(
"App\\DummyModel::class => App\\Policies\\DummyPolicy::class",
$this->getDomainModelNamespace() . "{$model}::class => " . $this->getDomainPolicyNamespace() . "{$model}Policy::class",
$this->getContent()
Expand All @@ -219,14 +220,14 @@ protected function replaceGates($model, $permissions)
{
foreach ($permissions as $permission) {
$access = $permission . '-' . lcfirst($model);
$this->setContent(str_replace_first("Gate::define(\'dummy-access\',\'\App\Policies\DummyPolicy@DummyAction\');\n", "Gate::define('$access',\"{$this->getDomainPolicyNamespace()}{$model}Policy@$permission\");\n", $this->getContent()));
$this->setContent(Str::replaceFirst("Gate::define(\'dummy-access\',\'\App\Policies\DummyPolicy@DummyAction\');\n", "Gate::define('$access',\"{$this->getDomainPolicyNamespace()}{$model}Policy@$permission\");\n", $this->getContent()));
}
return $this;
}
protected function generatePolicyStub($model, $permissions)
{
$originalContent = $this->filesystem->get(artify_path('artifies/stubs/Policy.stub'));
if (str_contains($model, 'User')) {
if (Str::contains($model, 'User')) {
$content = str_replace(['use NamespacedDummyModel;', ', DummyModel $dummyModel'], '', $originalContent);
} else {
$content = str_replace('use NamespacedDummyModel;', 'use ' . $this->getDomainModelNamespace() . ucfirst($model) . ';', $originalContent);
Expand Down
3 changes: 2 additions & 1 deletion src/artifies/RepositoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Str;

class RepositoryMakeCommand extends Command
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public function __construct(Filesystem $filesystem)
public function handle()
{
$name = $this->argument('name');
if (str_contains($name, '\\')) {
if (Str::contains($name, '\\')) {
$location = $this->filesystem->transformNamespaceToLocation($name);
$filename = $this->filesystem->getFileName($name);
} else {
Expand Down

0 comments on commit 59457ff

Please sign in to comment.