Skip to content

Commit

Permalink
Add support for XenForo Resources, bump version to v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jr-cologne committed Sep 20, 2020
1 parent d31cf65 commit 0d7367b
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 34 deletions.
34 changes: 17 additions & 17 deletions DevCommunityDE/CodeFormatter/CodeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DevCommunityDE\CodeFormatter;

use DevCommunityDE\CodeFormatter\XF\Service\Post\Preparer;
use DevCommunityDE\CodeFormatter\Preparer\PreparerInterface;
use \GuzzleHttp\Client as Guzzle;

/**
Expand All @@ -14,7 +14,7 @@ class CodeFormatter
{

/**
* @var Preparer
* @var PreparerInterface
*/
protected $preparer;

Expand All @@ -36,7 +36,7 @@ class CodeFormatter
/**
* @param Preparer $preparer
*/
public function __construct(Preparer $preparer)
public function __construct(PreparerInterface $preparer)
{
$this->preparer = $preparer;
$this->guzzle = new Guzzle;
Expand All @@ -50,37 +50,37 @@ public function __construct(Preparer $preparer)
*/
public function run()
{
if (!$this->postContainsCodeBlock()) {
if (!$this->contentContainsCodeBlock()) {
return;
}

$post = $this->getPostWithFormattedCode();
$content = $this->getEntityContentWithFormattedCode();

if (!$post) {
if (!$content) {
return;
}

$this->setPost($post);
$this->savePost();
$this->setEntityContent($content);
$this->saveEntity();
}

/**
* @return bool
*/
protected function postContainsCodeBlock()
protected function contentContainsCodeBlock()
{
return stripos($this->preparer->getPost()->message, '[CODE') !== false;
return stripos($this->preparer->getContent(), '[CODE') !== false;
}

/**
* @return string|null
*/
protected function getPostWithFormattedCode() : ?string
protected function getEntityContentWithFormattedCode() : ?string
{
$res = $this->guzzle->post(
$this->populateApiUrlWithApiKey($this->api_base_url, $this->api_key),
[
'body' => $this->preparer->getPost()->message,
'body' => $this->preparer->getContent(),
]
);

Expand All @@ -102,19 +102,19 @@ protected function populateApiUrlWithApiKey(string $api_url, string $api_key) :
}

/**
* @param string $post
* @param string $content
*/
protected function setPost(string $post)
protected function setEntityContent(string $content)
{
$this->preparer->setMessage($post);
$this->preparer->setContent($content);
}

/**
*
*/
protected function savePost()
protected function saveEntity()
{
$this->preparer->getPost()->save();
$this->preparer->getEntity()->save();
}

}
40 changes: 40 additions & 0 deletions DevCommunityDE/CodeFormatter/Preparer/PreparerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace DevCommunityDE\CodeFormatter\Preparer;

use XF\Mvc\Entity\Entity;

/**
* Class PreparerInterface
*
* @package DevCommunityDE\CodeFormatter\Preparer
*/
interface PreparerInterface
{

/**
* @return Entity
*/
public function getEntity(): Entity;

/**
* @return string
*/
public function getContent() : string;

/**
* @param string $content
*/
public function setContent(string $content);

/**
*
*/
public function afterInsert();

/**
*
*/
public function afterUpdate();

}
43 changes: 43 additions & 0 deletions DevCommunityDE/CodeFormatter/Traits/FormatsCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace DevCommunityDE\CodeFormatter\Traits;

use DevCommunityDE\CodeFormatter\CodeFormatter;

/**
* Class FormatsCode
*
* @package DevCommunityDE\CodeFormatter\Traits
*/
trait FormatsCode
{

/**
*
*/
public function afterInsert()
{
parent::afterInsert();

$this->formatCode();
}

/**
*
*/
public function afterUpdate()
{
parent::afterUpdate();

$this->formatCode();
}

/**
*
*/
protected function formatCode()
{
(new CodeFormatter($this))->run();
}

}
29 changes: 14 additions & 15 deletions DevCommunityDE/CodeFormatter/XF/Service/Post/Preparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,42 @@

namespace DevCommunityDE\CodeFormatter\XF\Service\Post;

use DevCommunityDE\CodeFormatter\CodeFormatter;
use DevCommunityDE\CodeFormatter\Preparer\PreparerInterface;
use DevCommunityDE\CodeFormatter\Traits\FormatsCode;
use XF\Entity\Post;
use XF\Service\Post\Preparer as BasePreparer;

/**
* Class Preparer
*
* @package DevCommunityDE\CodeFormatter\XF\Service\Post
*/
class Preparer extends BasePreparer
class Preparer extends BasePreparer implements PreparerInterface
{
use FormatsCode;

/**
*
* @return Post
*/
public function afterInsert()
public function getEntity() : Post
{
parent::afterInsert();

$this->formatCode();
return $this->getPost();
}

/**
*
* @return string
*/
public function afterUpdate()
public function getContent() : string
{
parent::afterUpdate();

$this->formatCode();
return $this->getEntity()->message;
}

/**
*
* @param string $content
*/
protected function formatCode()
public function setContent(string $content)
{
(new CodeFormatter($this))->run();
$this->setMessage($content);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace DevCommunityDE\CodeFormatter\XFRM\Service\ResourceUpdate;

use DevCommunityDE\CodeFormatter\Preparer\PreparerInterface;
use DevCommunityDE\CodeFormatter\Traits\FormatsCode;
use XFRM\Entity\ResourceUpdate;
use XFRM\Service\ResourceUpdate\Preparer as BasePreparer;

/**
* Class Preparer
*
* @package DevCommunityDE\CodeFormatter\XFRM\Service\ResourceUpdate
*/
class Preparer extends BasePreparer implements PreparerInterface
{
use FormatsCode;

/**
* @return ResourceUpdate
*/
public function getEntity() : ResourceUpdate
{
return $this->getUpdate();
}

/**
* @return string
*/
public function getContent() : string
{
return $this->getEntity()->message;
}

/**
* @param string $content
*/
public function setContent(string $content)
{
$this->setMessage($content);
}

}
1 change: 1 addition & 0 deletions DevCommunityDE/CodeFormatter/_data/class_extensions.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<class_extensions>
<extension from_class="XFRM\Service\ResourceUpdate\Preparer" to_class="DevCommunityDE\CodeFormatter\XFRM\Service\ResourceUpdate\Preparer" execute_order="10" active="1"/>
<extension from_class="XF\Service\Post\Preparer" to_class="DevCommunityDE\CodeFormatter\XF\Service\Post\Preparer" execute_order="10" active="1"/>
</class_extensions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"from_class": "XFRM\\Service\\ResourceUpdate\\Preparer",
"to_class": "DevCommunityDE\\CodeFormatter\\XFRM\\Service\\ResourceUpdate\\Preparer",
"execute_order": 10,
"active": true
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"XF-Service-Post-Preparer_DevCommunityDE-CodeFormatter-XF-Service-Post-Preparer.json": {
"hash": "38a93f88da5c59c03832964ae1f1522a"
},
"XFRM-Service-ResourceUpdate-Preparer_DevCommunityDE-CodeFormatter-XFRM-Service-ResourceUpdate-Preparer.json": {
"hash": "2ae5311a3c6c9b3d0b7e0539e15ce8d9"
}
}
5 changes: 5 additions & 0 deletions DevCommunityDE/CodeFormatter/_output/extension_hint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
// ################## THIS IS A GENERATED FILE ##################
// DO NOT EDIT DIRECTLY. EDIT THE CLASS EXTENSIONS IN THE CONTROL PANEL.

namespace DevCommunityDE\CodeFormatter\XFRM\Service\ResourceUpdate
{
class XFCP_Preparer extends \XFRM\Service\ResourceUpdate\Preparer {}
}

namespace DevCommunityDE\CodeFormatter\XF\Service\Post
{
class XFCP_Preparer extends \XF\Service\Post\Preparer {}
Expand Down
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions DevCommunityDE/CodeFormatter/addon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"legacy_addon_id": "",
"title": "dev-community.de - Code Formatter",
"description": "A XenForo add-on to format code in forum posts.",
"version_id": 1000070,
"version_string": "1.0.0",
"version_id": 1010070,
"version_string": "1.1.0",
"dev": "dev-community.de",
"dev_url": "https://github.com/dev-community-de",
"faq_url": "",
Expand Down

0 comments on commit 0d7367b

Please sign in to comment.