Skip to content

Commit

Permalink
Rewrited
Browse files Browse the repository at this point in the history
  • Loading branch information
Piterden committed Jun 17, 2017
1 parent ba7fea9 commit 2ee730c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 103 deletions.
57 changes: 27 additions & 30 deletions src/Addon/Console/Command/WriteAddonSeeder.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php namespace Defr\SeederMakeExtension\Addon\Console\Command;

use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
use Anomaly\Streams\Platform\Stream\StreamCollection;
use Anomaly\Streams\Platform\Support\Parser;
use Illuminate\Filesystem\Filesystem;

/**
* Class WriteAddonSeeder
* Class for write addon seeder.
*
* @author PyroCMS, Inc. <support@pyrocms.com>
* @author Ryan Thompson <ryan@pyrocms.com>
* @package defr.extension.seeder_make
*
* @link http://pyrocms.com/
* @author Denis Efremov <efremov.a.denis@gmail.com>
*/
class WriteAddonSeeder
{
Expand Down Expand Up @@ -43,6 +43,13 @@ class WriteAddonSeeder
*/
private $vendor;

/**
* The streams collection.
*
* @var StreamCollection
*/
private $streams;

/**
* Create a new WriteAddonSeeder instance.
*
Expand Down Expand Up @@ -74,26 +81,16 @@ public function handle(Parser $parser, Filesystem $filesystem)
$slug = ucfirst(camel_case($this->slug));
$type = ucfirst(camel_case($this->type));
$vendor = ucfirst(camel_case($this->vendor));
$addon = $slug . $type;

$addon = $slug.$type;
$class = $addon.'Seeder';

$class = $addon . 'Seeder';
$namespace = "{$vendor}\\{$addon}";
$uses = $this->getUses($namespace)->implode("\n");
$calls = $this->getCalls()->implode("\n ");

$uses = $this->getUses($namespace)->implode("\n");
$calls = $this->getCalls()->implode("\n ");

$path = "{$this->path}/src/{$class}.php";

$template = $filesystem->get(
dirname(
dirname(
dirname(
dirname(__DIR__)
)
)
).'/resources/stubs/addons/seeder.stub'
);
$path = "{$this->path}/src/{$class}.php";
$template = $filesystem->get(dirname(dirname(dirname(dirname(__DIR__))))
. '/resources/stubs/addons/seeder.stub');

$filesystem->makeDirectory(dirname($path), 0755, true, true);

Expand All @@ -109,14 +106,14 @@ public function handle(Parser $parser, Filesystem $filesystem)
* @param string $namespace The namespace
* @return Collection The uses.
*/
public function getUses($namespace)
private function getUses(string $namespace)
{
return $this->streams->map(
function ($stream) use ($namespace)
function (StreamInterface $stream) use ($namespace)
{
$name = ucfirst(str_singular($stream->getSlug()));
$entity = $this->getName($stream->getSlug());

return "use {$namespace}\\{$name}\\{$name}Seeder;";
return "use {$namespace}\\{$entity}\\{$entity}Seeder;";
}
);
}
Expand All @@ -126,14 +123,14 @@ function ($stream) use ($namespace)
*
* @return Collection The calls.
*/
public function getCalls()
private function getCalls()
{
return $this->streams->map(
function ($stream)
function (StreamInterface $stream)
{
$name = ucfirst(str_singular($stream->getSlug()));
$entity = $this->getName($stream->getSlug());

return "\$this->call({$name}Seeder::class);";
return "\$this->call({$entity}Seeder::class);";
}
);
}
Expand All @@ -144,7 +141,7 @@ function ($stream)
* @param string $slug The slug
* @return string The name.
*/
public function getName($slug)
private function getName(string $slug)
{
return ucfirst(str_singular($slug));
}
Expand Down
31 changes: 16 additions & 15 deletions src/Database/Seeder/Console/SeederMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
use \Illuminate\Support\Composer;

/**
* Class SeederMakeCommand
* Seeder make command class.
*
* @package defr.extension.seeder_make
*
* @author Denis Efremov <efremov.a.denis@gmail.com>
*/
class SeederMakeCommand extends \Illuminate\Console\Command
{

use DispatchesJobs;

/**
Expand All @@ -31,7 +34,7 @@ class SeederMakeCommand extends \Illuminate\Console\Command
*
* @var string
*/
protected $description = 'Create a new seeder class for addon';
protected $description = 'Create a new seeder classes for addon';

/**
* All streams string value
Expand All @@ -43,15 +46,15 @@ class SeederMakeCommand extends \Illuminate\Console\Command
/**
* The Composer instance.
*
* @var \Illuminate\Support\Composer
* @var Composer
*/
protected $composer;

/**
* Create a new command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Illuminate\Support\Composer $composer
* @param Filesystem $files
* @param Composer $composer
* @return void
*/
public function __construct(Filesystem $files, Composer $composer)
Expand All @@ -72,6 +75,7 @@ public function fire()
{
throw new \Exception('Addon could not be found.');
}

$path = $addon->getPath();
$type = $addon->getType();
$slug = $addon->getSlug();
Expand All @@ -91,6 +95,7 @@ public function fire()

/* @var StreamCollection $streams */
$streams = $this->getStreams($slug);

$answers = $this->makeQuestion($streams);

if (array_search($this->getAllChoice(), $answers) === false)
Expand All @@ -107,11 +112,7 @@ function ($stream) use ($answers)
{
$slug = $stream->getSlug();

$this->dispatch(new WriteEntitySeeder(
$addon,
$slug,
$stream->getNamespace()
));
$this->dispatch(new WriteEntitySeeder($addon, $slug));

$singular = ucfirst(str_singular($slug));

Expand All @@ -133,7 +134,7 @@ function ($stream) use ($answers)
* @throws \Exception
* @return string The stream namespace.
*/
public function getAddonNamespace()
private function getAddonNamespace()
{
$namespace = $this->argument('namespace');

Expand All @@ -151,7 +152,7 @@ public function getAddonNamespace()
* @param string $slug The addon slug
* @return StreamCollection
*/
public function getStreams($slug)
private function getStreams($slug)
{
return $this->dispatch(new GetStreams($slug))->filter(
function ($stream)
Expand All @@ -166,7 +167,7 @@ function ($stream)
*
* @return string All value.
*/
public function getAllChoice()
private function getAllChoice()
{
return $this->allChoice;
}
Expand All @@ -177,7 +178,7 @@ public function getAllChoice()
* @param StreamCollection $streams The streams
* @return array Answers
*/
public function makeQuestion(StreamCollection $streams)
private function makeQuestion(StreamCollection $streams)
{
$choices = $streams->map(
function ($stream)
Expand Down
20 changes: 7 additions & 13 deletions src/SeederMakeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@

use Anomaly\Streams\Platform\Addon\Extension\Extension;

/**
* Extension class.
*
* @package defr.extension.seeder_make
*
* @author Denis Efremov <efremov.a.denis@gmail.com>
*/
class SeederMakeExtension extends Extension
{

/**
* This extension provides...
*
* This should contain the dot namespace
* of the addon this extension is for followed
* by the purpose.variation of the extension.
*
* For example anomaly.module.store::gateway.stripe
*
* @var null|string
*/
protected $provides = null;

}
16 changes: 7 additions & 9 deletions src/SeederMakeExtensionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
use Anomaly\Streams\Platform\Addon\AddonServiceProvider;
use Defr\SeederMakeExtension\Database\Seeder\Console\SeederMakeCommand;

/**
* Extension service provider class.
*
* @package defr.extension.seeder_make
*
* @author Denis Efremov <efremov.a.denis@gmail.com>
*/
class SeederMakeExtensionServiceProvider extends AddonServiceProvider
{

/**
* Addon commands
*
* @var array
*/
protected $commands = [
SeederMakeCommand::class,
];

/**
* Register the addon
*/
Expand Down
52 changes: 16 additions & 36 deletions src/Stream/Console/Command/WriteEntitySeeder.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,43 @@
<?php namespace Defr\SeederMakeExtension\Stream\Console\Command;

use Illuminate\Filesystem\Filesystem;
use Anomaly\Streams\Platform\Addon\Addon;
use Anomaly\Streams\Platform\Support\Parser;
use Illuminate\Filesystem\Filesystem;

/**
* Class WriteEntitySeeder
* Class for write entity seeder.
*
* @author PyroCMS, Inc. <support@pyrocms.com>
* @author Ryan Thompson <ryan@pyrocms.com>
* @package defr.extension.seeder_make
*
* @link http://pyrocms.com/
* @author Denis Efremov <efremov.a.denis@gmail.com>
*/
class WriteEntitySeeder
{

/**
* The entity slug.
*
* @var string
*/
private $slug;

/**
* The addon instance.
*
* @var Addon
*/
private $addon;
protected $addon;

/**
* The entity stream namespace.
* The stream slug.
*
* @var string
*/
private $namespace;
protected $slug;

/**
* Create a new WriteEntitySeeder instance.
*
* @param Addon $addon
* @param Addon $addon
* @param $slug
* @param $namespace
*/
public function __construct(Addon $addon, $slug, $namespace)
public function __construct(Addon $addon, $slug)
{
$this->slug = $slug;
$this->addon = $addon;
$this->namespace = $namespace;
$this->slug = $slug;
$this->addon = $addon;
}

/**
Expand All @@ -58,26 +48,16 @@ public function __construct(Addon $addon, $slug, $namespace)
*/
public function handle(Parser $parser, Filesystem $filesystem)
{
$entities = camel_case($this->slug);

$suffix = ucfirst($entities);
$entity = str_singular($suffix);

$entities = camel_case($this->slug);
$suffix = ucfirst($entities);
$entity = str_singular($suffix);
$class = "{$entity}Seeder";

$namespace = $this->addon->getTransformedClass("{$entity}");

$path = $this->addon->getPath("src/{$entity}/{$entity}Seeder.php");

$template = $filesystem->get(
dirname(
dirname(
dirname(
dirname(__DIR__)
)
)
).'/resources/stubs/entity/seeder.stub'
);
$template = $filesystem->get(dirname(dirname(dirname(dirname(__DIR__))))
. '/resources/stubs/entity/seeder.stub');

$filesystem->makeDirectory(dirname($path), 0755, true, true);

Expand Down

0 comments on commit 2ee730c

Please sign in to comment.