Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobg committed Jun 18, 2020
1 parent 9f34bdb commit e8cb969
Show file tree
Hide file tree
Showing 36 changed files with 1,313 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Please follow the guidelines to contribute.

- Follow PSR-4 and PSR-2 for code style. You can autoformat your code with `composer format`.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Corollarium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
138 changes: 138 additions & 0 deletions Modelarium/Laravel/Console/Commands/ModelariumCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php declare(strict_types=1);

namespace Modelarium\Laravel\Console\Commands;

use Illuminate\Console\Command;
// use Formularium\FrameworkComposer;
// use Formularium\Frontend\Blade\Framework as FrameworkBlade;
// use Formularium\Frontend\Vue\Framework as FrameworkVue;

class ModelariumCommand extends Command
{
/**
* Undocumented variable
*
* @var Formularium\Model
*/
protected $model;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'formularium:scaffold
{name : The model name}
{--framework=* : The frameworks to use}
{--overwrite : overwrite files if they exist}
{--all : make everything}
{--model : make model}
{--controller : make controller}
{--migration : make migration}
{--factory : make factory}
{--seed : make seed}
{--policy : make policy}
{--frontend : make frontend files}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Creates scaffolding using Formularium';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$name = $this->argument('name');
if ($name === '*') {
$path = base_path('app/Formularium/');
$dir = scandir($path);
if ($dir === false) {
$this->error("Cannot find model dir $path");
return 1;
}

$modelNames = array_diff($dir, array('.', '..'));
foreach ($modelNames as $n) {
if (mb_strpos($n, '.php') === false) {
continue;
}
$this->_handle(str_replace('.php', '', $n));
}
} else {
$this->_handle($name);
}
$this->info('Finished. You might want to run `composer dump-autoload`');
}

protected function _handle($name)
{
// TODO
// // setup stuff
// $frameworks = $this->option('framework');
// FrameworkComposer::set($frameworks);

// /**
// * @var FrameworkVue $vue
// */
// $vue = FrameworkComposer::getByName('Vue');
// $blade = FrameworkComposer::getByName('Blade');

if ($this->hasOption('stubdir')) {
$this->stubDir = $this->option('stubdir');
}

// make stuff
if ($this->option('model') || $this->option('all')) {
new ModelGenerator($model)->generate();
}
if ($this->option('migration') || $this->option('all')) {
$this->makeMigration();
}
if ($this->option('factory') || $this->option('all')) {
$this->makeFactory();
}
if ($this->option('seed') || $this->option('all')) {
$this->makeSeed();
}
if ($this->option('controller') || $this->option('all')) {
$this->makeController(($blade ? 'FormulariumControllerBlade' : 'FormulariumControllerAPI'));
}
if ($this->option('policy') || $this->option('all')) {
$this->makePolicy();
}
if ($this->option('frontend') || $this->option('all')) {
if ($vue) {
$this->makeVueScaffold();
$this->makeVue($vue, 'Base', 'viewable');
$this->makeVue($vue, 'Item', 'viewable');
$this->makeVue($vue, 'ListPage', 'viewable');
$this->makeVue($vue, 'ShowPage', 'viewable');
$this->makeVue($vue, 'EditPage', 'editable');
$this->line('Generated Vue');
} elseif ($blade) {
$this->makeBlade($blade, 'show', 'viewable');
$this->makeBlade($blade, 'index', 'viewable');
$this->makeBlade($blade, 'form', 'editable');
$this->line('Generated Blade');
} else {
// TODO: react?
}
}
}
}
12 changes: 12 additions & 0 deletions Modelarium/Laravel/FieldParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace Modelarium\Laravel;

/**
* These are parameters passed to Renderable through Field::extensions.
*/
class FieldParameter
{
const LARAVEL_TYPE = 'laravel:type';
const FOREIGN_KEY = 'foreign';
}
11 changes: 11 additions & 0 deletions Modelarium/Laravel/ModelParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Modelarium\Laravel;

