Skip to content

Commit 8c459cf

Browse files
authored
Merge pull request #8928 from ddevsr/docs-service
docs: replace `Services` class to `service()`
2 parents 4dd9a87 + 1c27fd4 commit 8c459cf

File tree

62 files changed

+96
-86
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+96
-86
lines changed

user_guide_src/source/concepts/services.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ come in handy.
3737
Instead of creating the instance ourself, we let a central class create an instance of the
3838
class for us. This class is kept very simple. It only contains a method for each class that we want
3939
to use as a service. The method typically returns a **shared instance** of that class, passing any dependencies
40-
it might have into it. Then, we would replace our timer creation code with code that calls this new class:
40+
it might have into it. Then, we would replace our timer creation code with code that calls this global function or Services class:
4141

4242
.. literalinclude:: services/002.php
4343

@@ -55,7 +55,7 @@ As many CodeIgniter classes are provided as services, you can get them like the
5555

5656
.. literalinclude:: services/013.php
5757

58-
The ``$typography`` is an instance of the Typography class, and if you call ``\Config\Services::typography()`` again, you will get the exactly same instance.
58+
The ``$timer`` is an instance of the Timer class, and if you call ``service('timer')`` again, you will get the exactly same instance.
5959

6060
The Services typically return a **shared instance** of the class. The following code creates a ``CURLRequest`` instance at the first call. And the second call returns the exactly same instance.
6161

@@ -66,7 +66,7 @@ Therefore, the parameter ``$options2`` for the ``$client2`` does not work. It is
6666
Getting a New Instance
6767
======================
6868

69-
If you want to get a new instance of the Typography class, you need to pass ``false`` to the argument ``$getShared``:
69+
If you want to get a new instance of the Timer class, you need to pass ``false`` to the argument ``$getShared``:
7070

7171
.. literalinclude:: services/014.php
7272

@@ -85,6 +85,8 @@ always return the same instance:
8585

8686
.. literalinclude:: services/003.php
8787

88+
.. note:: Since v4.5.0, when you don't pass parameters to the service, the global function ``service()`` is recommended due to performance improvements.
89+
8890
If the creation method requires additional parameters, they can be passed after the service name:
8991

9092
.. literalinclude:: services/004.php
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<?php
22

3+
$timer = service('timer');
4+
5+
// The code above is the same as the code below.
36
$timer = \Config\Services::timer();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$postManager = \Config\Services::postManager();
3+
$postManager = service('postManager');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$typography = \Config\Services::typography();
3+
$timer = service('timer');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$typography = \Config\Services::typography(false);
3+
$timer = \Config\Services::timer(false);

user_guide_src/source/concepts/services/015.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
'baseURI' => 'http://example.com/api/v1/',
55
'timeout' => 3,
66
];
7-
$client1 = \Config\Services::curlrequest($options1);
7+
$client1 = service('curlrequest', $options1);
88

99
$options2 = [
1010
'baseURI' => 'http://another.example.com/api/v2/',
1111
'timeout' => 10,
1212
];
13-
$client2 = \Config\Services::curlrequest($options2);
13+
$client2 = service('curlrequest', $options2);
1414
// $options2 does not work.
1515
// $client2 is the exactly same instance as $client1.

user_guide_src/source/extending/basecontroller/003.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public function initController(/* ... */)
1818
// Do Not Edit This Line
1919
parent::initController($request, $response, $logger);
2020

21-
$this->session = \Config\Services::session();
21+
$this->session = service('session');
2222
}
2323
}

user_guide_src/source/general/common_functions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ Miscellaneous Functions
372372
:returns: The shared Request object.
373373
:rtype: IncomingRequest|CLIRequest
374374

375-
This function is a wrapper for ``Services::request()``.
375+
This function is a wrapper for ``Services::request()`` and ``service('request')``.
376376

377377
.. php:function:: response()
378378
@@ -381,7 +381,7 @@ Miscellaneous Functions
381381
:returns: The shared Response object.
382382
:rtype: Response
383383

384-
This function is a wrapper for ``Services::response()``.
384+
This function is a wrapper for ``Services::response()`` and ``service('response')``.
385385

386386
.. php:function:: route_to($method[, ...$params])
387387

