Skip to content

Commit

Permalink
Finished scaffold feature for mvc
Browse files Browse the repository at this point in the history
  • Loading branch information
hsemix committed Dec 10, 2020
1 parent d16f8a5 commit 2ac7e34
Show file tree
Hide file tree
Showing 31 changed files with 461 additions and 52 deletions.
7 changes: 1 addition & 6 deletions src/Yuga/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

class Application extends Container implements IApplication
{
const VERSION = '3.2.0';
const VERSION = '3.3.0';
const CHARSET_UTF8 = 'UTF-8';

/**
Expand Down Expand Up @@ -417,9 +417,4 @@ public function terminate()
{
exit(0);
}

public function __destruct()
{
$this->get('events')->dispatch('on:app-stop');
}
}
2 changes: 1 addition & 1 deletion src/Yuga/Authenticate/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function guest()
public function run(Request $request, Closure $next)
{
if ($this->guest()) {
return (\Auth::authRoutesExist()) ? $this->response->redirect->route('login') : $this->response->redirect->to(env('DEFAULT_LOGIN_REDIRECT', '/login'));
return (\Auth::authRoutesExist()) ? $this->response->redirect->route('login') : $this->response->redirect->to(env('DEFAULT_LOGIN_REDIRECT', route('/login')));
die();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Yuga/Authenticate/LoginWithRemember.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function __construct(Session $session, Cookie $cookie, User $user, Applic
public function run(Request $request, Closure $next)
{
$settings = $this->app->config->load('config.Settings');
if($this->cookie->exists($settings->get('remember.name')) && !$this->session->isLoggedIn()){
if($this->cookie->exists($settings->get('remember.name')) && !$this->session->isLoggedIn()) {
$hash = $this->cookie->get($settings->get('remember.name'));
if($hashCheck = $this->user->where('remember_token', $hash)->first()){
if($hashCheck = $this->user->where('remember_token', $hash)->first()) {
$this->session->login($hashCheck);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Yuga/Database/Elegant/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ public function datatable($length = 10, $draw = null, $pageName = 'draw', $pathN
$orderable = [];

foreach ($columns as $column) {
if ((bool)$column['searchable'] === 'true') {
if ($column['searchable'] === 'true') {
if (!in_array($column['data'], $this->getModel()->bootable))
$searchable[] = $column['data'];
}
Expand All @@ -1067,7 +1067,7 @@ public function datatable($length = 10, $draw = null, $pageName = 'draw', $pathN
if (count($orderBy) > 0) {
foreach ($orderBy as $filterI => $filter) {
$column = $columns[$filter['column']];
if ((bool)$column['orderable'] === 'true') {
if ($column['orderable'] === 'true') {
if (!in_array($column['data'], $this->getModel()->bootable))
$orderable[$column['data']] = $filter['dir'];
}
Expand Down
43 changes: 39 additions & 4 deletions src/Yuga/Database/Elegant/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,17 +732,52 @@ public function newFromQuery($attributes = [], array $bootable = null, array $it
}

if (count($this->cast) > 0) {
foreach ($model->cast as $attribute => $type) {
if (in_array($attribute, array_keys($model->attributes))) {
$attributeData = $model->attributes[$attribute];
$model = $this->castTo($model);
}
return $model;
}

protected function castTo(Model $model)
{
foreach ($model->cast as $attribute => $type) {
if (in_array($attribute, array_keys($model->attributes))) {
$attributeData = $model->attributes[$attribute];

if ($type == 'array') {
$attributeData = $this->castToArray($attributeData);
} else if ($type == 'json' || $type == 'json-array') {
$attributeData = $this->castToJson($attributeData);
} else {
settype($attributeData, $type);
$model->setAttribute($attribute, $attributeData);
}
$model->setAttribute($attribute, $attributeData);
}
}

return $model;
}

protected function isJson(?string $string = null)
{
json_decode($string ?? null);
return json_last_error() === JSON_ERROR_NONE;
}

protected function castToArray(string $string)
{
if ($this->isJson($string))
$string = json_decode($string, true);
else
$string = (array)$string;

return $string;
}

protected function castToJson($array)
{
return json_encode($array);
}

/**
* Invoke funtions or return strings or arrays that functions return
*
Expand Down
24 changes: 24 additions & 0 deletions src/Yuga/Database/Migration/MigrationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ protected function rollBackMigrations($migrations)
}
}

public function seed(Application $app)
{
$config = $app->config->load('config.migrations');

if ($app->runningInConsole()) {
if (count($config->get('migrate')) > 0) {
foreach (glob($this->getMigrationPath()."*.php") as $migration) {
require_once $migration;
}
$this->runSeeders($config->get('migrate'));
}
}
}

protected function runSeeders($migrations)
{
foreach ($migrations as $migration) {
if (class_exists($migration)) {
$migration = new $migration;
$migration->seeder();
}
}
}

protected function getMigrationPath()
{
return path().'database'.DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR;
Expand Down
17 changes: 13 additions & 4 deletions src/Yuga/Database/Migration/Schema/Pgsql/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Column

const INDEX_PRIMARY = 'PRIMARY KEY';
const INDEX_UNIQUE = 'UNIQUE';
const INDEX_INDEX = 'INDEX';
const INDEX_INDEX = null;
const INDEX_FULLTEXT = 'FULLTEXT';

const RELATION_TYPE_RESTRICT = 'RESTRICT';
Expand All @@ -31,8 +31,9 @@ class Column
const RELATION_TYPE_NO_ACTION = 'NO ACTION';

const TYPE_VARCHAR = 'VARCHAR';
const TYPE_LONGTEXT = 'LONGTEXT';
const TYPE_LONGTEXT = 'TEXT';
const TYPE_TEXT = 'TEXT';
const TYPE_TEXT_ARRAY = 'TEXT[]';
const TYPE_MEDIUMTEXT = 'MEDIUMTEXT';
const TYPE_TINYTEXT = 'TINYTEXT';
const TYPE_INTEGER = 'INTEGER';
Expand Down Expand Up @@ -81,6 +82,7 @@ class Column
self::TYPE_VARCHAR,
self::TYPE_LONGTEXT,
self::TYPE_TEXT,
self::TYPE_TEXT_ARRAY,
self::TYPE_MEDIUMTEXT,
self::TYPE_TINYTEXT,
self::TYPE_INTEGER,
Expand Down Expand Up @@ -227,7 +229,7 @@ public function setIndex($index)

public function getIndex()
{
return $this->index;
return $this->index ? : null;
}

public function setIncrement($increment)
Expand Down Expand Up @@ -373,6 +375,13 @@ public function longtext()
return $this;
}

public function textarray()
{
$this->setType(self::TYPE_TEXT_ARRAY);

return $this;
}

public function datetime()
{
$this->setType(self::TYPE_DATETIME);
Expand Down Expand Up @@ -474,7 +483,7 @@ public function getQuery($includeRelations = true)

$query = sprintf('"%s" %s%s %s', $this->getName(), $this->getType(), $length, $this->getAttributes());

$query .= (!$this->getNullable()) ? 'NOT null' : 'null';
$query .= (!$this->getNullable()) ? 'NOT Null' : 'DEFAULT Null';

if ($this->getDefaultValue()) {
$query .= PdoHelper::formatQuery(' DEFAULT %s', [$this->getDefaultValue()]);
Expand Down
39 changes: 34 additions & 5 deletions src/Yuga/Models/Console/CanCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,35 @@

trait CanCreate
{
/**
* Make the scaffold for a create form
*
* @param \Yuga\Database\Elegant\Model $model
* @param mixed
*/
protected function makeCreateForm(Model $model)
{
$name = \class_base($model);
$inputs = "";
$property = \strtolower($name);
$isEditor = false;
$editors = 0;

foreach ($model->scaffold as $fieldName => $type) {
$fieldType = Scaffold::getFormType($type);
$input = '<input name="'. $fieldName .'" type="'. $fieldType .'" class="form-control" />';
$input = '<input name="'. $fieldName .'" id="' . $fieldName . '" type="'. $fieldType .'" class="form-control" />';

if ($fieldType != 'password') {
$input = '<input name="'. $fieldName .'" type="'. $fieldType .'" value="{{ old(\''. $fieldName .'\') }}" class="form-control" />';
$input = '<input name="'. $fieldName .'" id="' . $fieldName . '" type="'. $fieldType .'" value="{{ old(\''. $fieldName .'\') }}" class="form-control" />';
}
if ($fieldType == 'textarea') {
$input = '<textarea name="'. $fieldName .'" cols="10" rows="4" class="form-control"></textarea>';
$input = '<textarea name="'. $fieldName .'" id="' . $fieldName . '" cols="10" rows="4" class="form-control" placeholder="Type Something..."></textarea>';
}

if ($fieldType == 'editor') {
$isEditor = true;
$input = '<textarea name="'. $fieldName .'" id="editor" cols="10" rows="4" class="form-control editor"></textarea>';
$editors += 1;
}

$label = \ucfirst($fieldName);
Expand All @@ -33,14 +48,28 @@ protected function makeCreateForm(Model $model)
@endif
</div>' . "\n\t\t\t\t";
}

$script = "";
if ($isEditor) {
$element = ($editors > 1) ? ".editor" : "#editor";

$script = "
<script src=\"https://slmta.org/assets/js/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js\"></script>
<script type=\"\"text/javascript\"\">
$(function() {
$('" . $element . "').wysihtml5();
});
</script>
";
}

$directory = path('resources/views/' . $property);
if (!is_dir($directory)) {
mkdir($directory, 0755, true);
}
$creator = str_replace(
['{title}', '{inputs}', '{form-title}', '{route}'],
[$name, $inputs, 'Create', Inflect::pluralize($property)],
['{title}', '{inputs}', '{form-title}', '{route}', '{scripts}'],
[$name, $inputs, 'Create', Inflect::pluralize($property), $script],
file_get_contents(__DIR__.'/temps/scaffold/create-form.temp')
);
$fileName = $directory . '/create.hax.php';
Expand Down
44 changes: 43 additions & 1 deletion src/Yuga/Models/Console/CanDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,49 @@

namespace Yuga\Models\Console;

use Yuga\Support\Inflect;
use Yuga\Scaffold\Scaffold;
use Yuga\Database\Elegant\Model;

trait CanDelete
{

/**
* Make the scaffold for a delete form
*
* @param \Yuga\Database\Elegant\Model $model
* @param mixed
*/
public function makeDeleteForm(Model $model)
{
$name = \class_base($model);
$inputs = "";
$property = \strtolower($name);
foreach ($model->scaffold as $fieldName => $type) {

$fieldType = Scaffold::getFormType($type);

$label = \ucfirst($fieldName);
if ($fieldType != 'password') {
$inputs .= '<div class="col-sm-2">'. str_replace('_', ' ', $label) .'</div> <div class="col-sm-10">{{ $' . $property . '->' . $fieldName . ' }}</div>' . "\n\t\t\t";
}
}

$directory = path('resources/views/' . $property);
if (!is_dir($directory)) {
mkdir($directory, 0755, true);
}
$creator = str_replace(
['{title}', '{form}', '{model-id}', '{route}'],
[$name, $inputs, '$' . $property . '->' . $model->getPrimaryKey(), Inflect::pluralize($property)],
file_get_contents(__DIR__.'/temps/scaffold/delete.temp')
);
$fileName = $directory . '/delete.hax.php';
if (file_exists($fileName) && !$this->option('force')) {
if ($this->confirm("The [{$fileName}] view already exists. Do you want to replace it?")) {
file_put_contents($fileName, $creator);
}
} else {
file_put_contents($fileName, $creator);
}
}
}
6 changes: 6 additions & 0 deletions src/Yuga/Models/Console/CanDisplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

trait CanDisplay
{
/**
* Make the scaffold for a Index page
*
* @param \Yuga\Database\Elegant\Model $model
* @param mixed
*/
protected function makeIndexForm(Model $model)
{
$name = \class_base($model);
Expand Down
9 changes: 6 additions & 3 deletions src/Yuga/Models/Console/CanShowDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@

trait CanShowDetails
{
/**
* Make the scaffold for a details form
*
* @param \Yuga\Database\Elegant\Model $model
*/
protected function makeDetailsForm(Model $model)
{
$name = \class_base($model);
$inputs = "";
$property = \strtolower($name);
foreach ($model->scaffold as $fieldName => $type) {



$fieldType = Scaffold::getFormType($type);

$label = \ucfirst($fieldName);
if ($fieldType != 'password') {
$inputs .= '<dt class="col-sm-2">'. str_replace('_', ' ', $label) .'</dt> <dd class="col-sm-10">{{ $' . $property . '->' . $fieldName . ' }}</dd>' . "\n\t\t\t";
$inputs .= '<div class="col-sm-2">'. str_replace('_', ' ', $label) .'</div> <div class="col-sm-10">{{ $' . $property . '->' . $fieldName . ' }}</div>' . "\n\t\t\t";
}
}

Expand Down
Loading

0 comments on commit 2ac7e34

Please sign in to comment.