Files
laravel_modules/src/Publishing/Publisher.php
T
2026-11-20 12:06:31 +00:00

194 lines
3.8 KiB
PHP

<?php
namespace absdev\Modules\Publishing;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use absdev\Modules\Contracts\PublisherInterface;
use absdev\Modules\Contracts\RepositoryInterface;
use absdev\Modules\Module;
abstract class Publisher implements PublisherInterface
{
/**
* The name of module will used.
*
* @var string
*/
protected $module;
/**
* The modules repository instance.
*
* @var absdev\Modules\Contracts\RepositoryInterface
*/
protected $repository;
/**
* The laravel console instance.
*
* @var Command
*/
protected $console;
/**
* The success message will displayed at console.
*
* @var string
*/
protected $success;
/**
* The error message will displayed at console.
*
* @var string
*/
protected $error = '';
/**
* Determine whether the result message will shown in the console.
*
* @var bool
*/
protected $showMessage = true;
/**
* The constructor.
*/
public function __construct(Module $module)
{
$this->module = $module;
}
/**
* Show the result message.
*
* @return self
*/
public function showMessage()
{
$this->showMessage = true;
return $this;
}
/**
* Hide the result message.
*
* @return self
*/
public function hideMessage()
{
$this->showMessage = false;
return $this;
}
/**
* Get module instance.
*
* @return \absdev\Modules\Module
*/
public function getModule()
{
return $this->module;
}
/**
* Set modules repository instance.
*
* @return $this
*/
public function setRepository(RepositoryInterface $repository)
{
$this->repository = $repository;
return $this;
}
/**
* Get modules repository instance.
*
* @return absdev\Modules\Contracts\RepositoryInterface
*/
public function getRepository()
{
return $this->repository;
}
/**
* Set console instance.
*
*
* @return $this
*/
public function setConsole(Command $console)
{
$this->console = $console;
return $this;
}
/**
* Get console instance.
*
* @return Command
*/
public function getConsole()
{
return $this->console;
}
/**
* Get laravel filesystem instance.
*
* @return Filesystem
*/
public function getFilesystem()
{
return $this->repository->getFiles();
}
/**
* Get destination path.
*
* @return string
*/
abstract public function getDestinationPath();
/**
* Get source path.
*
* @return string
*/
abstract public function getSourcePath();
/**
* Publish something.
*/
public function publish()
{
if (! $this->console instanceof Command) {
$message = "The 'console' property must instance of \\Illuminate\\Console\\Command.";
throw new \RuntimeException($message);
}
if (! $this->getFilesystem()->isDirectory($sourcePath = $this->getSourcePath())) {
return;
}
if (! $this->getFilesystem()->isDirectory($destinationPath = $this->getDestinationPath())) {
$this->getFilesystem()->makeDirectory($destinationPath, 0775, true);
}
if ($this->getFilesystem()->copyDirectory($sourcePath, $destinationPath)) {
if ($this->showMessage === true) {
$this->console->line("<info>Published</info>: {$this->module->getStudlyName()}");
}
} else {
$this->console->error($this->error);
}
}
}