-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52353d4
commit 18e90af
Showing
35 changed files
with
2,300 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace BristolSU\ControlDB\Export; | ||
|
||
use BristolSU\ControlDB\Contracts\Repositories\Group; | ||
use BristolSU\ControlDB\Contracts\Repositories\Position; | ||
use BristolSU\ControlDB\Contracts\Repositories\Role; | ||
use BristolSU\ControlDB\Contracts\Repositories\User; | ||
use BristolSU\ControlDB\Export\Formatter\Role\SortByGroupName; | ||
use BristolSU\ControlDB\Export\Formatter\Shared\SortByColumn; | ||
use Illuminate\Console\Command; | ||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Seed the database | ||
*/ | ||
class ExportControlCommand extends Command | ||
{ | ||
/** | ||
* Signature for the command | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'control:export | ||
{type : The type of resource to export} | ||
{--exporter= : The name of the exporter to use}'; | ||
|
||
/** | ||
* Name for the commmand | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'Export Control'; | ||
|
||
/** | ||
* Description of the command | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Export control to an external service'; | ||
|
||
/** | ||
* Handle the command execution | ||
* | ||
* Seed the database with fake data. | ||
*/ | ||
public function handle() | ||
{ | ||
Exporter::driver($this->option('exporter'))->export($this->exportData()); | ||
$this->info('Export complete'); | ||
} | ||
|
||
private function exportData() | ||
{ | ||
switch($this->argument('type')) { | ||
case 'user': | ||
return app(User::class)->all(); | ||
break; | ||
case 'group': | ||
return app(Group::class)->all(); | ||
break; | ||
case 'role': | ||
return app(Role::class)->all(); | ||
break; | ||
case 'position': | ||
return app(Position::class)->all(); | ||
break; | ||
default: | ||
throw new InvalidArgumentException(sprintf('The type option %s is not allowed.', $this->argument('type'))); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
<?php | ||
|
||
namespace BristolSU\ControlDB\Export; | ||
|
||
use BristolSU\ControlDB\Export\Handler\DumpHandler; | ||
use BristolSU\ControlDB\Export\Handler\Handler; | ||
use BristolSU\ControlDB\Export\Handler\SaveCsvHandler; | ||
use Closure; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Support\Str; | ||
use InvalidArgumentException; | ||
|
||
class ExportManager | ||
{ | ||
|
||
/** | ||
* The container instance. | ||
* | ||
* @var Container | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* The registered custom driver creators. | ||
* | ||
* @var array | ||
*/ | ||
protected $customCreators = []; | ||
|
||
protected $formatters = []; | ||
|
||
/** | ||
* Create a new Export manager instance. | ||
* | ||
* @param Container $container | ||
* @return void | ||
*/ | ||
public function __construct(Container $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* Get a export driver instance. | ||
* | ||
* @param string|null $driver | ||
* @return mixed | ||
*/ | ||
public function driver($driver = null) | ||
{ | ||
return $this->resolve($driver ?? $this->getDefaultDriver()); | ||
} | ||
|
||
/** | ||
* Resolve the given export instance by name. | ||
* | ||
* @param string $name | ||
* @return Handler | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
protected function resolve($name) | ||
{ | ||
$config = $this->configurationFor($name); | ||
|
||
if (is_null($config)) { | ||
throw new InvalidArgumentException("Exporter [{$name}] is not defined."); | ||
} | ||
if (isset($this->customCreators[$config['driver']])) { | ||
return $this->callCustomCreator($config); | ||
} | ||
|
||
$driverMethod = 'create'.Str::studly($config['driver']).'Driver'; | ||
|
||
if (method_exists($this, $driverMethod)) { | ||
return $this->{$driverMethod}($config); | ||
} | ||
|
||
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); | ||
} | ||
|
||
/** | ||
* Call a custom driver creator. | ||
* | ||
* @param array $config | ||
* @return mixed | ||
*/ | ||
protected function callCustomCreator(array $config) | ||
{ | ||
return $this->customCreators[$config['driver']]($this->container, $config); | ||
} | ||
|
||
/** | ||
* Get the export connection configuration. | ||
* | ||
* @param string $name | ||
* @return array | ||
*/ | ||
protected function configurationFor($name) | ||
{ | ||
$config = $this->container['config']["control.export.{$name}"]; | ||
if(is_null($config)) { | ||
return null; | ||
} | ||
if(!isset($config['formatters'])) { | ||
$config['formatters'] = []; | ||
} | ||
foreach($this->formatters() as $formatter => $formatterConfig) { | ||
$config['formatters'][$formatter] = $formatterConfig; | ||
} | ||
|
||
return $config; | ||
} | ||
|
||
/** | ||
* Get the default export driver name. | ||
* | ||
* @return string | ||
*/ | ||
public function getDefaultDriver() | ||
{ | ||
return $this->container['config']['control.export.default']; | ||
} | ||
|
||
/** | ||
* Register a custom driver creator Closure. | ||
* | ||
* @param string $driver | ||
* @param \Closure $callback | ||
* @return $this | ||
*/ | ||
public function extend($driver, Closure $callback) | ||
{ | ||
$this->customCreators[$driver] = $callback->bindTo($this, $this); | ||
|
||
return $this; | ||
} | ||
|
||
public function withFormatter(string $formatter, array $config = []) | ||
{ | ||
$this->formatters[$formatter] = $config; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Dynamically call the default driver instance. | ||
* | ||
* @param string $method | ||
* @param array $parameters | ||
* @return mixed | ||
*/ | ||
public function __call($method, $parameters) | ||
{ | ||
return $this->driver()->$method(...$parameters); | ||
} | ||
|
||
public function createDumpDriver(array $config) | ||
{ | ||
return new DumpHandler($config); | ||
} | ||
|
||
public function createSaveCsvDriver(array $config) | ||
{ | ||
return new SaveCsvHandler($config); | ||
} | ||
|
||
protected function formatters() | ||
{ | ||
return $this->formatters; | ||
} | ||
|
||
} |
Oops, something went wrong.