Skip to content

Commit a945ce1

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

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

Diff for: development/extensions/tutorial_advanced.rst

+175
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,177 @@ 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 ``acme_demo_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_acme_demo_dir extends \phpbb\db\migration\container_aware_migration
561+
{
562+
/** @var \phpbb\filesystem\filesystem */
563+
protected $filesystem;
564+
565+
/** @var string */
566+
protected $acme_demo_dir;
567+
568+
/**
569+
* Initialize class properties
570+
*/
571+
protected function init()
572+
{
573+
if (!isset($this->filesystem))
574+
{
575+
$this->filesystem = $this->container->get('filesystem');
576+
$this->acme_demo_dir = $this->container->getParameter('core.root_path') . 'store/acme_demo_dir';
577+
}
578+
}
579+
580+
public function effectively_installed()
581+
{
582+
$this->init();
583+
return $this->filesystem->exists($this->acme_demo_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->acme_demo_dir, 0755);
612+
$this->filesystem->touch($this->acme_demo_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->acme_demo_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+
/**
651+
* Create acme_demo_dir when extension is enabled
652+
*/
653+
public function enable_step($old_state)
654+
{
655+
if ($old_state !== false)
656+
{
657+
return parent::enable_step($old_state);
658+
}
659+
660+
$filesystem = $this->container->get('filesystem');
661+
$my_dir_path = $this->container->getParameter('core.root_path') . 'store/acme_demo_dir';
662+
663+
try
664+
{
665+
$filesystem->mkdir($my_dir_path, 0755);
666+
$filesystem->touch($my_dir_path . '/index.htm');
667+
}
668+
catch (\phpbb\filesystem\exception\filesystem_exception $e)
669+
{
670+
// log or handle any errors here using $e->get_filename() or $e->getMessage()
671+
}
672+
673+
return 'added acme_demo_dir';
674+
}
675+
676+
/**
677+
* Delete acme_demo_dir when deleting extension data
678+
*/
679+
public function purge_step($old_state)
680+
{
681+
if ($old_state !== false)
682+
{
683+
return parent::purge_step($old_state);
684+
}
685+
686+
$filesystem = $this->container->get('filesystem');
687+
$my_dir_path = $this->container->getParameter('core.root_path') . 'store/acme_demo_dir';
688+
689+
try
690+
{
691+
$filesystem->remove($my_dir_path);
692+
}
693+
catch (\phpbb\filesystem\exception\filesystem_exception $e)
694+
{
695+
// log or handle any errors here using $e->get_filename() or $e->getMessage()
696+
}
697+
698+
return 'removed acme_demo_dir';
699+
}
700+
}
701+
702+
By leveraging one of these methods, you can implement necessary filesystem changes while maintaining compatibility
703+
with phpBB’s structure and best practices.

0 commit comments

Comments
 (0)