Skip to content

Commit

Permalink
Merge pull request #16 from bobdenotter/more-checks
Browse files Browse the repository at this point in the history
More checks (`changelogCheck`, `systemlogCheck`, `thumbnailConfigCheck`), and a bit of cleanup.
  • Loading branch information
GwendolenLynch authored May 12, 2017
2 parents d09e3f5 + 88517a0 commit b54b723
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 9 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
Configuration Notices for Bolt
==============================

Friendly helpers for your Bolt installation. This extension comes bundled with
the packaged distribution version of Bolt (commonly known as "The `tar` or `zip`
version"). This extension provides helpful tips to prevent common pitfalls, in a
Friendly helpers for your Bolt installation. This extension comes bundled with
the packaged distribution version of Bolt (commonly known as "The `tar` or `zip`
version"). This extension provides helpful tips to prevent common pitfalls, in a
range of different situations.

To install this extension in your custom bootstrapped version of Bolt, run the
To install this extension in your custom bootstrapped version of Bolt, run the
following in the root folder with your main `composer.json`:

```
composer require bolt/configuration-notices
```

![7e22d8c8-bbdd-11e6-9668-48a1ec86ba04](https://cloud.githubusercontent.com/assets/1833361/21287029/3f4d8cf4-c463-11e6-8cd1-69583e7fa2ba.png)

You can influence a few of the checks through the configuration. How meta!

```yaml
configuration_notices:
local_domains: [ '.localhost' ]
log_threshold: 1000
```
- `local_domains` can be set to contain (parts of) domain names, and is used
in the check to determine if the current installation is considered to be
"local" or "production".
- `log_threshold` is used to determine what is considered 'a lot of rows' in
the checks for the Change log and System log, to give a suggestion to trim
the database tables.
95 changes: 90 additions & 5 deletions src/EventListener/ConfigurationNoticesListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Bolt\ConfigurationNotices\EventListener;

use Bolt\Storage\Entity\LogChange;
use Bolt\Storage\Entity\LogSystem;
use Bolt\Version;
use Silex\Application;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -20,6 +22,10 @@ class ConfigurationNoticesListener implements EventSubscriberInterface
/** @var \Silex\Application $app */
protected $app;

protected $logThreshold = null;

protected $defaultDomainPartials = ['.dev', 'dev.', 'devel.', 'development.', 'test.', '.test', 'new.', '.new', '.local', 'local.'];

/**
* Constructor function.
*
Expand Down Expand Up @@ -49,6 +55,10 @@ public function onKernelRequest(GetResponseEvent $event)
return;
}

$this->logThreshold = $this->app['config']->get('general/configuration_notices/log_threshold', 10000);

$this->app['stopwatch']->start('bolt.configuration_notices');

$this->mailConfigCheck();
$this->developmentCheck();
$this->liveCheck($request);
Expand All @@ -61,6 +71,11 @@ public function onKernelRequest(GetResponseEvent $event)
$this->canonicalCheck($request);
$this->imageFunctionsCheck();
$this->maintenanceCheck();
$this->changelogCheck();
$this->systemlogCheck();
$this->thumbnailConfigCheck();

$this->app['stopwatch']->stop('bolt.configuration_notices');
}

/**
Expand Down Expand Up @@ -104,11 +119,11 @@ protected function liveCheck(Request $request)
}

$host = $request->getHttpHost();
$domainpartials = $this->app['config']->get('general/debug_local_domains', []);
$domainpartials = (array) $this->app['config']->get('general/configuration_notices/local_domains', []);

$domainpartials = array_unique(array_merge(
(array) $domainpartials,
['.dev', 'dev.', 'devel.', 'development.', 'test.', '.test', 'new.', '.new', 'localhost', '.local', 'local.']
$this->defaultDomainPartials
));

foreach ($domainpartials as $partial) {
Expand Down Expand Up @@ -230,8 +245,6 @@ protected function writableFolderCheck()
$folders[] = ['filesystem' => 'app', 'folder' => 'database', 'name' => '<tt>app/cache/</tt>'];
}

// dump($folders);

foreach($folders as $folder) {
$mountPoint = $folder['filesystem'];
$filePath = $folder['folder'] . $filename;
Expand All @@ -247,7 +260,6 @@ protected function writableFolderCheck()
}
}


/**
* Check if the thumbs/ folder is writable, if `save_files: true`
*/
Expand Down Expand Up @@ -339,6 +351,79 @@ protected function maintenanceCheck()
}
}

/**
* Check if Changelog is enabled, and if doesn't contain too many rows.
*/
protected function changelogCheck()
{
if (!$this->app['config']->get('general/changelog/enabled', false)) {
return;
}

// Get the number of items in the changelog
$count = $this->app['storage']->getRepository(LogChange::class)->count();

if ($count > $this->logThreshold) {
$message = sprintf(
"Bolt's <strong>changelog</strong> is enabled, and there are more than %s rows in the table.",
$this->logThreshold
);
$info = sprintf(
"Be sure to clean it up periodically, using a Cron job or on the <a href='%s'>Changelog page</a>.",
$this->app['url_generator']->generate('changelog')
);
$notice = json_encode([
'severity' => 1,
'notice' => $message,
'info' => $info
]);
$this->app['logger.flash']->configuration($notice);
}
}

/**
* Check if systemlog doesn't contain too many rows.
*/
protected function systemlogCheck()
{
// Get the number of items in the changelog
$count = $this->app['storage']->getRepository(LogSystem::class)->count();

if ($count > $this->logThreshold) {
$message = sprintf(
"Bolt's <strong>systemlog</strong> is enabled, and there are more than %s rows in the table.",
$this->logThreshold
);
$info = sprintf(
"Be sure to clean it up periodically, using a Cron job or on the <a href='%s'>Systemlog page</a>.",
$this->app['url_generator']->generate('systemlog')
);
$notice = json_encode([
'severity' => 1,
'notice' => $message,
'info' => $info
]);
$this->app['logger.flash']->configuration($notice);
}
}

/**
* Check if the thumbnail config has been updated for 3.3+ .
*/
protected function thumbnailConfigCheck()
{
$thumbConfig = $this->app['config']->get('general/thumbnails');

if ((strpos($thumbConfig['notfound_image'] . $thumbConfig['error_image'], '://') === false)) {
$notice = json_encode([
'severity' => 1,
'notice' => "Your configuration settings for <code>thumbnails/notfound_image</code> or <code>thumbnails/error_image</code> contain a value that needs to be updated.",
'info' => "Update the value with a namespace, for example: <code>bolt_assets://img/default_notfound.png</code>."
]);
$this->app['logger.flash']->configuration($notice);
}
}

/**
* @param string $mountPoint
* @param string $filePath
Expand Down

0 comments on commit b54b723

Please sign in to comment.