Skip to content

Commit c93a262

Browse files
author
Andrew Welch
committed
refactor: Move to using ServicesTrait and add getter methods for services
1 parent 8a6a01a commit c93a262

File tree

2 files changed

+85
-34
lines changed

2 files changed

+85
-34
lines changed

src/Transcoder.php

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@
2626
use craft\utilities\ClearCaches;
2727
use craft\web\twig\variables\CraftVariable;
2828
use craft\web\UrlManager;
29-
use nystudio107\pluginvite\services\VitePluginService;
30-
use nystudio107\transcoder\assetbundles\transcoder\TranscoderAsset;
3129
use nystudio107\transcoder\models\Settings;
32-
use nystudio107\transcoder\services\Transcode;
30+
use nystudio107\transcoder\services\ServicesTrait;
3331
use nystudio107\transcoder\variables\TranscoderVariable;
3432
use yii\base\ErrorException;
3533
use yii\base\Event;
@@ -40,14 +38,14 @@
4038
* @author nystudio107
4139
* @package Transcode
4240
* @since 1.0.0
43-
*
44-
* @property Transcode $transcode
45-
* @property Settings $settings
46-
* @property VitePluginService $vite
47-
* @method Settings getSettings()
4841
*/
4942
class Transcoder extends Plugin
5043
{
44+
// Traits
45+
// =========================================================================
46+
47+
use ServicesTrait;
48+
5149
// Static Properties
5250
// =========================================================================
5351

@@ -61,32 +59,6 @@ class Transcoder extends Plugin
6159
*/
6260
public static $settings;
6361

64-
// Static Methods
65-
// =========================================================================
66-
67-
/**
68-
* @inheritdoc
69-
*/
70-
public function __construct($id, $parent = null, array $config = [])
71-
{
72-
$config['components'] = [
73-
'transcode' => Transcode::class,
74-
// Register the vite service
75-
'vite' => [
76-
'class' => VitePluginService::class,
77-
'assetClass' => TranscoderAsset::class,
78-
'useDevServer' => true,
79-
'devServerPublic' => 'http://localhost:3001',
80-
'serverPublic' => 'http://localhost:8000',
81-
'errorEntry' => 'src/js/app.ts',
82-
'devServerInternal' => 'http://craft-transcoder-buildchain:3001',
83-
'checkDevServer' => true,
84-
],
85-
];
86-
87-
parent::__construct($id, $parent, $config);
88-
}
89-
9062
// Public Properties
9163
// =========================================================================
9264

src/services/ServicesTrait.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Transcoder plugin for Craft CMS
4+
*
5+
* Transcode videos to various formats, and provide thumbnails of the video
6+
*
7+
* @link https://nystudio107.com
8+
* @copyright Copyright (c) 2022 nystudio107
9+
*/
10+
11+
namespace nystudio107\transcoder\services;
12+
13+
use craft\helpers\ArrayHelper;
14+
use nystudio107\pluginvite\services\VitePluginService;
15+
use nystudio107\transcoder\assetbundles\transcoder\TranscoderAsset;
16+
use yii\base\InvalidConfigException;
17+
18+
/**
19+
* @author nystudio107
20+
* @package Transcode
21+
* @since 1.2.23
22+
*
23+
* @property Transcode $transcode
24+
* @property VitePluginService $vite
25+
*/
26+
trait ServicesTrait
27+
{
28+
// Public Methods
29+
// =========================================================================
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function __construct($id, $parent = null, array $config = [])
35+
{
36+
// Merge in the passed config, so it our config can be overridden by Plugins::pluginConfigs['vite']
37+
// ref: https://github.com/craftcms/cms/issues/1989
38+
$config = ArrayHelper::merge([
39+
'components' => [
40+
'transcode' => Transcode::class,
41+
// Register the vite service
42+
'vite' => [
43+
'class' => VitePluginService::class,
44+
'assetClass' => TranscoderAsset::class,
45+
'useDevServer' => true,
46+
'devServerPublic' => 'http://localhost:3001',
47+
'serverPublic' => 'http://localhost:8000',
48+
'errorEntry' => 'src/js/app.ts',
49+
'devServerInternal' => 'http://craft-transcoder-buildchain:3001',
50+
'checkDevServer' => true,
51+
],
52+
]
53+
], $config);
54+
55+
parent::__construct($id, $parent, $config);
56+
}
57+
58+
/**
59+
* Returns the transcode service
60+
*
61+
* @return Transcode The transcode service
62+
* @throws InvalidConfigException
63+
*/
64+
public function getTranscode(): Transcode
65+
{
66+
return $this->get('transcode');
67+
}
68+
69+
/**
70+
* Returns the vite service
71+
*
72+
* @return VitePluginService The vite service
73+
* @throws InvalidConfigException
74+
*/
75+
public function getVite(): VitePluginService
76+
{
77+
return $this->get('vite');
78+
}
79+
}

0 commit comments

Comments
 (0)