jbzoo/crosscms

此包已被废弃且不再维护。未建议替代包。

适用于 Joomla 和 WordPress CMS 的摘要库

1.1.12 2016-07-21 13:37 UTC

This package is auto-updated.

Last update: 2020-05-20 00:09:51 UTC


README

一个扩展,一段代码 — 不同的 CMS!

License Latest Stable Version Scrutinizer Code Quality

俄语 README!

描述

CrossCMS 是一组简单的助手,可以帮助您为 Joomla 和 WordPress 创建一个跨平台扩展。因此,开发者只需编写一次代码,并对两个不同的 CMS 进行相同的测试。

这个库是结合不同的 API 系统的结果,以统一的外观隐藏了其中的差异。有必要将其视为 CMS 主函数的全局重命名(尽管这有点复杂)。此外,该库不包含任何特定功能的实现。因此,您的扩展几乎没有开销和内存性能。

CrossCMS 是为 JBZoo CCK(内容构造工具包)创建的,并且运行良好。您可以在存储库 JBZoo/JBZoo 中找到该项目。

测试

对于测试,我们在 PHP 中运行从 v5.4 到 v7.0 的相同单元测试集合 没有任何模拟或存根!。只有最新的真实 CMS,只有硬核!

make
make server
make test-all

CMS 支持

  • Joomla CMS:3.4.x ... 3.6.x
  • Wordpress:4.2.x ... 4.5.x

文档

安装

只需使用 composer

composer require jbzoo/crosscms

主要容器

Pimple DI 包含所有助手。

开始使用 CrossCMS。

use JBZoo\CrossCMS\Cms;

require_once './vendor/autoload.php';
$cms = Cms::getInstance(); // Create or Get DI-singleton with helpers. It's ready to use! 

自动完成

我们使用 PhpStorm IDE,因此我们建议您安装 Silex 插件 并将文件 pimple.json 复制到您项目的根目录。该文件是 JBZoo/PimpleDumper 的结果。

处理缓存

CrossCMS 仅使用 CMS 驱动程序和 API 进行缓存。

$cms = Cms::getInstance();

// Check is cache enabled
$cms['cache']->isEnabled();

// Store variable to cache
$cms['cache']->set($key, $data, $group, $isForce, $ttl);

// Get variable from cache
$cms['cache']->get($key, $group);

// Store output buffer, See https://php.ac.cn/manual/ru/ref.outcontrol.php
$cms['cache']->start($key, $data, $group, $isForce, $ttl);

// Get output buffer from cache
$cms['cache']->end($key, $group);

示例

$cms = Cms::getInstance();

$myVar = $cms['cache']->get('some-var');
if (!$myVar) {
    $myVar = someHardcoreFunction(); // Too slow code
    $cms['cache']->set('some-var', $myVar);
}

echo $myVar; // Use it!

一般网站属性

$cms = Cms::getInstance();

$cms['config']->isDebug(); 
$cms['config']->sitename();
$cms['config']->sitedesc();
$cms['config']->email();   
$cms['config']->dbHost();  
$cms['config']->dbName();  
$cms['config']->dbUser();  
$cms['config']->dbPass();  
$cms['config']->dbPrefix();
$cms['config']->dbType();  
$cms['config']->timezone();

数据库

我们建议您使用数据库助手配合 SqlBuilder。这是一个简单安全的SQL查询构建器,兼容CrossCMS、Joomla和Wordpress。这不是必须的,只有当您需要时才使用。如果发生错误,CMS将抛出异常(Joomla)或触发Die(Wordpress)。

$cms = Cms::getInstance();

$select = 'SELECT PI() AS pi';

$cms['db']->fetchAll($select);          // [['pi' => '3.141593']] 
$cms['db']->fetchRow($select);          // ['pi' => '3.141593']
$cms['db']->fetchArray($select);        // ['3.141593']
$cms['db']->query($select);             // Result: (int)1 - expected rows
$cms['db']->escape(' abc123-+\'"` ');   // ' abc123-+\\\'\"` ' - Т.е получим безопасную строку для SQL средствами CMS
$cms['db']->insertId();                 // Get last Primary ID after INSERT
$cms['db']->getTableColumns('#__table') // Table columns 

日期

助手允许您以不同的格式获取时间(检查时区和本地化)。它支持预定义的日期格式。我们使用助手 JBZoo/Utils 将字符串解析为日期。

