Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Jul 23, 2019
1 parent 3e9b65b commit 12c9936
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 64 deletions.
8 changes: 1 addition & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,12 @@
},
"autoload": {
"psr-4": {
"Akaunting\\Module\\": "./src"
"Akaunting\\Module\\": "src"
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Akaunting\\Module\\Tests\\": "tests",
"Modules\\Recipe\\": "tests/stubs/valid/Recipe"
}
},
"extra": {
"laravel": {
"providers": [
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function handle()
$contents = $this->getTemplateContents();

try {
with(new FileGenerator($path, $contents))->generate();
$overwrite_file = $this->hasOption('force') ? $this->option('force') : false;
(new FileGenerator($path, $contents))->withFileOverwrite($overwrite_file)->generate();

$this->info("Created : {$path}");
} catch (FileAlreadyExistException $e) {
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function handle()
*/
protected function migrate(Module $module)
{
$path = str_replace(base_path(), '', (new Migrator($module))->getPath());
$path = str_replace(base_path(), '', (new Migrator($module, $this->getLaravel()))->getPath());

if ($this->option('subpath')) {
$path = $path . "/" . $this->option("subpath");
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/MigrateResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function reset($module)
$module = $this->module->findOrFail($module);
}

$migrator = new Migrator($module);
$migrator = new Migrator($module, $this->getLaravel());

$database = $this->option('database');

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/MigrateRollbackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function rollback($module)
$module = $this->module->findOrFail($module);
}

$migrator = new Migrator($module);
$migrator = new Migrator($module, $this->getLaravel());

$database = $this->option('database');

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/MigrateStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function handle()
*/
protected function migrateStatus(Module $module)
{
$path = str_replace(base_path(), '', (new Migrator($module))->getPath());
$path = str_replace(base_path(), '', (new Migrator($module, $this->getLaravel()))->getPath());

$this->call('migrate:status', [
'--path' => $path,
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/PublishMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function handle()
*/
public function publish($module)
{
with(new MigrationPublisher(new Migrator($module)))
with(new MigrationPublisher(new Migrator($module, $this->getLaravel())))
->setRepository($this->laravel['module'])
->setConsole($this)
->publish();
Expand Down
12 changes: 10 additions & 2 deletions src/Commands/RouteProviderMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Akaunting\Module\Support\Stub;
use Akaunting\Module\Traits\ModuleCommandTrait;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class RouteProviderMakeCommand extends GeneratorCommand
{
Expand Down Expand Up @@ -39,6 +40,13 @@ protected function getArguments()
];
}

protected function getOptions()
{
return [
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when the file already exists.'],
];
}

/**
* Get template contents.
*
Expand Down Expand Up @@ -86,15 +94,15 @@ protected function getDestinationFilePath()
*/
protected function getWebRoutesPath()
{
return '/' . $this->laravel['config']->get('stubs.files.routes', 'Routes/web.php');
return '/' . $this->laravel['modules']->config('stubs.files.routes/web', 'Routes/web.php');
}

/**
* @return mixed
*/
protected function getApiRoutesPath()
{
return '/' . $this->laravel['config']->get('stubs.files.routes', 'Routes/api.php');
return '/' . $this->laravel['modules']->config('stubs.files.routes/api', 'Routes/api.php');
}

public function getDefaultNamespace() : string
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/stubs/route-provider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ class $CLASS$ extends ServiceProvider
Route::prefix('api')
->middleware('api')
->namespace($this->moduleNamespace)
->group(__DIR__ . '/../Routes/api.php');
->group(__DIR__ . '/..$API_ROUTES_PATH$');
}
}
2 changes: 1 addition & 1 deletion src/Commands/stubs/scaffold/provider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class $CLASS$ extends ServiceProvider
*/
public function registerFactories()
{
if (!app()->environment('production')) {
if (!app()->environment('production') && $this->app->runningInConsole()) {
app(Factory::class)->load(__DIR__ . '/../$FACTORIES_PATH$');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/stubs/views/index.stub
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
<p>
This view is loaded from module: {!! config('$ALIAS$.name') !!}
</p>
@stop
@endsection
9 changes: 9 additions & 0 deletions src/Contracts/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,13 @@ public function getModulePath($moduleName);
* @return \Illuminate\Filesystem\Filesystem
*/
public function getFiles();

/**
* Get a specific config data from a configuration file.
* @param string $key
*
* @param string|null $default
* @return mixed
*/
public function config(string $key, $default = null);
}
64 changes: 46 additions & 18 deletions src/FileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
namespace Akaunting\Module;

use Countable;
use Illuminate\Cache\CacheManager;
use Illuminate\Container\Container;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Akaunting\Module\Contracts\RepositoryInterface;
Expand Down Expand Up @@ -42,6 +46,26 @@ abstract class FileRepository implements RepositoryInterface, Countable
*/
protected $stubPath;

/**
* @var UrlGenerator
*/
private $url;

/**
* @var ConfigRepository
*/
private $config;

/**
* @var Filesystem
*/
private $files;

/**
* @var CacheManager
*/
private $cache;

/**
* The constructor.
*
Expand All @@ -52,6 +76,10 @@ public function __construct(Container $app, $path = null)
{
$this->app = $app;
$this->path = $path;
$this->url = $app['url'];
$this->config = $app['config'];
$this->files = $app['files'];
$this->cache = $app['cache'];
}

/**
Expand Down Expand Up @@ -104,8 +132,8 @@ public function getScanPaths() : array
* Creates a new Module instance
*
* @param Container $app
* @param $name
* @param $path
* @param string $args
* @param string $path
* @return \Akaunting\Module\Module
*/
abstract protected function createModule(...$args);
Expand All @@ -122,7 +150,7 @@ public function scan()
$modules = [];

foreach ($paths as $key => $path) {
$manifests = $this->app['files']->glob("{$path}/module.json");
$manifests = $this->getFiles()->glob("{$path}/module.json");

is_array($manifests) || $manifests = [];

Expand Down Expand Up @@ -162,7 +190,7 @@ protected function formatCached($cached)
$modules = [];

foreach ($cached as $alias => $module) {
$path = $module["path"];
$path = $module['path'];

$modules[$alias] = $this->createModule($this->app, $alias, $path);
}
Expand All @@ -177,7 +205,7 @@ protected function formatCached($cached)
*/
public function getCached()
{
return $this->app['cache']->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () {
return $this->cache->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () {
return $this->toCollection()->toArray();
});
}
Expand Down Expand Up @@ -434,9 +462,9 @@ public function assetPath($alias) : string
* @param null $default
* @return mixed
*/
public function config($key, $default = null)
public function config(string $key, $default = null)
{
return $this->app['config']->get('module.' . $key, $default);
return $this->config->get('module.' . $key, $default);
}

/**
Expand All @@ -447,13 +475,13 @@ public function config($key, $default = null)
public function getUsedStoragePath() : string
{
$directory = storage_path('app/modules');
if ($this->app['files']->exists($directory) === false) {
$this->app['files']->makeDirectory($directory, 0777, true);
if ($this->getFiles()->exists($directory) === false) {
$this->getFiles()->makeDirectory($directory, 0777, true);
}

$path = storage_path('app/modules/modules.used');
if (!$this->app['files']->exists($path)) {
$this->app['files']->put($path, '');
if (!$this->getFiles()->exists($path)) {
$this->getFiles()->put($path, '');
}

return $path;
Expand All @@ -470,16 +498,16 @@ public function setUsed($name)
{
$module = $this->findOrFail($name);

$this->app['files']->put($this->getUsedStoragePath(), $module);
$this->getFiles()->put($this->getUsedStoragePath(), $module);
}

/**
* Forget the module used for cli session.
*/
public function forgetUsed()
{
if ($this->app['files']->exists($this->getUsedStoragePath())) {
$this->app['files']->delete($this->getUsedStoragePath());
if ($this->getFiles()->exists($this->getUsedStoragePath())) {
$this->getFiles()->delete($this->getUsedStoragePath());
}
}

Expand All @@ -490,17 +518,17 @@ public function forgetUsed()
*/
public function getUsedNow() : string
{
return $this->findOrFail($this->app['files']->get($this->getUsedStoragePath()));
return $this->findOrFail($this->getFiles()->get($this->getUsedStoragePath()));
}

/**
* Get laravel filesystem instance.
*
* @return \Illuminate\Filesystem\Filesystem
* @return Filesystem
*/
public function getFiles()
{
return $this->app['files'];
return $this->files;
}

/**
Expand Down Expand Up @@ -528,7 +556,7 @@ public function asset($asset) : string

$baseUrl = str_replace(public_path() . DIRECTORY_SEPARATOR, '', $this->getAssetsPath());

$url = $this->app['url']->asset($baseUrl . "/{$name}/" . $url);
$url = $this->url->asset($baseUrl . "/{$name}/" . $url);

return str_replace(['http://', 'https://'], '//', $url);
}
Expand Down
15 changes: 14 additions & 1 deletion src/Generators/FileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,25 @@ public function setPath($path)
return $this;
}

public function withFileOverwrite(bool $overwrite): FileGenerator
{
$this->overwriteFile = $overwrite;

return $this;
}

/**
* Generate the file.
*/
public function generate()
{
if (!$this->filesystem->exists($path = $this->getPath())) {
$path = $this->getPath();

if (!$this->filesystem->exists($path)) {
return $this->filesystem->put($path, $this->getContents());
}

if ($this->overwriteFile === true) {
return $this->filesystem->put($path, $this->getContents());
}

Expand Down
7 changes: 4 additions & 3 deletions src/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ class Migrator
/**
* Create new instance.
*
* @param \Akaunting\Module\Module $module
* @param Module $module
* @param Application $application
*/
public function __construct(Module $module)
public function __construct(Module $module, Application $application)
{
$this->module = $module;
$this->laravel = $module->getLaravel();
$this->laravel = $application;
}

/**
Expand Down
Loading

0 comments on commit 12c9936

Please sign in to comment.