user_guide_src/source/general/errors/018.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$response = \Config\Services::response()
3+
$response = service('response')
44
->redirect('https://example.com/path')
55
->setHeader('Some', 'header')
66
->setCookie('and', 'cookie');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$negotiate = \Config\Services::negotiator();
3+
$negotiate = service('negotiator');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$request = \Config\Services::request();
3+
$request = service('request');

user_guide_src/source/incoming/incomingrequest/003.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ public function __construct(RequestInterface $request)
1414
}
1515
}
1616

17-
$someClass = new SomeClass(\Config\Services::request());
17+
$someClass = new SomeClass(service('request'));

user_guide_src/source/libraries/caching.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The following example shows a common usage pattern within your controllers.
1919

2020
.. literalinclude:: caching/001.php
2121

22-
You can grab an instance of the cache engine directly through the Services class:
22+
You can grab an instance of the cache engine directly through the global function ``service()``:
2323

2424
.. literalinclude:: caching/002.php
2525

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

3-
$cache = \Config\Services::cache();
3+
$cache = service('cache');
44

55
$foo = $cache->get('foo');

user_guide_src/source/libraries/cookies/002.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Config\Cookie as CookieConfig;
55

66
// pass in a Config\Cookie instance before constructing a Cookie class
7-
Cookie::setDefaults(new CookieConfig());
7+
Cookie::setDefaults(config(CookieConfig::class));
88
$cookie = new Cookie('login_token');
99

1010
// pass in an array of defaults

user_guide_src/source/libraries/cookies/003.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use CodeIgniter\Cookie\Cookie;
44
use Config\Cookie as CookieConfig;
55

6-
$oldDefaults = Cookie::setDefaults(new CookieConfig());
6+
$oldDefaults = Cookie::setDefaults(config(CookieConfig::class));
77
$cookie = new Cookie('my_token', 'muffins');
88

99
// return the old defaults
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
<?php
22

3-
use Config\Services;
4-
5-
$cookieStore = Services::response()->getCookieStore();
3+
$cookieStore = service('response')->getCookieStore();

user_guide_src/source/libraries/cookies/009.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use CodeIgniter\Cookie\Cookie;
44
use CodeIgniter\Cookie\CookieStore;
5-
use Config\Services;
65