$cms = Cms::getInstance();
$time = '2011-12-13 01:02:03';              // Date in some popular format   
$time = '1323738123';                       // or in seconds (timestamp)

$cms['date']->format($time);                // '2011-12-13 01:02:03' (Sql is default)
$cms['date']->format($time, 'timestamp');   // (int)1323738123
$cms['date']->format($time, 'detail');      // 'Tuesday, Dec 13 2011 01:02'
$cms['date']->format($time, 'full');        // 'Tuesday, Dec 13 2011'
$cms['date']->format($time, 'long')         // '13 December, 2011'
$cms['date']->format($time, 'medium')       // 'Dec 13, 2011'
$cms['date']->format($time, 'short');       // '12/13/11'
$cms['date']->format($time, 'time')         // '01:02'

环境

$cms = Cms::getInstance();

$cms['env']->getVersion();  // CMS Version
$cms['env']->isAdmin();     // Is control panel (back-end)?
$cms['env']->isSite();      // Is front-end
$cms['env']->isCli();       // Is command line (cron...) ?
$cms['env']->getRootUrl();  // Get frontpage URL

事件

CrossCMS使用简单且功能强大的事件管理器 JBZoo/Event。这是一个 只有一个文件 的解决方案。

示例

use JBZoo\CrossCMS\AbstractEvents;

$cms = Cms::getInstance();

$noopCallback = function(){ /* some action */};

$cms->on(AbstractEvents::EVENT_INIT, $noopCallback);    // CMS inited
$cms->on(AbstractEvents::EVENT_CONTENT, $noopCallback); // on parse content
$cms->on(AbstractEvents::EVENT_HEADER, $noopCallback);  // before head-tag rendering
$cms->on(AbstractEvents::EVENT_SHUTDOWN, $noopCallback);// before CMS shutdown

您可以为前后端页面订阅。

$cms->on(AbstractEvents::EVENT_INIT.'.admin', $noopCallback);   // Init only for admin-panel 
$cms->on(AbstractEvents::EVENT_INIT.'.site', $noopCallback);    // Init only for front-end 

安装示例(请参阅注释)

资源

$cms = Cms::getInstance();

$cms['header']->setTitle($title);               // Set page title
$cms['header']->setDesc($description);          // Set page description
$cms['header']->setKeywords($keywords);         // Set meta keywords
$cms['header']->addMeta($meta, $value = null);  // Add some meta content (Open Graph, etc)
$cms['header']->noindex();                      // Noindex for Google
$cms['header']->jsFile($file);                  // Include JS-file (url)
$cms['header']->jsCode($code);                  // Some custom JS-code in the head-tag of website. (jQuery-widgets, etc).
$cms['header']->cssFile($file);                 // Include CSS-file (url)
$cms['header']->cssCode($code);                 // Some custom CSS-styles in the head-tag of website. (Dynamic background from PHP-code, etc).

HTTP客户端

$cms = Cms::getInstance();

$response = $cms['http']->request('http://site.com/path');  // Only first argument is required

$response = $cms['http']->request(
    'http://site.com/path',                                 // Url of service
    [                                                       // GET/POST variables
        'var-1' => 'value-1',
        'var-2' => 'value-2',
    ],
    [                                                       // Options
        'timeout'    => 5,                                  // Max wait or connection timeout 
        'method'     => 'GET',                              // HTTP-method (GET|POST|HEAD|PUT|DELETE|OPTIONS|PATCH)  
        'headers'    => [                                   // Custom headers
            'x-custom-header' => '42',
        ],                             
        'response'   => 'full',                             // Format of response (full|body|headers|code) 
        'cache'      => 0,                                  // Enable cache (see $cms['cache']) 
        'cache_ttl'  => 60,                                 // Cache TTL in minutes   
        'user_agent' => 'CrossCMS HTTP Client',             // Custom USER_AGENT  
        'ssl_verify' => 1,                                  // Verify SSL cert 
        'debug'      => 0,                                  // Debug mode on errors (additional debug messages from CMS) 
    ]
);

// Вариант ответа для ['response' => 'full'] 
$response->code;     // HTTPcode, (int)200
$response->headers;  // Array of headers, 'key' => 'value'
$response->body;     // Body, some string

本地化

$cms = Cms::getInstance();

echo $cms['lang']->translate('january');                        // "January" | "Январь"
echo $cms['lang']->translate('%s and %s', 'qwerty', 123456));   // "qwerty and 123456" | 'qwerty и 123456'

