Skip to content

Commit

Permalink
Симлинки создаются с относительными путями, в windows os вместо симли…
Browse files Browse the repository at this point in the history
…нка копируется файл
  • Loading branch information
visavi committed Jan 12, 2020
1 parent 2752392 commit f9bc4ca
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 282 deletions.
58 changes: 12 additions & 46 deletions app/Commands/AppConfigure.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace App\Commands;

use Exception;
use Phinx\Console\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;

class AppConfigure extends AbstractCommand
Expand All @@ -18,62 +20,26 @@ protected function configure(): void
parent::configure();

$this->setName('app:configure')
->setDescription('Configures the application');
}

/**
* Configures the app
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output): void
{
$this->setPermissions();
$this->createSymlinks();

$output->writeln('<info>Application successfully configured.</info>');
->setDescription('Configures the application');
}

/**
* Set permissions
*
* @return void
*/
protected function setPermissions(): void
{
$storage = glob(STORAGE . '/*', GLOB_ONLYDIR);
$uploads = glob(UPLOADS . '/*', GLOB_ONLYDIR);
$modules = [HOME . '/assets/modules'];

$dirs = array_merge($storage, $uploads, $modules);

foreach ($dirs as $dir) {
$old = umask(0);
@chmod($dir, 0777);
umask($old);
}
}

/**
* Create symlinks
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
* @throws Exception
*/
protected function createSymlinks(): void
protected function execute(InputInterface $input, OutputInterface $output): void
{
$languages = array_map('basename', glob(RESOURCES . '/lang/*', GLOB_ONLYDIR));
$command = $this->getApplication()->find('app:permission');
$command->run($input, new NullOutput());

foreach ($languages as $language) {
$file = RESOURCES . '/lang/' . $language . '/main.js';
$link = HOME . '/assets/modules/' . $language . '.js';
$command = $this->getApplication()->find('app:symlink');
$command->run($input, new NullOutput());

deleteFile($link);

if (file_exists($file)) {
@symlink($file, $link);
}
}
$output->writeln('<info>Application successfully configured.</info>');
}
}
46 changes: 46 additions & 0 deletions app/Commands/AppPermission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace App\Commands;

use Phinx\Console\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;

class AppPermission extends AbstractCommand
{
/**
* {@inheritdoc}
*/
protected function configure(): void
{
parent::configure();

$this->setName('app:permission')
->setDescription('Set file permissions');
}

/**
* Set permissions
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output): void
{
$storage = glob(STORAGE . '/*', GLOB_ONLYDIR);
$uploads = glob(UPLOADS . '/*', GLOB_ONLYDIR);
$modules = [HOME . '/assets/modules'];

$dirs = array_merge($storage, $uploads, $modules);

$filesystem = new Filesystem();
$filesystem->chmod($dirs, 0777);

$output->writeln('<info>Permissions set successfully.</info>');
}
}
53 changes: 53 additions & 0 deletions app/Commands/AppSymlink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace App\Commands;

use Phinx\Console\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;

class AppSymlink extends AbstractCommand
{
/**
* {@inheritdoc}
*/
protected function configure(): void
{
parent::configure();

$this->setName('app:symlink')
->setDescription('Create file symlinks');
}

/**
* Create symlinks
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output): void
{
$filesystem = new Filesystem();
$languages = array_map('basename', glob(RESOURCES . '/lang/*', GLOB_ONLYDIR));

foreach ($languages as $language) {
$languagePath = RESOURCES . '/lang/' . $language;
$assetsPath = HOME . '/assets/modules/';

$relativePath = $filesystem->makePathRelative($languagePath, $assetsPath);

$filesystem->symlink(
$relativePath . 'main.js',
$assetsPath . $language . '.js',
true
);
}

$output->writeln('<info>Symlinks successfully created.</info>');
}
}
6 changes: 2 additions & 4 deletions app/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Controllers;

use App\Commands\AppSymlink;
use App\Models\Ban;
use Exception;
use Gregwar\Captcha\PhraseBuilder;
Expand Down Expand Up @@ -126,10 +127,7 @@ public function language(string $lang, Request $request): void
}

if (! file_exists(HOME . '/assets/modules/' . $lang . '.js')) {
symlink(
RESOURCES . '/lang/' . $lang . '/main.js',
HOME . '/assets/modules/' . $lang . '.js'
);
runCommand(new AppSymlink());
}
}

Expand Down
18 changes: 8 additions & 10 deletions app/Models/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Phinx\Console\Command\Migrate;
use Phinx\Console\PhinxApplication;
use Phinx\Wrapper\TextWrapper;
use Symfony\Component\Filesystem\Filesystem;

/**
* Class Module
Expand Down Expand Up @@ -39,7 +40,7 @@ class Module extends BaseModel
/**
* Assets modules path
*/
public $assetsPath = HOME . '/assets/modules';
public $assetsPath = HOME . '/assets/modules/';

/**
* Выполняет применение миграции
Expand Down Expand Up @@ -101,16 +102,13 @@ public function rollback(string $modulePath): void
*/
public function createSymlink(string $modulePath): void
{
$target = $modulePath . '/resources/assets';
$link = $this->getLinkName($modulePath);
$filesystem = new Filesystem();
$originPath = $this->getLinkName($modulePath);
$modulesPath = $modulePath . '/resources/assets';

if (is_link($link)) {
unlink($link);
}
$relativePath = $filesystem->makePathRelative($modulesPath, $this->assetsPath);

if (file_exists($target)) {
symlink($target, $link);
}
$filesystem->symlink($relativePath, $originPath, true);
}

/**
Expand All @@ -135,6 +133,6 @@ public function deleteSymlink(string $modulePath): void
*/
public function getLinkName(string $modulePath): string
{
return $this->assetsPath . '/' . Str::plural(strtolower(basename($modulePath)));
return $this->assetsPath . Str::plural(strtolower(basename($modulePath)));
}
}
Loading

0 comments on commit f9bc4ca

Please sign in to comment.