76
// check if cookie is in the current cookie collection
87
$store = new CookieStore([
@@ -13,7 +12,7 @@
1312

1413
// check if cookie is in the current Response's cookie collection
1514
cookies()->has('login_token');
16-
Services::response()->hasCookie('remember_token');
15+
service('response')->hasCookie('remember_token');
1716

1817
// using the cookie helper to check the current Response
1918
// not available to v4.1.1 and lower

user_guide_src/source/libraries/cookies/010.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
// getting cookie in the current Response's cookie collection
1515
cookies()->get('login_token');
16-
Services::response()->getCookie('remember_token');
16+
service('response')->getCookie('remember_token');
1717

1818
// using the cookie helper to get cookie from the Response's cookie collection
1919
helper('cookie');
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<?php
22

3-
use Config\Services;
4-
53
cookies()->get(); // array of Cookie objects
64

75
// alternatively, you can use the display method
86
cookies()->display();
97

108
// or even from the Response
11-
Services::response()->getCookies();
9+
service('response')->getCookies();

user_guide_src/source/libraries/cookies/014.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use CodeIgniter\Cookie\Cookie;
44
use CodeIgniter\Cookie\CookieStore;
5-
use Config\Services;
65

76
$store = new CookieStore([
87
new Cookie('login_token'),

user_guide_src/source/libraries/cookies/015.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
use Config\Services;
4-
5-
Services::response()->setCookie('admin_token', 'yes');
6-
Services::response()->deleteCookie('login_token');
3+
service('response')->setCookie('admin_token', 'yes');
4+
service('response')->deleteCookie('login_token');
75

86
// using the cookie helper
97
helper('cookie');

user_guide_src/source/libraries/cookies/017.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22

33
use CodeIgniter\Cookie\Cookie;
4-
use Config\Services;
54

6-
$response = Services::response();
5+
$response = service('response');
76

87
$cookie = new Cookie(
98
'remember_token',

user_guide_src/source/libraries/curlrequest.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Loading the Library
5050

5151
The library can be loaded either manually or through the :doc:`Services class </concepts/services>`.
5252

53-
To load with the Services class call the ``curlrequest()`` method:
53+
To load with the Services class call the ``curlrequest()`` method or global function ``service()``:
5454

5555
.. literalinclude:: curlrequest/002.php
5656

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<?php
22

3+
$client = service('curlrequest'); // Since v4.5.0, this code is recommended due to performance improvements
4+
5+
// The code above is the same as the code below.
36
$client = \Config\Services::curlrequest();

user_guide_src/source/libraries/curlrequest/003.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
'baseURI' => 'http://example.com/api/v1/',
55
'timeout' => 3,
66
];
7-
$client = \Config\Services::curlrequest($options);
7+
$client = service('curlrequest', $options);
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3+
use Config\App;
4+
35
$client = new \CodeIgniter\HTTP\CURLRequest(
4-
new \Config\App(),
6+
config(App::class),
57
new \CodeIgniter\HTTP\URI(),
6-
new \CodeIgniter\HTTP\Response(new \Config\App()),
8+
new \CodeIgniter\HTTP\Response(config(App::class)),
79
$options
810
);

user_guide_src/source/libraries/curlrequest/005.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$client = \Config\Services::curlrequest();
3+
$client = service('curlrequest');
44

55
$response = $client->request('GET', 'https://api.github.com/user', [
66
'auth' => ['user', 'pass'],

user_guide_src/source/libraries/curlrequest/008.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$client = \Config\Services::curlrequest([
3+
$client = service('curlrequest', [
44
'baseURI' => 'https://example.com/api/v1/',
55
]);
66

user_guide_src/source/libraries/email/001.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$email = \Config\Services::email();
3+
$email = service('email');
44

55
$email->setFrom('[email protected]', 'Your Name');
66
$email->setTo('[email protected]');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$encrypter = \Config\Services::encrypter();
3+
$encrypter = service('encrypter');
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

3-
$config = new \Config\Encryption();
3+
use Config\Encryption;
4+
5+
$config = config(Encryption::class);
46
$config->key = 'aBigsecret_ofAtleast32Characters';
57
$config->driver = 'OpenSSL';
68

7-
$encrypter = \Config\Services::encrypter($config);
9+
$encrypter = service('encrypter', $config);

user_guide_src/source/libraries/encryption/013.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
use Config\Encryption;
4-
use Config\Services;
54

65
$config = new Encryption();
76
$config->driver = 'OpenSSL';
@@ -15,4 +14,4 @@
1514
$config->encryptKeyInfo = 'encryption';
1615
$config->authKeyInfo = 'authentication';
1716

18-
$encrypter = Services::encrypter($config, false);
17+
$encrypter = service('encrypter', $config);

user_guide_src/source/libraries/images.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ Initializing the Class
2222
**********************
2323

2424
Like most other classes in CodeIgniter, the image class is initialized
25-
in your controller by calling the Services class:
25+
in your controller by calling the global function ``service()``:
2626

2727
.. literalinclude:: images/001.php
2828

29-
You can pass the alias for the image library you wish to use into the
30-
Service function:
29+
You can pass the alias for the image library you wish to use into the global
30+
function ``service()``:
3131

3232
.. literalinclude:: images/002.php
3333

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$image = \Config\Services::image();
3+
$image = service('image');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$image = \Config\Services::image('imagick');
3+
$image = service('image', 'imagick');

user_guide_src/source/libraries/images/007.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$image = \Config\Services::image();
3+
$image = service('image');
44

55
try {
66
$image->withFile('/path/to/image/mypic.jpg')
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
$info = \Config\Services::image('imagick')
3+
$info = service('image', 'imagick')
44
->withFile('/path/to/image/mypic.jpg')
55
->getFile()
66
->getProperties(true);
77

88
$xOffset = ($info['width'] / 2) - 25;
99
$yOffset = ($info['height'] / 2) - 25;
1010

11-
\Config\Services::image('imagick')
11+
service('image', 'imagick')
1212
->withFile('/path/to/image/mypic.jpg')
1313
->crop(50, 50, $xOffset, $yOffset)
1414
->save('/path/to/new/image.jpg');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
\Config\Services::image()
3+
service('image')
44
->withFile('/path/to/image/mypic.jpg')
55
->convert(IMAGETYPE_PNG)
66
->save('/path/to/new/image.png');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
\Config\Services::image('imagick')
3+
service('image', 'imagick')
44
->withFile('/path/to/image/mypic.jpg')
55
->fit(100, 150, 'left')
66
->save('/path/to/new/image.jpg');

0 commit comments

Comments
 (0)