diff --git a/composer.json b/composer.json index 9a9bd53..2fb9803 100755 --- a/composer.json +++ b/composer.json @@ -1,49 +1,45 @@ { - "name": "fostercommerce/entry-type-rules", - "description": "A Craft plugin that allows you to set rules on number of entry types in a Craft section and/or limit who can include entry type entries based on their user group.", - "type": "craft-plugin", - "version": "3.0.0", - "keywords": [ - "craft", - "cms", - "craftcms", - "craft-plugin", - "entry type", - "rules" - ], - "support": { - "docs": "https://github.com/FosterCommerce/entry-type-rules/blob/main/README.md", - "issues": "https://github.com/FosterCommerce/entry-type-rules/issues" - }, - "license": "proprietary", - "authors": [ - { - "name": "Foster Commerce", - "homepage": "https://fostercommerce.com" - } - ], - "require": { - "php": "^8.2", - "craftcms/cms": "^5.0" - }, - "autoload": { - "psr-4": { - "fostercommerce\\entrytyperules\\": "src/" - } - }, - "extra": { - "name": "Entry Type Rules", - "handle": "entry-type-rules", - "developer": "Foster Commerce", - "developerUrl": "https://fostercommerce.com", - "documentationUrl": "https://github.com/FosterCommerce/entry-type-rules/blob/main/README.md", - "changelogUrl": "https://raw.githubusercontent.com/FosterCommerce/entry-type-rules/main/CHANGELOG.md", - "components": { - "entryTypeRulesService": "fostercommerce\\entrytyperules\\services\\EntryTypeRulesService" - }, - "class": "fostercommerce\\entrytyperules\\EntryTypeRules" - }, - "require-dev": { + "name": "fostercommerce/entry-type-rules", + "description": "A Craft plugin that allows you to set rules on number of entry types in a Craft section and/or limit who can include entry type entries based on their user group.", + "type": "craft-plugin", + "version": "3.0.0", + "keywords": [ + "craft", + "cms", + "craftcms", + "craft-plugin", + "entry type", + "rules" + ], + "support": { + "docs": "https://github.com/FosterCommerce/entry-type-rules/blob/main/README.md", + "issues": "https://github.com/FosterCommerce/entry-type-rules/issues" + }, + "license": "proprietary", + "authors": [ + { + "name": "Foster Commerce", + "homepage": "https://fostercommerce.com" + } + ], + "require": { + "php": "^8.2", + "craftcms/cms": "^5.0" + }, + "autoload": { + "psr-4": { + "fostercommerce\\entrytyperules\\": "src/" + } + }, + "extra": { + "name": "Entry Type Rules", + "handle": "entry-type-rules", + "developer": "Foster Commerce", + "developerUrl": "https://fostercommerce.com", + "documentationUrl": "https://github.com/FosterCommerce/entry-type-rules/blob/main/README.md", + "changelogUrl": "https://raw.githubusercontent.com/FosterCommerce/entry-type-rules/main/CHANGELOG.md" + }, + "require-dev": { "roave/security-advisories": "dev-latest", "craftcms/generator": "^2.1", "fostercommerce/rector": "dev-main", @@ -59,10 +55,10 @@ "rector": "rector process --config rector.php", "rector-dry-run": "rector process --dry-run --config rector.php" }, - "config": { - "allow-plugins": { - "yiisoft/yii2-composer": true, - "craftcms/plugin-installer": true - } - } + "config": { + "allow-plugins": { + "yiisoft/yii2-composer": true, + "craftcms/plugin-installer": true + } + } } diff --git a/src/EntryTypeRules.php b/src/Plugin.php similarity index 53% rename from src/EntryTypeRules.php rename to src/Plugin.php index 96995be..b0975c1 100755 --- a/src/EntryTypeRules.php +++ b/src/Plugin.php @@ -6,22 +6,23 @@ use craft\base\Element; use craft\base\Model; -use craft\base\Plugin; +use craft\base\Plugin as BasePlugin; use craft\elements\Entry; use craft\events\DefineHtmlEvent; +use craft\web\Controller; use craft\web\View; use fostercommerce\entrytyperules\assetbundles\entrytyperules\EntryTypeRulesAsset; use fostercommerce\entrytyperules\models\Settings; -use fostercommerce\entrytyperules\services\EntryTypeRulesService as EntryTypeRulesServiceService; +use fostercommerce\entrytyperules\services\Service; use yii\base\Event; use yii\base\InvalidConfigException; /** - * @property EntryTypeRulesServiceService $entryTypeRulesService + * @property Service $service * @property Settings $settings * @method Settings getSettings() */ -class EntryTypeRules extends Plugin +class Plugin extends BasePlugin { public bool $hasCpSettings = true; @@ -32,8 +33,10 @@ public function init(): void { parent::init(); - Craft::setAlias('@plugin', $this->getBasePath()); - // + $this->setComponents([ + 'service' => Service::class, + ]); + Event::on( Element::class, Element::EVENT_DEFINE_SIDEBAR_HTML, @@ -57,48 +60,13 @@ function (DefineHtmlEvent $event): void { } } ); - - /** - * Logging in Craft involves using one of the following methods: - * - * Craft::trace(): record a message to trace how a piece of code runs. This is mainly for development use. - * Craft::info(): record a message that conveys some useful information. - * Craft::warning(): record a warning message that indicates something unexpected has happened. - * Craft::error(): record a fatal error that should be investigated as soon as possible. - * - * Unless `devMode` is on, only Craft::warning() & Craft::error() will log to `craft/storage/logs/web.log` - * - * It's recommended that you pass in the magic constant `__METHOD__` as the second parameter, which sets - * the category to the method (prefixed with the fully qualified class name) where the constant appears. - * - * To enable the Yii debug toolbar, go to your user account in the AdminCP and check the - * [] Show the debug toolbar on the front end & [] Show the debug toolbar on the Control Panel - * - * http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html - */ - Craft::info( - Craft::t( - 'entry-type-rules', - '{name} plugin loaded', - [ - 'name' => $this->name, - ] - ), - __METHOD__ - ); } - /** - * Intercepts the plugin settings page response so we can check the config override file - * (if it exists) and so we can process the Post response in our own settings controller method - * instead of using the general Craft settings HTML method to render the settings page. - * @inheritdoc - */ public function getSettingsResponse(): mixed { $overrides = Craft::$app->getConfig()->getConfigFromFile($this->handle); - /** @var \craft\web\Controller $controller */ + /** @var Controller $controller */ $controller = Craft::$app->controller; return $controller->renderTemplate( 'entry-type-rules/settings', diff --git a/src/controllers/DefaultController.php b/src/controllers/DefaultController.php index 6db236b..a9e6dbc 100755 --- a/src/controllers/DefaultController.php +++ b/src/controllers/DefaultController.php @@ -17,7 +17,7 @@ use craft\web\Application; use craft\web\Controller; use craft\web\User; -use fostercommerce\entrytyperules\EntryTypeRules; +use fostercommerce\entrytyperules\Plugin; /** * Default Controller @@ -75,8 +75,8 @@ public function actionIndex(): mixed /** @var User $user */ $user = Craft::$app->getUser(); - $result['lockedEntryTypes'] = EntryTypeRules::getInstance() - ?->entryTypeRulesService + $result['lockedEntryTypes'] = Plugin::getInstance() + ?->service ->getLockedEntryTypes($sectionId, $user); } diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 409346b..af4f0c4 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -7,9 +7,8 @@ use craft\errors\MissingComponentException; use craft\web\Controller; use craft\web\Request; -use fostercommerce\entrytyperules\EntryTypeRules; use fostercommerce\entrytyperules\models\Settings; -use fostercommerce\shipstationconnect\Plugin; +use fostercommerce\entrytyperules\Plugin; use yii\base\InvalidConfigException; use yii\web\BadRequestHttpException; use yii\web\MethodNotAllowedHttpException; @@ -35,8 +34,8 @@ public function actionSaveSettings(): Response /** @var Request $request */ $request = Craft::$app->getRequest(); - /** @var EntryTypeRules $plugin */ - $plugin = EntryTypeRules::getInstance(); + /** @var Plugin $plugin */ + $plugin = Plugin::getInstance(); $settings = new Settings([ 'sections' => $request->getBodyParam('sections'), diff --git a/src/services/EntryTypeRulesService.php b/src/services/Service.php similarity index 93% rename from src/services/EntryTypeRulesService.php rename to src/services/Service.php index 6a813e5..e2a9002 100755 --- a/src/services/EntryTypeRulesService.php +++ b/src/services/Service.php @@ -7,10 +7,10 @@ use craft\base\Component; use craft\elements\Entry; use craft\web\User; -use fostercommerce\entrytyperules\EntryTypeRules; use fostercommerce\entrytyperules\models\Settings; +use fostercommerce\entrytyperules\Plugin; -class EntryTypeRulesService extends Component +class Service extends Component { /** * @return array @@ -23,7 +23,7 @@ public function getLockedEntryTypes(int $sectionId, User $user): array // Get the plugins settings /** @var Settings $settings */ - $settings = EntryTypeRules::getInstance()?->getSettings(); + $settings = Plugin::getInstance()?->getSettings(); // Get all the entry types for this section into an array $sectionEntryTypes = Craft::$app->getEntries()->getEntryTypesBySectionId($sectionId); diff --git a/tests/unit/ExampleUnitTest.php b/tests/unit/ExampleUnitTest.php index 1abb538..8c1f4c2 100644 --- a/tests/unit/ExampleUnitTest.php +++ b/tests/unit/ExampleUnitTest.php @@ -14,7 +14,7 @@ use Codeception\Test\Unit; use UnitTester; use Craft; -use fostercommerce\entrytyperules\EntryTypeRules; +use fostercommerce\entrytyperules\Plugin; /** * ExampleUnitTest @@ -46,8 +46,8 @@ class ExampleUnitTest extends Unit public function testPluginInstance() { $this->assertInstanceOf( - EntryTypeRules::class, - EntryTypeRules::$plugin + Plugin::class, + Plugin::$plugin ); }