Skip to content

Commit

Permalink
Publish the purify storage directory automatically during install
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Jan 7, 2021
1 parent 64d75dc commit 093bf29
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 31 deletions.
39 changes: 18 additions & 21 deletions src/Purify.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use HTMLPurifier;
use HTMLPurifier_Config;
use HTMLPurifier_ConfigSchema;

class Purify
{
Expand All @@ -17,19 +16,16 @@ class Purify

/**
* Constructor.
*
* @param array $config
*/
public function __construct()
public function __construct(array $config = [])
{
$config = HTMLPurifier_Config::create($this->getSettings());

$this->setPurifier(new HTMLPurifier($config));
$this->purifier = $this->makeNewHtmlPurifier($config);
}

/**
* Cleans the specified input.
*
* If a configuration array is given, it **will not**
* merge your current configuration.
* Sanitize the given input.
*
* @param array|string $input
* @param array|null $config
Expand All @@ -38,16 +34,13 @@ public function __construct()
*/
public function clean($input, array $config = null)
{
if (is_array($input)) {
return $this->purifier->purifyArray($input, $config);
} else {
return $this->purifier->purify($input, $config);
}
return is_array($input)
? $this->purifier->purifyArray($input, $config)
: $this->purifier->purify($input, $config);
}

/**
* Sets the current purifier to
* the specified purifier object.
* Set the underlying purifier instance.
*
* @param HTMLPurifier $purifier
*
Expand All @@ -61,7 +54,7 @@ public function setPurifier(HTMLPurifier $purifier)
}

/**
* Returns the HTML purifier object.
* Get the underlying purifier instance.
*
* @return HTMLPurifier
*/
Expand All @@ -71,12 +64,16 @@ public function getPurifier()
}

/**
* Get the purifier settings.
* Create a new HTMLPurifier instance.
*
* @return array
* @param array $config
*
* @return HTMLPurifier
*/
public function getSettings()
protected function makeNewHtmlPurifier(array $config = [])
{
return config('purify.settings', HTMLPurifier_ConfigSchema::instance()->defaults);
return new HTMLPurifier(
HTMLPurifier_Config::create($config)
);
}
}
47 changes: 37 additions & 10 deletions src/PurifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,70 @@

namespace Stevebauman\Purify;

use HTMLPurifier_Config;
use HTMLPurifier_ConfigSchema;
use HTMLPurifier_DefinitionCache_Serializer;
use Laravel\Lumen\Application as Lumen;
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Application as Laravel;

class PurifyServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('purify', function ($app) {
return new Purify();
return new Purify(
$app['config']['purify.settings'] ?? $this->getDefaultPurifierConfig()
);
});
}

/**
* Perform post-registration booting of services.
* Register the publishable configuration.
*
* @return void
*/
public function boot()
{
if ($this->app instanceof Laravel && $this->app->runningInConsole()) {
$this->publishes([__DIR__ . '/Config/purify.php' => config_path('purify.php'),], 'config');
$this->publishes([
__DIR__ . '/../config/purify.php' => config_path('purify.php'),
$this->getDefaultPurifierDefinitionCacheDirectory() => storage_path('app/purify'),
], 'config');
} elseif ($this->app instanceof Lumen) {
$this->app->configure('purify');
}
}

/**
* Get the default HTML Purifier definition cache directory.
*
* @return string
*/
protected function getDefaultPurifierDefinitionCacheDirectory()
{
$defaultConfig = HTMLPurifier_Config::create($this->getDefaultPurifierConfig());

$serializer = new HTMLPurifier_DefinitionCache_Serializer(null);

return $serializer->generateBaseDirectoryPath($defaultConfig);
}

/**
* Get the default HTML Purifier configuration.
*
* @return array
*/
protected function getDefaultPurifierConfig()
{
return HTMLPurifier_ConfigSchema::instance()->defaults;
}

/**
* Get the services provided by the provider.
*
Expand Down

0 comments on commit 093bf29

Please sign in to comment.