Skip to content

Commit

Permalink
Fixed datatabases and added scalffold functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
hsemix committed Aug 15, 2020
1 parent 225b6cc commit d16f8a5
Show file tree
Hide file tree
Showing 38 changed files with 957 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Yuga/Authenticate/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function login($username, $password, $remember = null)

public function logout()
{
event('on:logout', ['user' => $this->user()]);
event('on:signout', ['user' => $this->user()]);
if (!is_null($this->user()->remember_token) && $this->cookie->exists($this->settings->get('remember.name'))) {
$this->user()->save([
'remember_token' => null
Expand Down
8 changes: 8 additions & 0 deletions src/Yuga/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class Command extends ConsoleCommand
*/
protected $description;

/**
* The consold command Help text
*/
protected $help;

/**
* Create a new console command instance.
*
Expand All @@ -62,6 +67,9 @@ public function __construct()
// so they don't have to all be manually specified in the constructors.
$this->setDescription($this->description);

// We set the help text if provided
$this->setHelp($this->help);

$this->specifyParameters();
}

Expand Down
53 changes: 46 additions & 7 deletions src/Yuga/Database/Console/MakeMigrationCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

namespace Yuga\Database\Console;

use Yuga\Console\Command;
use Yuga\Scaffold\Scaffold;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

Expand All @@ -22,37 +24,74 @@ class MakeMigrationCommand extends Command
* @return void
*/
public function handle()
{
$this->processMigration();
$this->info('migration table class created successfully.');
}

/**
* Process Migration
*
* @param string|null $name
*/
public function processMigration($name = null, array $scaffold = null)
{
$this->createDirectories();
file_put_contents(
path('database/migrations/' . $this->formatName() . '.php'),
$this->compileMigrationTemp(trim($this->argument('name')))
path('database/migrations/' . $this->formatName($name) . '.php'),
$this->compileMigrationTemp(trim($name ? $name : $this->argument('name')), $scaffold)
);

file_put_contents(
path('config/migrations.php'),
$this->compileYugaMigrationsTemp(trim($this->argument('name')))
$this->compileYugaMigrationsTemp(trim($name ? $name : $this->argument('name')))
);
$this->info('migration table class created successfully.');
}

protected function compileMigrationTemp($model)
protected function compileMigrationTemp($model, array $scaffold = null)
{
$table = strtolower($model);
$migration = str_replace('{table}', $table, file_get_contents(__DIR__.'/temps/Migration.temp'));
if ($scaffold) {
if (count($scaffold) > 0)
$migration = $this->processMigrationTemp($table, $scaffold);
}

return str_replace(
'{class}',
'Create'.ucfirst($model).'Table',
$migration
);
}

protected function processMigrationTemp($table, array $fields = [])
{
$scaffold = '';
$i = 0;
foreach ($fields as $field => $type) {
$dataType = $this->processFieldType($type);

if ($i != (count($fields) - 1))
$scaffold .= "$" . "table->column('" . $field . "')->" . $dataType . "->nullable();\n\t\t\t";
else
$scaffold .= "$" . "table->column('" . $field . "')->" . $dataType . "->nullable();";
$i++;
}

return str_replace(['{table}', '{scaffold_fields}'], [$table, $scaffold], file_get_contents(__DIR__.'/temps/MigrationScaffold.temp'));;
}

protected function processFieldType($type)
{
return Scaffold::getMethod($type) == 'string' ? Scaffold::getMethod($type) . '(255)' : Scaffold::getMethod($type) . '()';
}

/**
* Format the name of the table given i.e. deCamalize it
*/
protected function formatName()
protected function formatName($name = null)
{
$table = trim($this->argument('name'));
$table = trim($name ? $name : $this->argument('name'));
$table = (new \DateTime)->format('YmdHis') . '_create_' . strtolower($table) . '_table';
return $table;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Yuga/Database/Console/temps/Migration.temp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class {class} extends Migration
{
/**
* This method contains the entire schema of the table you want to create
* I'ts what the php yuga migration:up runs
* It's what the php yuga migration:up runs
*
* @param null
*
Expand Down
49 changes: 49 additions & 0 deletions src/Yuga/Database/Console/temps/MigrationScaffold.temp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Yuga\Database\Migration\Migration;
use Yuga\Database\Migration\Schema\Table;

class {class} extends Migration
{
/**
* This method contains the entire schema of the table you want to create
* It's what the php yuga migration:up runs
*
* @param null
*
* @return null
*/
public function up()
{
$this->schema->create('{table}', function (Table $table) {
$table->column('id')->bigint()->primary()->increment();
{scaffold_fields}
$table->timestamps();
});
}

/**
* When php yuga migration:down is run, the method will be run
*
* @param null
*
* @return null
*/
public function down()
{
$this->schema->dropIfExists('{table}');
}

/**
* When php yuga migration:seed is run, this method will run,
* Put here what records you want to intialize the table
*
* @param null
*
* @return null
*/
public function seeder()
{

}
}
3 changes: 3 additions & 0 deletions src/Yuga/Database/Elegant/Association/Association.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Yuga\Database\Elegant\Association;

use Closure;
Expand All @@ -12,6 +13,7 @@ abstract class Association implements Relation
private $parent;
private $child;
static $conditions;

public function __construct(Builder $query, Model $parent)
{
$this->query = $query;
Expand All @@ -36,6 +38,7 @@ public function __call($method, $parameters)

return $result;
}

public function noConditions(Closure $callback)
{
return call_user_func($callback);
Expand Down
1 change: 1 addition & 0 deletions src/Yuga/Database/Elegant/Association/BelongsTo.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Yuga\Database\Elegant\Association;

use Yuga\Database\Elegant\Model;
Expand Down
1 change: 1 addition & 0 deletions src/Yuga/Database/Elegant/Association/BelongsToMany.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Yuga\Database\Elegant\Association;

use Yuga\Database\Elegant\Model;
Expand Down
8 changes: 7 additions & 1 deletion src/Yuga/Database/Elegant/Association/HasMany.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Yuga\Database\Elegant\Association;

use Yuga\Database\Elegant\Model;
Expand All @@ -7,7 +8,6 @@

class HasMany extends Association
{

private $child;
private $query;
private $parent;
Expand All @@ -28,14 +28,17 @@ public function getChild()
{
return $this->child;
}

public function getParent()
{
return $this->parent;
}

public function addConditions()
{
$this->query->where($this->foreignKey, '=', $this->getParentIdValue());
}

public function getParentIdValue()
{
return $this->parent->getAttribute($this->otherKey);
Expand All @@ -53,6 +56,7 @@ public function getPlainForeignKey()
$foreign = explode(".", $this->foreignKey);
return end($foreign);
}

public function saveMany($models)
{
foreach ($models as $model) {
Expand All @@ -61,6 +65,7 @@ public function saveMany($models)

return $models;
}

public function bootRelation(array $models, $relation)
{

Expand All @@ -71,6 +76,7 @@ public function bootRelation(array $models, $relation)

return $models;
}

public function match(array $models, Collection $results, $relation)
{
return $this->matchMany($models, $results, $relation);
Expand Down
2 changes: 1 addition & 1 deletion src/Yuga/Database/Elegant/Association/HasOne.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Yuga\Database\Elegant\Association;

use Yuga\Database\Elegant\Model;
use Yuga\Database\Elegant\Builder;

class HasOne extends Association
{

private $child;
private $query;
private $parent;
Expand Down
1 change: 1 addition & 0 deletions src/Yuga/Database/Elegant/Association/Mergeable.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Yuga\Database\Elegant\Association;

use Yuga\Database\Elegant\Model;
Expand Down
1 change: 1 addition & 0 deletions src/Yuga/Database/Elegant/Association/MergeableMany.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Yuga\Database\Elegant\Association;

use Yuga\Database\Elegant\Model;
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 ((bool)$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 ((bool)$column['orderable'] === 'true') {
if (!in_array($column['data'], $this->getModel()->bootable))
$orderable[$column['data']] = $filter['dir'];
}
Expand Down
2 changes: 2 additions & 0 deletions src/Yuga/Events/Auth/Registered.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Yuga\Events\Auth;

use Yuga\Models\User;
Expand All @@ -7,6 +8,7 @@
class Registered extends Event
{
public $user;

public function __construct(User $user)
{
$this->user = $user;
Expand Down
4 changes: 4 additions & 0 deletions src/Yuga/Events/Dispatcher/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function getParams()
{
return $this->params;
}

/**
* Get an individual parameter
*
Expand All @@ -103,6 +104,7 @@ public function getParam($name, $default = null)
return $default;
}
}

/**
* Set the event name
*
Expand All @@ -115,6 +117,7 @@ public function setName($name)
$this->name = (string)$name;
return $this;
}

/**
* Set an individual parameter to a value
*
Expand All @@ -135,6 +138,7 @@ public function __get($key)
{
return $this->getAttribute($key);
}

/**
* Set a variable and make an object point to it
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Yuga/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ public function attach($eventName, $callback = null, $priority = 1)
}
return $this;
}

/**
* Allias to the attach method
*
* @param string $eventName
* @param callable $callback|null
* @param int $priority
*
* @return void
*/
public function on($eventName, $callback = null, $priority = 1)
{
return $this->attach($eventName, $callback, $priority);
}

/**
* Some times the name provided in the attach method might be an instance of the HandlerInterface
* When that happens, Make sure the $event has the handle method
Expand Down
Loading

0 comments on commit d16f8a5

Please sign in to comment.