use anonymous function

This commit is contained in:
Jean-Christian Paul Denis 2022-12-10 21:03:00 +01:00
parent a68017d3b0
commit f5c59b638c
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951

View File

@ -14,43 +14,13 @@ if (!defined('DC_CONTEXT_ADMIN')) {
return null; return null;
} }
if (!tinyPacker::repositoryDir()) {
return null;
}
dcCore::app()->addBehavior( dcCore::app()->addBehavior(
'adminModulesListGetActions', 'adminModulesListGetActions',
['tinyPacker', 'adminModulesGetActions'] function ($list, $id, $_) {
); if (!in_array($list->getList(), [
dcCore::app()->addBehavior( 'plugin-activate',
'adminModulesListDoActions', 'theme-activate',
['tinyPacker', 'adminModulesDoActions'] ])) {
);
/**
* @ingroup DC_PLUGIN_TINYPACKER
* @brief Quick create packages of modules from admin to public dir.
* @since 2.6
*/
class tinyPacker
{
/**
* Blog's public sub-directory where to put packages
* @var string
*/
public static $sub_dir = 'packages';
/**
* Add button to create package to modules lists
* @param object $list adminModulesList instance
* @param string $id Module id
* @param arrray $_ Module properties
* @return string HTML submit button
*/
public static function adminModulesGetActions($list, $id, $_)
{
if ($list->getList() != 'plugin-activate'
&& $list->getList() != 'theme-activate') {
return null; return null;
} }
@ -58,17 +28,11 @@ class tinyPacker
'<input type="submit" name="tinypacker[' . '<input type="submit" name="tinypacker[' .
html::escapeHTML($id) . ']" value="Pack" />'; html::escapeHTML($id) . ']" value="Pack" />';
} }
);
/** dcCore::app()->addBehavior(
* Create package on modules lists action 'adminModulesListDoActions',
* @param object $list adminModulesList instance function ($list, $modules, $type) {
* @param array $modules Selected modules ids
* @param string $type List type (plugins|themes)
* @throws Exception If no public dir or module
* @return null Null
*/
public static function adminModulesDoActions($list, $modules, $type)
{
# Pack action # Pack action
if (empty($_POST['tinypacker']) if (empty($_POST['tinypacker'])
|| !is_array($_POST['tinypacker'])) { || !is_array($_POST['tinypacker'])) {
@ -79,12 +43,18 @@ class tinyPacker
$id = $modules[0]; $id = $modules[0];
# Repository directory # Repository directory
if (($root = self::repositoryDir()) === false) { $dir = path::real(
throw new Exception( dcCore::app()->blog->public_path . '/' . (
__( defined('TINYPACKER_SUBDIR') ? TINYPACKER_SUBDIR : 'packages'
'Destination directory is not writable.' ),
) false
); );
if (!is_dir($dir)) {
files::makeDir($dir, true);
}
if (!is_writable($dir)) {
throw new Exception(__('Destination directory is not writable.'));
} }
# Module to pack # Module to pack
@ -105,6 +75,7 @@ class tinyPacker
'\.directory', '\.directory',
'\.DS_Store', '\.DS_Store',
'Thumbs\.db', 'Thumbs\.db',
'_disabled',
]; ];
# Packages names # Packages names
@ -116,7 +87,7 @@ class tinyPacker
# Create zip # Create zip
foreach ($files as $f) { foreach ($files as $f) {
@set_time_limit(300); @set_time_limit(300);
$fp = fopen($root . '/' . $f, 'wb'); $fp = fopen($dir . '/' . $f, 'wb');
$zip = new fileZip($fp); $zip = new fileZip($fp);
@ -138,28 +109,4 @@ class tinyPacker
); );
http::redirect($list->getURL()); http::redirect($list->getURL());
} }
);
/**
* Check and create directories used by packer
* @return string|boolean Cleaned path or false on error
*/
public static function repositoryDir()
{
$dir = path::real(
dcCore::app()->blog->public_path . '/' . tinyPacker::$sub_dir,
false
);
try {
if (!is_dir($dir)) {
files::makeDir($dir, true);
}
if (is_writable($dir)) {
return $dir;
}
} catch(Exception $e) {
}
return false;
}
}