Created
March 21, 2020 04:25
-
-
Save sniper7kills/b56b01a3bbc23541316e695f9935f5f2 to your computer and use it in GitHub Desktop.
Laravel Model Public Names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Contracts; | |
interface HasPublicClassName | |
{ | |
/** | |
* @return string | |
*/ | |
public function getPrimaryPublicName() : string; | |
/** | |
* @return array | |
*/ | |
public function getSecondaryPublicNames() : array; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Helpers; | |
use Illuminate\Support\Facades\Config; | |
class PublicClassName | |
{ | |
private function getPrimaryArray() | |
{ | |
return Config::get('public_class_name.primary', []); | |
} | |
private function getSecondaryArray() | |
{ | |
return Config::get('public_class_name.secondary', []); | |
} | |
public function resolve(string $public) | |
{ | |
$publicLower = strtolower($public); | |
$nameArray = array_merge( | |
$this->getPrimaryArray(), | |
$this->getSecondaryArray(), | |
); | |
if(array_key_exists($publicLower, $nameArray)) | |
return $nameArray[$publicLower]; | |
return $public; | |
} | |
public function toPublic($private) | |
{ | |
if(is_object($private) && method_exists($private,'getPrimaryPublicName')) | |
return strtolower($private->getPrimaryPublicName()); | |
if(is_object($private)) | |
$private = get_class($private); | |
$nameArray = $this->getPrimaryArray(); | |
if(array_search($private, $nameArray)) | |
return strtolower(array_search($private, $nameArray)); | |
return $private; | |
} | |
public function setPrimary(array $names) | |
{ | |
Config::set('public_class_name.primary', array_change_key_case($names, CASE_LOWER)); | |
} | |
public function appendPrimary(array $names) | |
{ | |
$primary = array_merge($this->getPrimaryArray(), $names); | |
$this->setPrimary($primary); | |
} | |
public function setSecondary(array $names) | |
{ | |
Config::set('public_class_name.secondary', array_change_key_case($names, CASE_LOWER)); | |
} | |
public function appendSecondary(array $names) | |
{ | |
$secondary = array_merge($this->getSecondaryArray(), $names); | |
$this->setSecondary($secondary); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Facades\PublicClassName; | |
use Illuminate\Support\Facades\App; | |
use Illuminate\Support\ServiceProvider; | |
class PublicClassNameServiceProvider extends ServiceProvider | |
{ | |
/** @var array */ | |
private $modelDirectories = []; | |
/** @var array */ | |
private $classList = []; | |
/** | |
* Register services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
App::bind('publicclassname', function() | |
{ | |
return new \App\Helpers\PublicClassName; | |
}); | |
} | |
/** | |
* Bootstrap services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
$this->modelDirectories = [ | |
app_path('Models'), | |
app_path('Tenant/Models') | |
]; | |
PublicClassName::appendPrimary($this->getPrimaryModelNames()); | |
PublicClassName::appendSecondary($this->getSecondaryModelNames()); | |
} | |
private function getClasses() | |
{ | |
if(count($this->classList) > 0) | |
return $this->classList; | |
foreach($this->modelDirectories as $directory) | |
foreach($this->getClassesList($directory) as $item) | |
$this->classList[] = $item->classname; | |
return $this->classList; | |
} | |
private function getPrimaryModelNames() | |
{ | |
$primary = []; | |
foreach($this->getClasses() as $item) | |
{ | |
if(!method_exists($item, 'getPrimaryPublicName')) | |
continue; | |
$primary[strtolower((new $item)->getPrimaryPublicName())] = (new $item)->getMorphClass(); | |
} | |
return $primary; | |
} | |
private function getSecondaryModelNames() | |
{ | |
$secondary = []; | |
foreach($this->getClasses() as $item) | |
{ | |
if(!method_exists($item, 'getSecondaryPublicNames')) | |
continue; | |
foreach((new $item)->getSecondaryPublicNames() as $secondaryPublicName) | |
$secondary[strtolower($secondaryPublicName)] = (new $item)->getMorphClass(); | |
} | |
return $secondary; | |
} | |
/** | |
* Src: https://stackoverflow.com/questions/31837075/laravel-get-list-of-models | |
* | |
* @param $dir | |
* @return mixed | |
*/ | |
private function getClassesList($dir) | |
{ | |
$classes = \File::allFiles($dir); | |
foreach ($classes as $class) { | |
$class->classname = str_replace( | |
[app_path(), '/', '.php'], | |
['App', '\\', ''], | |
$class->getRealPath() | |
); | |
} | |
return $classes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment