Add new documentation page about moving classes to src
This commit is contained in:
parent
70563e0324
commit
dfd4c8528e
|
@ -113,3 +113,8 @@ For Composer, this would be:
|
||||||
````
|
````
|
||||||
$> COMPOSER_HOME=/var/tmp/composer sudo -u [web user] util/composer.phar [mode]
|
$> COMPOSER_HOME=/var/tmp/composer sudo -u [web user] util/composer.phar [mode]
|
||||||
````
|
````
|
||||||
|
|
||||||
|
## Related
|
||||||
|
|
||||||
|
* [Class autoloading](help/autoloader)
|
||||||
|
* [How To Move Classes to `src`](help/Developer-How-To-Move-Classes-to-src)
|
||||||
|
|
|
@ -0,0 +1,105 @@
|
||||||
|
How To Move Classes to `src`
|
||||||
|
==============
|
||||||
|
|
||||||
|
* [Home](help)
|
||||||
|
* [Developer Intro](help/Developers-Intro)
|
||||||
|
|
||||||
|
Since April 2017, Friendica uses [Composer](help/Composer) to manage autoloading.
|
||||||
|
This means that all the PHP class files moved to the `src` folder will be [automatically included](help/autoloader) when the class it defines is first used in the flow.
|
||||||
|
This is an improvement over the current `require` usage since files will be included on an actual usage basis instead of the presence of a `require` call.
|
||||||
|
|
||||||
|
However, there are a significant number of items to check when moving a class file from the `include` folder to the `src` folder, and this page is there to list them.
|
||||||
|
|
||||||
|
## Decide the namespace
|
||||||
|
|
||||||
|
This isn't the most technical decision of them all, but it has long lasting consequences as it will be the name that will be used to refer to this class from now on.
|
||||||
|
There is [a shared Ethercalc sheet](https://ethercalc.org/friendica_classes) to suggest namespace/class names that lists all the already moved class files for inspiration.
|
||||||
|
|
||||||
|
A few pointers though:
|
||||||
|
* `Friendica` is the base namespace for all classes in the `src` folder
|
||||||
|
* Namespaces match the directory structure, with `Friendica` namespace being the base `src` directory. The `Config` class set in the `Friendica\Core` namespace is expected to be found at `src/Core/Config.php`.
|
||||||
|
* Namespaces can help group classes with a similar purpose or relevant to a particular feature
|
||||||
|
|
||||||
|
When you're done deciding the namespace, it's time to use it.
|
||||||
|
Let's say we choose `Friendica\Core` for the `Config` class.
|
||||||
|
|
||||||
|
## Use the namespace
|
||||||
|
|
||||||
|
To declare the namespace, the file `src/Core/Config.php` must start with the following statement:
|
||||||
|
|
||||||
|
````php
|
||||||
|
namespace Friendica\Core;
|
||||||
|
````
|
||||||
|
|
||||||
|
From now on, the `Config` class can be referred to as `Friendica\Core\Config`, however it isn't very practical, especially when the class was previously used as `Config`.
|
||||||
|
Thankfully, PHP provides namespace shortcuts through `use`.
|
||||||
|
|
||||||
|
This language construct just provides a different naming scheme for a namespace or a class, but doesn't trigger the autoload mechanism on its own.
|
||||||
|
Here are the different ways you can use `use`:
|
||||||
|
|
||||||
|
````php
|
||||||
|
// No use
|
||||||
|
$config = new Friendica\Core\Config();
|
||||||
|
````
|
||||||
|
````php
|
||||||
|
// Namespace shortcut
|
||||||
|
use Friendica\Core;
|
||||||
|
|
||||||
|
$config = new Core\Config();
|
||||||
|
````
|
||||||
|
````php
|
||||||
|
// Class name shortcut
|
||||||
|
use Friendica\Core\Config;
|
||||||
|
|
||||||
|
$config = new Config();
|
||||||
|
````
|
||||||
|
````php
|
||||||
|
// Aliasing
|
||||||
|
use Friendica\Core\Config as Cfg;
|
||||||
|
|
||||||
|
$config = new Cfg();
|
||||||
|
````
|
||||||
|
|
||||||
|
Whatever the style chosen, a repository-wide search has to be done to find all the class name usage and either use the fully-qualified class name (including the namespace) or add a `use` statement at the start of each relevant file.
|
||||||
|
|
||||||
|
## Escape non-namespace classes
|
||||||
|
|
||||||
|
The class file you just moved is now in the `Friendica` namespace, but it probably isn't the case for all the classes referenced in this file.
|
||||||
|
Since we added a `namespace Friendica\Core;` to the file, all the class names still declared in `include` will be implicitly understood as `Friendica\Core\ClassName`, which is rarely what we expect.
|
||||||
|
|
||||||
|
To avoid `Class Friendica\Core\ClassName not found` errors, all the `include`-declared class names have to be prepended with a `\`, it tells the autoloader not to look for the class in the namespace but in the global space where non-namespaced classes are set.
|
||||||
|
If there are only a handful of references to a single non-namespaced class, just prepending `\` is enough. However, if there are many instance, we can use `use` again.
|
||||||
|
|
||||||
|
````php
|
||||||
|
namespace Friendica\Core;
|
||||||
|
...
|
||||||
|
if (\dbm::is_result($r)) {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
````
|
||||||
|
````php
|
||||||
|
namespace Friendica\Core;
|
||||||
|
|
||||||
|
use \dbm;
|
||||||
|
|
||||||
|
if (dbm::is_result($r)) {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
|
## Remove any useless `require`
|
||||||
|
|
||||||
|
Now that you successfully moved your class to the autoloaded `src` folder, there's no need to include this file anywhere in the app ever again.
|
||||||
|
Please remove all the `require_once` mentions of the former file, as they will provoke a Fatal Error even if the class isn't used.
|
||||||
|
|
||||||
|
## Miscellaneous tips
|
||||||
|
|
||||||
|
When you are done with moving the class, please run `php util/typo.php` from the Friendica base directory to check for obvious mistakes.
|
||||||
|
Howevever, this tool isn't bullet-proof, and a staging install of Friendica is recommended to test your class move without impairing your production server if you host one.
|
||||||
|
|
||||||
|
Most of Friendica processes are run in the background, so make sure to turn on your debug log to check for errors that wouldn't show up while simply browsing Friendica.
|
||||||
|
|
||||||
|
## Related
|
||||||
|
|
||||||
|
* [Class autoloading](help/autoloader)
|
||||||
|
* [Using Composer](help/Composer)
|
|
@ -52,7 +52,9 @@ Friendica uses [Composer](https://getcomposer.org) to manage dependencies librar
|
||||||
|
|
||||||
It's a command-line tool that downloads required libraries into the `vendor` folder and makes any namespaced class in `src` available through the whole application through `boot.php`.
|
It's a command-line tool that downloads required libraries into the `vendor` folder and makes any namespaced class in `src` available through the whole application through `boot.php`.
|
||||||
|
|
||||||
|
* [Class autoloading](help/autoloader)
|
||||||
* [Using Composer](help/Composer)
|
* [Using Composer](help/Composer)
|
||||||
|
* [How To Move Classes to `src`](help/Developer-How-To-Move-Classes-to-src)
|
||||||
|
|
||||||
###Coding standards
|
###Coding standards
|
||||||
|
|
||||||
|
|
16
doc/Home.md
16
doc/Home.md
|
@ -36,21 +36,25 @@ Friendica Documentation and Resources
|
||||||
|
|
||||||
**Developer Manual**
|
**Developer Manual**
|
||||||
|
|
||||||
* [Where to get started?](help/Developers-Intro)
|
* [Get started](help/Developers-Intro)
|
||||||
|
* Set up development environment
|
||||||
* [Help on Github](help/Github)
|
* [Help on Github](help/Github)
|
||||||
* [Help on Vagrant](help/Vagrant)
|
* [Help on Vagrant](help/Vagrant)
|
||||||
* [How to translate Friendica](help/translations)
|
|
||||||
* [Bugs and Issues](help/Bugs-and-Issues)
|
* [Bugs and Issues](help/Bugs-and-Issues)
|
||||||
|
* Code structure
|
||||||
* [Plugin Development](help/Plugins)
|
* [Plugin Development](help/Plugins)
|
||||||
* [Theme Development](help/themes)
|
* [Theme Development](help/themes)
|
||||||
* [Smarty 3 Templates](help/smarty3-templates)
|
* [Smarty 3 Templates](help/smarty3-templates)
|
||||||
|
* How To
|
||||||
|
* [Translate Friendica](help/translations)
|
||||||
|
* [Use Composer](help/Composer)
|
||||||
|
* [Move classes to `src`](help/Developer-How-To-Move-Classes-to-src)
|
||||||
|
* Reference
|
||||||
|
* [Twitter/GNU Social API Functions](help/api)
|
||||||
|
* [Code (Doxygen generated - sets cookies)](doc/html/)
|
||||||
* [Protocol Documentation](help/Protocol)
|
* [Protocol Documentation](help/Protocol)
|
||||||
* [Database schema documantation](help/database)
|
* [Database schema documantation](help/database)
|
||||||
* [Class Autoloading](help/autoloader)
|
* [Class Autoloading](help/autoloader)
|
||||||
* [Using Composer](help/Composer)
|
|
||||||
* [Code - Reference(Doxygen generated - sets cookies)](doc/html/)
|
|
||||||
* [Twitter/GNU Social API Functions](help/api)
|
|
||||||
|
|
||||||
|
|
||||||
**External Resources**
|
**External Resources**
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ Let's say you have a PHP file in `src/` that define a very useful class:
|
||||||
```php
|
```php
|
||||||
// src/ItemsManager.php
|
// src/ItemsManager.php
|
||||||
<?php
|
<?php
|
||||||
namespace \Friendica;
|
namespace Friendica;
|
||||||
|
|
||||||
class ItemsManager {
|
class ItemsManager {
|
||||||
public function getAll() { ... }
|
public function getAll() { ... }
|
||||||
|
@ -47,7 +47,7 @@ The code will be something like:
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
function network_content(App $a) {
|
function network_content(App $a) {
|
||||||
$itemsmanager = new \Friendica\ItemsManager();
|
$itemsmanager = new Friendica\ItemsManager();
|
||||||
$items = $itemsmanager->getAll();
|
$items = $itemsmanager->getAll();
|
||||||
|
|
||||||
// pass $items to template
|
// pass $items to template
|
||||||
|
@ -63,7 +63,7 @@ Going further: now we have a bunch of `*Manager` classes that cause some code du
|
||||||
```php
|
```php
|
||||||
// src/BaseManager.php
|
// src/BaseManager.php
|
||||||
<?php
|
<?php
|
||||||
namespace \Friendica;
|
namespace Friendica;
|
||||||
|
|
||||||
class BaseManager {
|
class BaseManager {
|
||||||
public function thatFunctionEveryManagerUses() { ... }
|
public function thatFunctionEveryManagerUses() { ... }
|
||||||
|
@ -75,7 +75,7 @@ and then let's change the ItemsManager class to use this code
|
||||||
```php
|
```php
|
||||||
// src/ItemsManager.php
|
// src/ItemsManager.php
|
||||||
<?php
|
<?php
|
||||||
namespace \Friendica;
|
namespace Friendica;
|
||||||
|
|
||||||
class ItemsManager extends BaseManager {
|
class ItemsManager extends BaseManager {
|
||||||
public function getAll() { ... }
|
public function getAll() { ... }
|
||||||
|
@ -89,7 +89,7 @@ It works with the "BaseManager" example here and it works when we need to call s
|
||||||
```php
|
```php
|
||||||
// src/Dfrn.php
|
// src/Dfrn.php
|
||||||
<?php
|
<?php
|
||||||
namespace \Friendica;
|
namespace Friendica;
|
||||||
|
|
||||||
class Dfrn {
|
class Dfrn {
|
||||||
public static function mail($item, $owner) { ... }
|
public static function mail($item, $owner) { ... }
|
||||||
|
@ -102,7 +102,7 @@ It works with the "BaseManager" example here and it works when we need to call s
|
||||||
|
|
||||||
mail_post($a){
|
mail_post($a){
|
||||||
...
|
...
|
||||||
\Friendica\dfrn::mail($item, $owner);
|
Friendica\dfrn::mail($item, $owner);
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -113,7 +113,7 @@ If your code is in same namespace as the class you need, you don't need to prepe
|
||||||
// include/delivery.php
|
// include/delivery.php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace \Friendica;
|
namespace Friendica;
|
||||||
|
|
||||||
// this is the same content of current include/delivery.php,
|
// this is the same content of current include/delivery.php,
|
||||||
// but has been declared to be in "Friendica" namespace
|
// but has been declared to be in "Friendica" namespace
|
||||||
|
@ -139,7 +139,7 @@ But if you want to use classes from another library, you need to use the full na
|
||||||
// src/Diaspora.php
|
// src/Diaspora.php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace \Friendica;
|
namespace Friendica;
|
||||||
|
|
||||||
class Diaspora {
|
class Diaspora {
|
||||||
public function md2bbcode() {
|
public function md2bbcode() {
|
||||||
|
@ -153,7 +153,7 @@ if you use that class in many places of the code and you don't want to write the
|
||||||
```php
|
```php
|
||||||
// src/Diaspora.php
|
// src/Diaspora.php
|
||||||
<?php
|
<?php
|
||||||
namespace \Friendica;
|
namespace Friendica;
|
||||||
|
|
||||||
use \Michelf\MarkdownExtra;
|
use \Michelf\MarkdownExtra;
|
||||||
|
|
||||||
|
@ -170,23 +170,28 @@ You can go deeper if you want to, like:
|
||||||
```
|
```
|
||||||
// src/Network/Dfrn.php
|
// src/Network/Dfrn.php
|
||||||
<?php
|
<?php
|
||||||
namespace \Friendica\Network;
|
namespace Friendica\Network;
|
||||||
|
|
||||||
class Dfrn {
|
class Dfrn {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Please note that the location of the file defining the class must be placed in the appropriate sub-folders of `src` if the namespace isn't plain `\Friendica`.
|
Please note that the location of the file defining the class must be placed in the appropriate sub-folders of `src` if the namespace isn't plain `Friendica`.
|
||||||
|
|
||||||
or
|
or
|
||||||
|
|
||||||
```
|
```
|
||||||
// src/Dba/Mysql
|
// src/Dba/Mysql
|
||||||
<?php
|
<?php
|
||||||
namespace \Friendica\Dba;
|
namespace Friendica\Dba;
|
||||||
|
|
||||||
class Mysql {
|
class Mysql {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
So you can think of namespaces as folders in a Unix file system, with global scope as the root ("\").
|
So you can think of namespaces as folders in a Unix file system, with global scope as the root ("\").
|
||||||
|
|
||||||
|
## Related
|
||||||
|
|
||||||
|
* [Using Composer](help/Composer)
|
||||||
|
* [How To Move Classes to `src`](help/Developer-How-To-Move-Classes-to-src)
|
|
@ -90,7 +90,7 @@ function help_content(App $a) {
|
||||||
for($k=0;$k<$lastlevel; $k++) $toc.="</ul>";
|
for($k=0;$k<$lastlevel; $k++) $toc.="</ul>";
|
||||||
$html = implode("\n",$lines);
|
$html = implode("\n",$lines);
|
||||||
|
|
||||||
$a->page['aside'] = $toc.$a->page['aside'];
|
$a->page['aside'] = '<section class="help-aside-wrapper">' . $toc . $a->page['aside'] . '</section>';
|
||||||
}
|
}
|
||||||
|
|
||||||
$html = "
|
$html = "
|
||||||
|
|
|
@ -98,7 +98,7 @@ blockquote {
|
||||||
code {
|
code {
|
||||||
white-space: pre-wrap;
|
white-space: pre-wrap;
|
||||||
}
|
}
|
||||||
.help-content-wrapper code {display: inline}
|
.help-content-wrapper code, .help-aside-wrapper code {display: inline}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* standard page elements
|
* standard page elements
|
||||||
|
|
Loading…
Reference in New Issue
Block a user