Skip to content

Commit

Permalink
add Actions
Browse files Browse the repository at this point in the history
move exportTrait to ExportAction
refactoring
minor fixes
  • Loading branch information
lee-to committed May 15, 2022
1 parent 8b7529c commit 14b7716
Show file tree
Hide file tree
Showing 44 changed files with 506 additions and 374 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"require-dev": {
"fakerphp/faker": "^1.9.2",
"phpunit/phpunit": "^9.5.8",
"mockery/mockery": "^1.4.4"
"mockery/mockery": "^1.4.4",
"phpstan/phpstan": "^1.4.7"
},
"autoload": {
"psr-4": {
Expand Down
47 changes: 47 additions & 0 deletions src/Actions/BaseAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Leeto\MoonShine\Actions;

use Leeto\MoonShine\Contracts\Actions\ActionContract;
use Leeto\MoonShine\Contracts\Resources\ResourceContract;

class BaseAction
{
protected string $label;

protected ResourceContract|null $resource;

public static function make(...$arguments): static
{
return new static(...$arguments);
}

final public function __construct(string $label)
{
$this->setLabel($label);
}

public function label(): string
{
return $this->label;
}

public function setLabel(string $label): static
{
$this->label = $label;

return $this;
}

public function resource(): ResourceContract|null
{
return $this->resource;
}

public function setResource(ResourceContract $resource): static
{
$this->resource = $resource;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,33 @@
<?php

namespace Leeto\MoonShine\Traits\Resources;
namespace Leeto\MoonShine\Actions;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
use Leeto\MoonShine\Contracts\Actions\ActionContract;
use Leeto\MoonShine\Exceptions\ActionException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Exception;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

trait ExportTrait
class ExportAction extends BaseAction implements ActionContract
{
public function exportRoute(): string
/**
* @throws Exception|ActionException
*/
public function handle(): Response|Application|ResponseFactory
{
$query = ['exportCsv' => true];

if(request()->has('filters')) {
foreach (request()->query('filters') as $filterField => $filterQuery) {
if(is_array($filterQuery)) {
foreach ($filterQuery as $filterInnerField => $filterValue) {
if(is_numeric($filterInnerField) && !is_array($filterValue)) {
$query['filters'][$filterField][] = $filterValue;
} else {
$query['filters'][$filterInnerField] = $filterValue;
}
}
} else {
$query['filters'][$filterField] = $filterQuery;
}
}
if(is_null($this->resource())) {
throw new ActionException('Resource is required for action');
}

if(request()->has('search')) {
$query['search'] = request('search');
}

return $this->route('index', null, $query);
}

protected function exportCsv(): Response|Application|ResponseFactory
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$letter = 'A';

foreach ($this->resource->exportFields() as $index => $field) {
foreach ($this->resource()->exportFields() as $field) {
$sheet->setCellValue("{$letter}1", $field->label());

$letter++;
Expand All @@ -54,9 +37,9 @@ protected function exportCsv(): Response|Application|ResponseFactory


$line = 2;
foreach ($this->resource->all() as $item) {
foreach ($this->resource()->all() as $item) {
$letter = 'A';
foreach ($this->resource->exportFields() as $index => $field) {
foreach ($this->resource()->exportFields() as $index => $field) {
$sheet->setCellValue($letter . $line, $field->exportViewValue($item));
$letter++;
}
Expand All @@ -66,9 +49,48 @@ protected function exportCsv(): Response|Application|ResponseFactory

$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$this->resource->title().'.xlsx"');
header('Content-Disposition: attachment;filename="'.$this->resource()->title().'.xlsx"');
header('Cache-Control: max-age=0');

return response($writer->save('php://output'));
}

public function isTriggered(): bool
{
return request()->has('exportCsv');
}

/**
* @throws ActionException
*/
public function url(): string
{
if(is_null($this->resource())) {
throw new ActionException('Resource is required for action');
}

$query = ['exportCsv' => true];

if(request()->has('filters')) {
foreach (request()->query('filters') as $filterField => $filterQuery) {
if(is_array($filterQuery)) {
foreach ($filterQuery as $filterInnerField => $filterValue) {
if(is_numeric($filterInnerField) && !is_array($filterValue)) {
$query['filters'][$filterField][] = $filterValue;
} else {
$query['filters'][$filterInnerField] = $filterValue;
}
}
} else {
$query['filters'][$filterField] = $filterQuery;
}
}
}

if(request()->has('search')) {
$query['search'] = request('search');
}

return $this->resource()->route('index', null, $query);
}
}
32 changes: 11 additions & 21 deletions src/Commands/BaseMoonShineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,22 @@

class BaseMoonShineCommand extends Command
{
/**
* Install directory.
*
* @var string
*/
protected string $directory = 'app/MoonShine';

/**
* Get stub contents.
*
* @param $name
*
* @return string
*/
protected function getStub($name): string
protected function getStub(string $name): string
{
return $this->laravel['files']->get(__DIR__."/stubs/$name.stub");
return $this->laravel['files']->get(__DIR__."/../stubs/$name.stub");
}

/**
* Make new directory.
*
* @param string $path
*/
protected function makeDir(string $path = '')
protected function getDirectory(): string
{
$this->laravel['files']->makeDirectory("$this->directory/$path", 0755, true, true);
return config('moonshine.dir', $this->directory);
}

protected function makeDir(string $path = ''): void
{
if(isset($this->laravel['files'])) {
$this->laravel['files']->makeDirectory("$this->directory/$path", 0755, true, true);
}
}
}
36 changes: 6 additions & 30 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,27 @@

namespace Leeto\MoonShine\Commands;

use Illuminate\Console\Command;

class InstallCommand extends BaseMoonShineCommand
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'moonshine:install';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Install the moonshine package';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
public function handle(): void
{
$this->initDirectory();
$this->initDirectories();
}

/**
* Initialize the admAin directory.
*
* @return void
*/
protected function initDirectory()
protected function initDirectories(): void
{
$this->directory = config('moonshine.dir', $this->directory);

if (is_dir($this->directory)) {
$this->error("$this->directory directory already exists!");

return;
if (is_dir($this->getDirectory())) {
$this->error("{$this->getDirectory()} directory already exists!");
}

$this->makeDir('/');
$this->info('Directory was created:' . str_replace(base_path(), '', $this->directory));
$this->info('Directory was created:' . str_replace(base_path(), '', $this->getDirectory()));

$this->makeDir('Controllers');
$this->makeDir('Resources');
Expand Down
25 changes: 4 additions & 21 deletions src/Commands/ResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,17 @@

class ResourceCommand extends BaseMoonShineCommand
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'moonshine:resource {name?} {--m|model=} {--t|title=}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create resource';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
public function handle(): void
{
$this->createResource();
}

public function createResource()
public function createResource(): void
{
$this->directory = config('moonshine.dir', $this->directory);

$name = str($this->argument('name'));

if(!$name) {
Expand All @@ -47,7 +30,7 @@ public function createResource()
$model = $this->option('model') ?? $name;
$title = $this->option('title') ?? $name;

$resource = $this->directory."/Resources/{$name}Resource.php";
$resource = $this->getDirectory()."/Resources/{$name}Resource.php";
$contents = $this->getStub('Resource');
$contents = str_replace('DummyModel', $model, $contents);
$contents = str_replace('DummyTitle', $title, $contents);
Expand All @@ -59,7 +42,7 @@ public function createResource()

$this->info("{$name}Resource file was created: " . str_replace(base_path(), '', $resource));

$controller = $this->directory."/Controllers/{$name}Controller.php";
$controller = $this->getDirectory()."/Controllers/{$name}Controller.php";
$contents = $this->getStub('ResourceController');

$this->laravel['files']->put(
Expand Down
13 changes: 1 addition & 12 deletions src/Commands/UserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,11 @@

class UserCommand extends BaseMoonShineCommand
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'moonshine:user';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create user';


public function handle()
public function handle(): void
{
$email = $this->ask('Email');
$name = $this->ask('Name');
Expand Down
2 changes: 1 addition & 1 deletion src/Components/MenuComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MenuComponent extends Component
{
public function render(): View|Factory|Htmlable|Closure|string|Application
{
$data = app(Menu::class)->get();
$data = app(Menu::class)->all();

return view('moonshine::components.menu', [
"data" => $data,
Expand Down
12 changes: 12 additions & 0 deletions src/Contracts/Actions/ActionContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Leeto\MoonShine\Contracts\Actions;

interface ActionContract
{
public function handle(): mixed;

public function url(): string;

public function isTriggered(): bool;
}
Loading

0 comments on commit 14b7716

Please sign in to comment.