Skip to content

Commit 1aa92a9

Browse files
committed
Extension Doc - Implementing Filesystem Changes to phpBB
1 parent f55c9d0 commit 1aa92a9

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

development/extensions/tutorial_advanced.rst

+169
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This tutorial explains:
1616
* `Using service decoration`_
1717
* `Replacing the phpBB Datetime class`_
1818
* `Extension version checking`_
19+
* `Implementing Filesystem changes to phpBB`_
1920

2021
Adding a PHP event to your extension
2122
====================================
@@ -526,3 +527,171 @@ branch can be used to provide links to versions in development.
526527
``download`` | "(Optional) A URL to download this version of the extension."
527528
``eol`` | "This is currently not being used yet. Use ``null``"
528529
``security`` | "This is currently not being used yet. Use ``false``"
530+
531+
Implementing Filesystem changes to phpBB
532+
========================================
533+
In certain scenarios, an extension may need to implement filesystem changes within phpBB beyond the extension's
534+
self-contained structure. While this approach is generally discouraged, there are specific conditions where it
535+
may be appropriate. Extensions can safely add or remove files and folders within phpBB’s ``files``, ``images``,
536+
and ``store`` directories, as these are designed to hold user-generated content and are typically not replaced
537+
during phpBB or extension updates.
538+
539+
There are two primary methods for implementing filesystem changes in phpBB through an extension:
540+
541+
1. Using Migrations
542+
2. Using the Extension Enabling Process (ext.php)
543+
544+
Below are examples of how to create a directory named ``my_extension_dir`` in the ``store`` directory for storing additional extension-related files.
545+
546+
Using Migrations
547+
----------------
548+
While migrations are generally designed for database changes, they offer specific advantages when managing filesystem changes:
549+
550+
- Existence Check: Use the ``effectively_installed`` method to check if the files or directories exist already.
551+
- Installation Order: Use the ``depends_on`` method to ensure the directory is created at the correct stage during the extension’s installation process.
552+
- Run separate (optional) filesystem processes during installation and uninstallation.
553+
554+
.. code-block:: php
555+
556+
<?php
557+
558+
namespace acme\demo\migrations;
559+
560+
class add_my_extension_dir extends \phpbb\db\migration\container_aware_migration
561+
{
562+
/** @var \phpbb\filesystem\filesystem */
563+
protected $filesystem;
564+
565+
/** @var string */
566+
protected $my_new_dir;
567+
568+
/**
569+
* Initialize required properties
570+
*/
571+
protected function init()
572+
{
573+
if (!isset($this->filesystem))
574+
{
575+
$this->filesystem = $this->container->get('filesystem');
576+
$this->my_new_dir = $this->container->getParameter('core.root_path') . 'store/my_extension_dir';
577+
}
578+
}
579+
580+
public function effectively_installed()
581+
{
582+
$this->init();
583+
return $this->filesystem->exists($this->$my_new_dir);
584+
}
585+
586+
public static function depends_on()
587+
{
588+
return ['\acme\demo\migrations\first_migration'];
589+
}
590+
591+
public function update_data(): array
592+
{
593+
return [
594+
['custom', [[$this, 'add_dir']]],
595+
];
596+
}
597+
598+
public function revert_data(): array
599+
{
600+
return [
601+
['custom', [[$this, 'remove_dir']]],
602+
];
603+
}
604+
605+
public function add_dir()
606+
{
607+
$this->init();
608+
609+
try
610+
{
611+
$this->filesystem->mkdir($this->$my_new_dir, 0755);
612+
$this->filesystem->touch($this->$my_new_dir . '/index.htm');
613+
}
614+
catch (\phpbb\filesystem\exception\filesystem_exception $e)
615+
{
616+
// log or handle any errors here using $e->get_filename() or $e->getMessage()
617+
}
618+
}
619+
620+
public function remove_dir()
621+
{
622+
$this->init();
623+
624+
try
625+
{
626+
$this->filesystem->remove($this->$my_new_dir);
627+
}
628+
catch (\phpbb\filesystem\exception\filesystem_exception $e)
629+
{
630+
// log or handle any errors here using $e->get_filename() or $e->getMessage()
631+
}
632+
}
633+
}
634+
635+
Using ext.php
636+
-------------
637+
Filesystem changes can also be implemented within the extension’s ``ext.php`` file. This method is preferable if:
638+
639+
- The changes have no specific requirements or dependencies that need monitoring through a migration step.
640+
- The changes should occur at a particular stage, such as enabling, disabling, or deleting the extension.
641+
642+
.. code-block:: php
643+
644+
<?php
645+
646+
namespace acme\demo;
647+
648+
class ext extends \phpbb\extension\base
649+
{
650+
public function enable_step($old_state)
651+
{
652+
if ($old_state !== false)
653+
{
654+
return parent::enable_step($old_state);
655+
}
656+
657+
$filesystem = $this->container->get('filesystem');
658+
$my_dir_path = $this->container->getParameter('core.root_path') . 'store/my_extension_dir';
659+
660+
try
661+
{
662+
$filesystem->mkdir($my_dir_path, 0755);
663+
$filesystem->touch($my_dir_path . '/index.htm');
664+
}
665+
catch (\phpbb\filesystem\exception\filesystem_exception $e)
666+
{
667+
// log or handle any errors here using $e->get_filename() or $e->getMessage()
668+
}
669+
670+
return 'added my_extension_dir';
671+
}
672+
673+
public function purge_step($old_state)
674+
{
675+
if ($old_state !== false)
676+
{
677+
return parent::purge_step($old_state);
678+
}
679+
680+
$filesystem = $this->container->get('filesystem');
681+
$my_dir_path = $this->container->getParameter('core.root_path') . 'store/my_extension_dir';
682+
683+
try
684+
{
685+
$filesystem->remove($my_dir_path);
686+
}
687+
catch (\phpbb\filesystem\exception\filesystem_exception $e)
688+
{
689+
// log or handle any errors here using $e->get_filename() or $e->getMessage()
690+
}
691+
692+
return 'removed my_extension_dir';
693+
}
694+
}
695+
696+
By leveraging one of these methods, you can implement necessary filesystem changes while maintaining compatibility
697+
with phpBB’s structure and best practices.

0 commit comments

Comments
 (0)