Earl
Well-known member
- What I'm trying to do is:
I wanna add some metadata to a file and modify it as soon as it uploads as an attachment. (before hash calculations etc)
So I thought overriding beforeNewAttachment method in AbstractHandler would do the trick.
XF\Attachment\AbstractHandler
PHP:
public function beforeNewAttachment(\XF\FileWrapper $file, array &$extra = [])
{
return;
}
As you can see that method uses \XF\FileWrapper class
Then again, Having my code inside of new method called "addMetaData" in \XF\FileWrapper class would allow me to execute my code.
I mean by doing something like this (I will use attachment handler in XenForo Resource Manager to illustrate the idea here):
MyAddon\XFRM\Attachment\ResourceUpdate
PHP:
namespace MyAddon\XFRM\Attachment;
class ResourceVersion extends XFCP_ResourceUpdate
{
/**
* @param \MyAddon\XF\FileWrapper $file
* @param array $extra
*/
public function beforeNewAttachment(\XF\FileWrapper $file, array &$extra = [])
{
parent::beforeNewAttachment($file, $extra);
$file->addMetaData();
}
}
PHP:
namespace MyAddon\XF;
class FileWrapper extends XFCP_FileWrapper
{
/**
* Add watermark to images
* @return $this|bool
*/
public function addMetaData()
{
//my code here
}
Would allow me to run my code in each "attachment handlers" as opposed to repeating my code inside of beforeNewAttachment method in every attachment handlers (you can't extend abstract classes)
- My problem:
But Class Extionsionfor MyAddon\XFRM\Attachment\ResourceUpdate works perfectly. (in my case MyAddon\XF\Attachment\Post)
When I call addMetaData() method (as in first code)
$file->addMetaData(); //after this line
It jumps into an error
Debug screenshots:
Then after pressing f7 (Jump into) next line:
Exception: Call to undefined method XF\FileWrapper::addMetaData()
Strange right?
Namespace looks fine:
And class extensions in admincp:
The craziest thing is:
I tried adding breakpoints to extension.php too
As you can see, it loads both class extensions in __construct method of \XF\Extension class when I debug.
and It only loads the attachment handler class extension with the class proxy system, But not the FileWrapper extension when I running the debugger here:
\XF\Extension
PHP:
// XFCP = XenForo Class Proxy, in case you're wondering
$nsSplit = strrpos($extendClass, '\\');
if ($nsSplit !== false && $ns = substr($extendClass, 0, $nsSplit))
{
$proxyClass = $ns . '\\XFCP_' . substr($extendClass, $nsSplit + 1);
}
else
{
$proxyClass = 'XFCP_' . $extendClass;
}
// TODO: there may be a situation where this fails. If we've changed the extensions after classes have
// been loaded, it's possible these classes will already be loaded with a different config. Figure out
// how to handle that if possible. Remains to be seen if it comes up (mostly relating to add-on imports).