/**
* These are parameters passed to Renderable through Field::extensions.
*/
class ModelParameter
{
const SOFT_DELETES = 'softdeletes';
}
22 changes: 22 additions & 0 deletions Modelarium/Laravel/ModelariumServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace Modelarium\Laravel\ModelariumServiceProvider;

use Illuminate\Support\ServiceProvider;

class ModelariumServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
\Modelarium\Laravel\Console\Commands::class
]);
}
}
}
161 changes: 161 additions & 0 deletions Modelarium/Laravel/Targets/BaseGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php declare(strict_types=1);

namespace Modelarium\Laravel\Targets;

use Doctrine\Inflector\InflectorFactory;
use Exception;
use Illuminate\Support\Str;

abstract class BaseGenerator
{
protected $targetName = '';

protected $studlyName = '';

protected $lowerName = '';

protected $lowerNamePlural = '';

protected $stubDir = __DIR__ . "/stubs/";

protected $inflector = null;

public function __construct($name)
{
$this->inflector = InflectorFactory::create()->build();

$this->targetName = $name;
$this->studlyName = Str::studly($this->targetName);
$this->lowerName = mb_strtolower($this->targetName);
$this->lowerNamePlural = $this->inflector->pluralize($this->lowerName);
}

/**
* Returns the base path (where composer.json is located)
*
* @param string $file The filename
* @return string
*/
public function getBasePath(string $file = null)
{
$basepath = dirname(\Composer\Factory::getComposerFile());
if ($file) {
$basepath .= '/' . $file;
}
return $basepath;
}

abstract public function generate();

/**
* Prints a warning message.
*
* @param string $message
* @return void
*/
protected function warning(string $message)
{
echo 'WARNING: ' . $message;
}

protected function error(string $message)
{
echo 'ERROR: ' . $message;
}

protected function line(string $message)
{
echo $message;
}

/**
* Takes a stub file and generates the target file with replacements.
*
* @param string $targetPath The path for the stub file.
* @param string $stubName The name for the stub file.
* @param boolean $overwrite
* @param Callable $f
* @return void
*/
public function stubFile(string $targetPath, string $stubName, bool $overwrite = true, callable $f = null)
{
if (file_exists($targetPath) && !$overwrite) {
$this->warning("File $targetPath already exists.");
return;
}

mkdir(dirname($targetPath), 0777, true);
$data = $this->stubString($stubName);

$ret = file_put_contents($targetPath, $data);
if (!$ret) {
$this->error("Cannot write to $targetPath");
throw new \Exception("Cannot write to $targetPath");
}
$this->line("Generated $targetPath");
}

/**
* Stubs from a stub file.
*
* @param string $stubName The name for the stub file.
* @param Callable $f
* @return void
* @see BaseGenerator::stubFile()
*/
public function stubString(string $stubName, callable $f = null)
{
$stub = file_get_contents($this->stubDir . "/$stubName.stub.php");
if ($stub === false) {
throw new \Exception('Stub file not found');
}
return $this->stubData($stub, $f);
}

/**
* Stubs a string.
*
* @param string $stub
* @param callable $f
* @return void
*/
public function stubData(string $stub, callable $f = null)
{
$data = $this->replaceDummy($stub);
if ($f) {
$data = $f($data);
}
return $data;
}

/**
* Replaces common strings from the stubs
*
* @param string $str The string data to apply replaces
* @return string
*/
protected function replaceDummy(string $str)
{
$str = str_replace(
'DummyClass',
$this->studlyName,
$str
);
$str = str_replace(
'DummyName',
$this->targetName,
$str
);
$str = str_replace(
'dummynameplural',
$this->lowerNamePlural,
$str
);
$str = str_replace(
'dummyname',
$this->lowerName,
$str
);
return $str;
}
}
12 changes: 12 additions & 0 deletions Modelarium/Laravel/Targets/FactoryGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace Modelarium\Laravel\Targets;

class FactoryGenerator extends BaseGenerator
{
public function generate()
{
$path = $this->getBasePath('database/seeds/'. $this->studlyName . 'Factory.php');
$this->stubFile($path, 'factory');
}
}
Loading

0 comments on commit e8cb969

Please sign in to comment.