Skip to content

Commit

Permalink
Get all code up to coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdraper committed Jul 19, 2019
1 parent 98c448c commit 4216433
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 130 deletions.
6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

41 changes: 18 additions & 23 deletions src/Craftstatic.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
<?php

/**
* @author TJ Draper <tj@buzzingpixel.com>
* @copyright 2018 BuzzingPixel, LLC
* @license Apache-2.0
*/
declare(strict_types=1);

namespace buzzingpixel\craftstatic;

use buzzingpixel\craftstatic\models\SettingsModel;
use buzzingpixel\craftstatic\services\StaticHandlerService;
use buzzingpixel\craftstatic\twigextensions\CraftStaticTwigExtension;
use Craft;
use LogicException;
use yii\base\Event;
use craft\base\Plugin;
use craft\console\Application as ConsoleApplication;
use craft\events\RegisterCacheOptionsEvent;
use craft\services\Elements;
use craft\utilities\ClearCaches;
use craft\events\RegisterCacheOptionsEvent;
use buzzingpixel\craftstatic\models\SettingsModel;
use craft\console\Application as ConsoleApplication;
use buzzingpixel\craftstatic\services\StaticHandlerService;
use buzzingpixel\craftstatic\twigextensions\CraftStaticTwigExtension;
use LogicException;
use yii\base\Event;

