Skip to content

Commit 940c2c3

Browse files
author
Andrew Welch
committed
refactor: Move to using ServicesTrait and add getter methods for services
1 parent 83accd2 commit 940c2c3

File tree

2 files changed

+87
-35
lines changed

2 files changed

+87
-35
lines changed

src/Transcoder.php

+10-35
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@
2727
use craft\utilities\ClearCaches;
2828
use craft\web\twig\variables\CraftVariable;
2929
use craft\web\UrlManager;
30-
use nystudio107\pluginvite\services\VitePluginService;
31-
use nystudio107\transcoder\assetbundles\transcoder\TranscoderAsset;
3230
use nystudio107\transcoder\models\Settings;
33-
use nystudio107\transcoder\services\Transcode;
31+
use nystudio107\transcoder\services\ServicesTrait;
3432
use nystudio107\transcoder\variables\TranscoderVariable;
3533
use yii\base\ErrorException;
3634
use yii\base\Event;
@@ -41,14 +39,14 @@
4139
* @author nystudio107
4240
* @package Transcode
4341
* @since 1.0.0
44-
*
45-
* @property Transcode $transcode
46-
* @property Settings $settings
47-
* @property VitePluginService $vite
48-
* @method Settings getSettings()
4942
*/
5043
class Transcoder extends Plugin
5144
{
45+
// Traits
46+
// =========================================================================
47+
48+
use ServicesTrait;
49+
5250
// Static Properties
5351
// =========================================================================
5452

@@ -62,46 +60,23 @@ class Transcoder extends Plugin
6260
*/
6361
public static ?Settings $settings;
6462

65-
// Static Methods
66-
// =========================================================================
67-
/**
68-
* @var string
69-
*/
70-
public string $schemaVersion = '1.0.0';
71-
7263
// Public Properties
7364
// =========================================================================
65+
7466
/**
7567
* @var bool
7668
*/
7769
public bool $hasCpSection = false;
70+
7871
/**
7972
* @var bool
8073
*/
8174
public bool $hasCpSettings = false;
8275

8376
/**
84-
* @inheritdoc
77+
* @var string
8578
*/
86-
public function __construct($id, $parent = null, array $config = [])
87-
{
88-
$config['components'] = [
89-
'transcode' => Transcode::class,
90-
// Register the vite service
91-
'vite' => [
92-
'class' => VitePluginService::class,
93-
'assetClass' => TranscoderAsset::class,
94-
'useDevServer' => true,
95-
'devServerPublic' => 'http://localhost:3001',
96-
'serverPublic' => 'http://localhost:8000',
97-
'errorEntry' => 'src/js/app.ts',
98-
'devServerInternal' => 'http://craft-transcoder-buildchain:3001',
99-
'checkDevServer' => true,
100-
],
101-
];
102-
103-
parent::__construct($id, $parent, $config);
104-
}
79+
public string $schemaVersion = '1.0.0';
10580

10681
// Public Methods
10782
// =========================================================================

src/services/ServicesTrait.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 nystudio107\pluginvite\services\VitePluginService;
14+
use nystudio107\transcoder\assetbundles\transcoder\TranscoderAsset;
15+
use yii\base\InvalidConfigException;
16+
17+
/**
18+
* @author nystudio107
19+
* @package Transcode
20+
* @since 1.2.23
21+
*
22+
* @property Transcode $transcode
23+
* @property VitePluginService $vite
24+
*/
25+
trait ServicesTrait
26+
{
27+
// Public Static Methods
28+
// =========================================================================
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public static function config(): array
34+
{
35+
return [
36+
'components' => [
37+
'transcode' => Transcode::class,
38+
// Register the vite service
39+
'vite' => [
40+
'class' => VitePluginService::class,
41+
'assetClass' => TranscoderAsset::class,
42+
'useDevServer' => true,
43+
'devServerPublic' => 'http://localhost:3001',
44+
'serverPublic' => 'http://localhost:8000',
45+
'errorEntry' => 'src/js/app.ts',
46+
'devServerInternal' => 'http://craft-transcoder-buildchain:3001',
47+
'checkDevServer' => true,
48+
],
49+
]
50+
];
51+
}
52+
53+
// Public Methods
54+
// =========================================================================
55+
56+
/**
57+
* Returns the transcode service
58+
*
59+
* @return Transcode The transcode service
60+
* @throws InvalidConfigException
61+
*/
62+
public function getTranscode(): Transcode
63+
{
64+
return $this->get('transcode');
65+
}
66+
67+
/**
68+
* Returns the vite service
69+
*
70+
* @return VitePluginService The vite service
71+
* @throws InvalidConfigException
72+
*/
73+
public function getVite(): VitePluginService
74+
{
75+
return $this->get('vite');
76+
}
77+
}

0 commit comments

Comments
 (0)