Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ version: 2
jobs:
build:
docker:
- image: circleci/php:7.4.4-cli
- image: developernaren/reactphp-gd
steps:
- checkout

- run: sudo apt update
- run: apt update

- restore_cache:
keys:
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
"react/http": "^0.8.6",
"react/stream": "^1.1",
"symfony/console": "^5.0",
"react/filesystem": "^0.1.2",
"react/filesystem": "dev-master",
"yosymfony/resource-watcher": "^2.0",
"symfony/dom-crawler": "^5.0",
"jenssegers/blade": "^1.3",
"intervention/image": "^2.5"
"intervention/image": "^2.5",
"icamys/php-sitemap-generator": "^2.0"
},
"replace": {
"paphper/contracts": "self.version"
Expand Down
76 changes: 65 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/BuildFileResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@ public function getFolder()
{
return (new Str($this->name))->getBeforeLast('/');
}

public function getUrlPath()
{
$folder = $this->getFolder();
$path = (new Str($folder))->removeAll($this->config->getBuildBaseFolder());

if(!(new Str($path))->startsWith('/')){
return '/' . $path;
}

return $path;
}

}
23 changes: 21 additions & 2 deletions src/Commands/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Paphper\Commands;

use Paphper\Config;
use Paphper\SiteGenerator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Build extends Command
Expand All @@ -16,9 +18,8 @@ class Build extends Command
private $filesystem;
private $loop;
private $manager;
private $io;

public function __construct($config, $pageResolvers, $fileContentResolver, $filesystem, $loop, $manager)
public function __construct(Config $config, $pageResolvers, $fileContentResolver, $filesystem, $loop, $manager)
{
parent::__construct();
$this->config = $config;
Expand All @@ -32,9 +33,27 @@ public function __construct($config, $pageResolvers, $fileContentResolver, $file
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new PaphperStyle($input, $output);

$silent = $input->getOption('quiet');
$force = $input->getOption('force');
$baseFolder = $this->config->getBuildBaseFolder();

if (false === $force && true !== $silent) {
$answer = $io->ask(sprintf('This will delete the folder %s. Are you sure? Type yes to continue.', $baseFolder));
if (!in_array($answer, ['y', 'ye', 'yes'])) {
$io->section('Skipping. Bye!');

return 0;
}
}
$generator = new SiteGenerator($this->pageResolvers, $this->fileContentResolver, $this->config, $this->filesystem, $this->loop, $this->manager, $io);
$generator->build();

return 0;
}

protected function configure()
{
$this->addOption('force', '-f', InputOption::VALUE_OPTIONAL, 'Force Build?', false);
}
}
69 changes: 59 additions & 10 deletions src/Commands/Watch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
namespace Paphper\Commands;