/**
* Class Craftstatic
*/
class Craftstatic extends Plugin
{
/** @var Craftstatic $plugin */
public static $plugin;

/**
* Initializes plugin
*
* @throws LogicException
*/
public function init()
public function init() : void
{
parent::init();
self::$plugin = $this;
Expand All @@ -42,32 +36,33 @@ public function init()
Event::on(
Elements::class,
Elements::EVENT_AFTER_SAVE_ELEMENT,
function () {
static function () : void {
self::getStaticHandler()->clearCache();
}
);

Event::on(
ClearCaches::class,
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
function (RegisterCacheOptionsEvent $event) {
static function (RegisterCacheOptionsEvent $event) : void {
$event->options[] = [
'key' => 'craft-static-caches',
'label' => 'Static File Caches',
'action' => [self::$plugin->getStaticHandler(), 'clearCache']
'action' => [self::$plugin->getStaticHandler(), 'clearCache'],
];
}
);

// Add in our console commands
if (Craft::$app instanceof ConsoleApplication) {
$this->controllerNamespace = 'buzzingpixel\craftstatic\console\controllers';
if (! (Craft::$app instanceof ConsoleApplication)) {
return;
}

$this->controllerNamespace = 'buzzingpixel\craftstatic\console\controllers';
}

/**
* Creates the settings model
* @return SettingsModel
*/
protected function createSettingsModel() : SettingsModel
{
Expand All @@ -76,12 +71,12 @@ protected function createSettingsModel() : SettingsModel

/**
* Gets the static handler service
* @return StaticHandlerService
*/
public function getStaticHandler() : StaticHandlerService
{
/** @var SettingsModel $settings */
$settings = $this->getSettings();

return new StaticHandlerService([
'cachePath' => $settings->cachePath,
'nixBasedClearCache' => $settings->nixBasedClearCache === true,
Expand Down
15 changes: 5 additions & 10 deletions src/console/controllers/CacheController.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<?php

/**
* @author TJ Draper <tj@buzzingpixel.com>
* @copyright 2018 BuzzingPixel, LLC
* @license Apache-2.0
*/
declare(strict_types=1);

namespace buzzingpixel\craftstatic\console\controllers;

use yii\helpers\Console;
use yii\console\Controller;
use yii\db\Exception as DbException;
use buzzingpixel\craftstatic\Craftstatic;
use yii\console\Controller;
use yii\helpers\Console;
use const PHP_EOL;

/**
* Static Cache Command
Expand All @@ -20,9 +16,8 @@ class CacheController extends Controller
{
/**
* Purges all static cache
* @throws DbException
*/
public function actionPurge()
public function actionPurge() : void
{
Craftstatic::$plugin->getStaticHandler()->clearCache();

Expand Down
15 changes: 5 additions & 10 deletions src/models/SettingsModel.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
<?php

/**
* @author TJ Draper <tj@buzzingpixel.com>
* @copyright 2018 BuzzingPixel, LLC
* @license Apache-2.0
*/
declare(strict_types=1);

namespace buzzingpixel\craftstatic\models;

use Craft;
use craft\base\Model;
use craft\console\Application as ConsoleApplication;
use const DIRECTORY_SEPARATOR;
use function rtrim;

/**
* Class SettingsModel
*/
class SettingsModel extends Model
{
/**
* Initializes model
*/
public function init()
public function init() : void
{
if (! isset($_SERVER['DOCUMENT_ROOT']) ||
! $_SERVER['DOCUMENT_ROOT'] ||
Expand All @@ -33,7 +28,7 @@ public function init()

$docRoot = rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR);

$this->cachePath = "{$docRoot}{$sep}cache";
$this->cachePath = $docRoot . $sep . 'cache';
}

/** @var string $cachePath */
Expand Down
40 changes: 22 additions & 18 deletions src/services/StaticHandlerService.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
<?php

/**
* @author TJ Draper <tj@buzzingpixel.com>
* @copyright 2018 BuzzingPixel, LLC
* @license Apache-2.0
*/
declare(strict_types=1);

namespace buzzingpixel\craftstatic\services;

use LogicException;
use craft\base\Component;
use craft\web\Request;
use FilesystemIterator;
use craft\base\Component;
use RecursiveIteratorIterator;
use LogicException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use const DIRECTORY_SEPARATOR;
use function file_put_contents;
use function is_dir;
use function ltrim;
use function mkdir;
use function rmdir;
use function rtrim;
use function shell_exec;
use function sprintf;
use function str_replace;
use function unlink;

/**
* Class StaticHandlerService
*/
class StaticHandlerService extends Component
{
/** @var string $cachePath */
Expand All @@ -31,6 +35,7 @@ class StaticHandlerService extends Component

/**
* StaticHandlerService constructor
*
* @param array $config
*/
public function __construct(array $config = [])
Expand All @@ -52,11 +57,8 @@ public function __construct(array $config = [])

/**
* Handles the incoming content
* @param string $content
* @param bool $cache
* @return string
*/
public function handleContent(string $content, $cache = true) : string
public function handleContent(string $content, bool $cache = true) : string
{
if (! $this->cachePath) {
return $content;
Expand Down Expand Up @@ -92,23 +94,25 @@ public function handleContent(string $content, $cache = true) : string
return $content;
}

file_put_contents("{$cachePath}/index.html", $content);
file_put_contents($cachePath . '/index.html', $content);

return $content;
}

/**
* Clears the cache
*
* @throws LogicException
*/
public function clearCache()
public function clearCache() : void
{
if (! $this->cachePath) {
throw new LogicException('The cache path is not defined');
}

if ($this->nixBasedClearCache) {
shell_exec("rm -rf {$this->cachePath}/*");
shell_exec(sprintf('rm -rf %s/*', $this->cachePath));

return;
}

Expand Down
22 changes: 7 additions & 15 deletions src/twigextensions/CraftStaticNode.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
<?php

/**
* @author TJ Draper <tj@buzzingpixel.com>
* @copyright 2018 BuzzingPixel, LLC
* @license Apache-2.0
*/
declare(strict_types=1);

namespace buzzingpixel\craftstatic\twigextensions;

use Twig_Compiler;
use buzzingpixel\craftstatic\Craftstatic;
use Twig\Compiler;
use Twig\Node\Node;
use function is_bool;

/**
* Class CraftStaticNode
*/
class CraftStaticNode extends \Twig_Node
class CraftStaticNode extends Node
{
/**
* @param Twig_Compiler $compiler
*/
public function compile(Twig_Compiler $compiler)
public function compile(Compiler $compiler) : void
{
$cache = $this->getAttribute('cache');

if (\is_bool($cache)) {
if (is_bool($cache)) {
$cache = $cache ? 'true' : 'false';
$compiler->write("\$cache = {$cache};\n");
} elseif ($cache === 0 || $cache === 1) {
Expand Down
34 changes: 11 additions & 23 deletions src/twigextensions/CraftStaticTokenParser.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
<?php

/**
* @author TJ Draper <tj@buzzingpixel.com>
* @copyright 2018 BuzzingPixel, LLC
* @license Apache-2.0
*/
declare(strict_types=1);

namespace buzzingpixel\craftstatic\twigextensions;

use Twig_Token;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;

/**
* Class CraftStaticTokenParser
*/
class CraftStaticTokenParser extends \Twig_TokenParser
class CraftStaticTokenParser extends AbstractTokenParser
{
/**
* @inheritdoc
Expand All @@ -26,26 +20,24 @@ public function getTag() : string
/**
* @inheritdoc
*/
public function parse(Twig_Token $token)
public function parse(Token $token)
{
$attributes = [
'cache' => true
];
$attributes = ['cache' => true];

$stream = $this->parser->getStream();

if ($stream->test(Twig_Token::NAME_TYPE, 'cache')) {
if ($stream->test(Token::NAME_TYPE, 'cache')) {
$stream->next();
$attributes['cache'] = $stream->getCurrent()->getValue();
$stream->next();
}

$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(Token::BLOCK_END_TYPE);
$nodes['body'] = $this->parser->subparse([
$this,
'decideStaticEnd'
'decideStaticEnd',
], true);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(Token::BLOCK_END_TYPE);

return new CraftStaticNode(
$nodes,
Expand All @@ -55,11 +47,7 @@ public function parse(Twig_Token $token)
);
}

/**
* @param \Twig_Token $token
* @return bool
*/
public function decideStaticEnd(Twig_Token $token) : bool
public function decideStaticEnd(Token $token) : bool
{
return $token->test('endstatic');
}
Expand Down

0 comments on commit 4216433

Please sign in to comment.