// Short alias
function _text($message) {
    $cms = Cms::getInstance();
    return call_user_func_array(array($cms['lang'], 'translate'), func_get_args());
}

echo _text('january');                      // "January" | "Январь"
echo _text('%s and %s', 'qwerty', 123456)); // "qwerty and 123456" | 'qwerty и 123456'

内部库

通常所有流行的CMS都包含流行的JS库。如果有内置的,就没有必要包含您的jQuery文件。因此,我们避免了与其他CMS扩展的经典冲突。

$cms = Cms::getInstance();

$cms['libs']->jQuery();             // Include built-in jQuery file
$cms['libs']->jQueryUI();
$cms['libs']->jQueryAutocomplete();
$cms['libs']->jQueryDatePicker();
$cms['libs']->colorPicker();

邮件发送器

$cms = Cms::getInstance();
$mail = $cms['mailer'];

$mail->clean(); // Cleanup before new message

$mail->setTo('admin@example.com'); 
$mail->setSubject('Test message subject');
$mail->setBody('<p>Simple test</p>');
$mail->isHtml(false);
$mail->setFrom('no-replay@example.com', 'Website name');

$mail->setHeader('Cc', 'John Smith <john@smith.org>'); 
$mail->setHeaders([
    'Cc'  => 'John Smith <john@smith.org>',
    'Bcc' => 'Mike Smith <mike@smith.org>'
]);

$mail->addAttachment('/full/file/path.zip', 'Some custom name'); 
        
$mail->send(); // return true|false        

简写别名

$mail->complex('admin@example.com', 'Test message subject', 'Test complex method'); // (return true|false) 

文件系统

$cms = Cms::getInstance();

$cms['path']->get('root:');     // Root of website
$cms['path']->get('upload:');   // Uploaded images and other files
$cms['path']->get('tmpl:');     // Templates
$cms['path']->get('cache:');    // FS caches
$cms['path']->get('logs:');     // Logs
$cms['path']->get('tmp:');      // For any temporary data

// Get path to file in tmp directory
$path = $cms['path']->get('tmp:my-file.txt');
$path = $cms['path']->get('tmp:folder/my-file.txt');

请求

$cms = Cms::getInstance();

$cms['request']->getMethod();                   // Current http-method 
$cms['request']->isGet();                       // Is GET method? 
$cms['request']->isPost();                      // Is POST method?
$cms['request']->isAjax();                      // Is AJAX request? 
$cms['request']->getUri();                      // URL od current page 
$cms['request']->checkToken();                  // Check tocken (XSS) 
$cms['request']->getHeader('x-custom-header');  // Get HTTP-header from request 
 
$cms['request']->set($name, $value);            // Set new value

$cms['request']->get('foo', '42');                       // Get var
$cms['request']->get('foo', null, 'trim');               // Get variable and trim it (See JBZoo/Utils/Filter)
$cms['request']->get('foo', null, 'trim, alias, float'); // Several filters JBZoo/Utils/Filter
$cms['request']->get('foo', null, function ($value) {    // Custom filter (handler) 
    $value = str_replace('124', '789', $value);
    return $value;
}));

响应

$cms = Cms::getInstance();

$cms['response']->set404($message);            // Show page 404
$cms['response']->set500($message);            // Show fatal error
$cms['response']->redirect($url, 301);         // Redirect use to new location
$cms['response']->json(array $data = array()); // Send JSON
$cms['response']->text();                      // Send header "Content-Type: plain/text"
$cms['response']->noCache();                   // Send no cache headers
$cms['response']->setHeader($name, $value);    // Send response http-header

会话

$cms = Cms::getInstance();

$cms['session']->has($key);                                    
$cms['session']->get($key, $default, $group);                  
$cms['session']->set($key, $value, $group);                    
$cms['session']->getGroup($group, $default);                    
$cms['session']->setGroup($group, array $data, $isFullReplace);
$cms['session']->clear($key, $group);                          
$cms['session']->clearGroup($group);                             
$cms['session']->getToken();                                   

用户

$cms = Cms::getInstance();
$user = $cms['user']->getCurrent();

$user->isGuest();     
$user->isAdmin();     
$user->getEmail();    
$user->getLogin();    
$user->getName();      
$user->getId();       
$user->getAvatar(128);

示例

Wordpress插件

Joomla!CMS 扩展

许可证

MIT