use Paphper\BuildFileResolver;
use Paphper\FileContentResolver;
use Paphper\Responses\Factory as ResponseFactory;
use Paphper\SiteGenerator;
use Paphper\Utils\FileCreator;
use Paphper\Utils\Str;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Server;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Yosymfony\ResourceWatcher\Crc32ContentHash;
use Yosymfony\ResourceWatcher\ResourceCachePhpFile;
Expand All @@ -20,18 +22,18 @@
class Watch extends Command
{
public static $contentHashMap = [];
public static $filesToBuild = [];
protected static $defaultName = 'dev';
private $config;
private $filesystem;
private $io;
private $loop;
private $watcher;
private $changedFiles = [];
private $fileContentResolver;
private $pageResolver;
private $manager;

public function __construct($config, $pageResolver, $fileContentResolver, $filesystem, $loop, $manager)
public function __construct($config, $pageResolver, FileContentResolver $fileContentResolver, $filesystem, $loop, $manager)
{
parent::__construct();
$this->config = $config;
Expand All @@ -45,15 +47,33 @@ public function __construct($config, $pageResolver, $fileContentResolver, $files
public function execute(InputInterface $input, OutputInterface $output)
{
$port = $this->config->getPort();
$this->io = $io = new SymfonyStyle($input, $output);
$this->io = $io = new PaphperStyle($input, $output);

$folderWatching = [$this->config->getPageBaseFolder()];
$baseFolder = $this->config->getBuildBaseFolder();

$silent = $input->getOption('quiet');
$force = $input->getOption('force');

if (false === $force && true !== $silent) {
$answer = $this->io->ask(sprintf('This will delete the folder %s. Are you sure? Type yes to continue.', $baseFolder));
if (!in_array($answer, ['y', 'ye', 'yes'])) {
$this->io->section('Skipping. Bye!');

return 0;
}
}

$folderWatching = [(new Str($this->config->getPageBaseFolder()))->getBeforeLast('/')];
$this->io->title('Watching for file changes');
$this->io->table(['watching folder'], [[implode(' and ', $folderWatching)]]);

$extensionToWatch = array_map(function ($extension) {
return '*'.$extension;
}, $this->fileContentResolver->getExtensions());

$finder = new Finder();
$finder->files()
->name(['*.html', '*.md', '*.blade.php'])
->name($extensionToWatch)
->in($folderWatching);

$hashContent = new Crc32ContentHash();
Expand All @@ -66,15 +86,33 @@ public function execute(InputInterface $input, OutputInterface $output)

$this->loop->addPeriodicTimer(1, function () {
$result = $this->watcher->findChanges();
$this->changedFiles = $changedFiles = $result->getUpdatedFiles();
foreach ($changedFiles as $filename) {
$changedFiles = $result->getUpdatedFiles();
$pageFiles = array_filter($changedFiles, function ($filename) {
return (new Str($filename))->startsWith($this->config->getPageBaseFolder());
});

if (empty($changedFiles)) {
return;
}

if (0 === count($pageFiles)) {
$pagesToBuild = self::$filesToBuild;
} else {
$pagesToBuild = $pageFiles;
}

foreach ($pagesToBuild as $filename) {
if (!in_array($filename, self::$filesToBuild)) {
self::$filesToBuild[] = $filename;
}

$htmlGenerator = $this->fileContentResolver->resolve($filename);
$htmlGenerator->getPageContent()
->then(function ($content) use ($filename) {
$buildFile = new BuildFileResolver($this->config, $filename);
(new FileCreator($this->filesystem, $buildFile->getName(), $content))->writeFile()
->then(function () use ($buildFile) {
$this->io->text(sprintf('%s build', $buildFile->getName()));
$this->io->text(sprintf('%s built', $buildFile->getName()));
});
});
}
Expand All @@ -86,7 +124,13 @@ public function execute(InputInterface $input, OutputInterface $output)
return $response->toResponse();
});

$this->openBrowser($port);
$this->io->text('trying to open browser. Serving static server at 0.0.0.0:'.$port);

try {
$this->openBrowser($port);
} catch (\Exception $exception){
$this->io->text(sprintf('failed opening browser. open at 0.0.0.0:%s in a browser', $port));
}

$socket = new \React\Socket\Server('0.0.0.0:'.$port, $this->loop);
$server->listen($socket);
Expand All @@ -109,4 +153,9 @@ public function openBrowser($port)
exec('open http://localhost:'.$port);
}
}

protected function configure()
{
$this->addOption('force', '-f', InputOption::VALUE_OPTIONAL, 'Force Build?', false);
}
}
17 changes: 16 additions & 1 deletion src/Contents/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,22 @@ public function getPageContent(): PromiseInterface
$filename = (new Str($this->filename))->getBeforeLast('.blade.php');
$filename = (new Str($filename))->replaceAllWith($this->config->getPageBaseFolder(), '');

$content = $this->blade->render('pages'.$filename);
/*
* because we are making the root of the folder base view file
* we need to build the page from the root
* For eg. for the folder path
* - pages
* - layouts
* we load all the folders for blade so that she can use @include etc.
* but for this to work the blade needs to be loaded as `pages.` pagename for pagename.blade.php
* so we get the name of the base folder and append to whatever the filename is, hence getting that folder to append
*/
$relativePageFolder = (new Str($this->config->getPageBaseFolder()))->getAfterLast('/');

/*
* added the folder name here explained above.
*/
$content = $this->blade->render($relativePageFolder.$filename);

$meta = new PaperTagContentParser($this->config, $this->filesystem, $content);

Expand Down
Loading