├── src ├── translations │ └── en │ │ └── connect.php ├── config.php ├── models │ └── Settings.php ├── Connect.php ├── variables │ └── ConnectVariable.php ├── db │ └── Query.php └── icon.svg ├── CHANGELOG.md ├── LICENSE.md ├── composer.json └── README.md /src/translations/en/connect.php: -------------------------------------------------------------------------------- 1 | '{name} plugin loaded' 18 | ]; 19 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Connect Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | ## 4.0.1 - 2022.10.05 6 | ### Changed 7 | * Added `allow-plugins` to `composer.json` so CI tests can run 8 | * Updated docs for more explicit install instructions ([#43](https://github.com/nystudio107/craft-connect/pull/43)) 9 | 10 | ## 4.0.0 - 2022.05.17 11 | ### Added 12 | * Initial Craft CMS 4 release 13 | 14 | ## 4.0.0-beta.1 - 2022.03.25 15 | 16 | ### Added 17 | 18 | * Initial Craft CMS 4 compatibility 19 | 20 | ## 1.0.0 - 2018-08-01 21 | ### Added 22 | - Initial release 23 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 nystudio107 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nystudio107/craft-connect", 3 | "description": "Allows you to connect to external databases and perform db queries", 4 | "type": "craft-plugin", 5 | "version": "4.0.1", 6 | "keywords": [ 7 | "craft", 8 | "cms", 9 | "craftcms", 10 | "craft-plugin", 11 | "database", 12 | "connect", 13 | "query" 14 | ], 15 | "support": { 16 | "docs": "https://nystudio107.com/docs/connect/", 17 | "issues": "https://nystudio107.com/plugins/connect/support", 18 | "source": "https://github.com/nystudio107/craft-connect" 19 | }, 20 | "license": "MIT", 21 | "authors": [ 22 | { 23 | "name": "nystudio107", 24 | "homepage": "https://nystudio107.com/" 25 | } 26 | ], 27 | "require": { 28 | "craftcms/cms": "^4.0.0" 29 | }, 30 | "config": { 31 | "allow-plugins": { 32 | "craftcms/plugin-installer": true, 33 | "yiisoft/yii2-composer": true 34 | } 35 | }, 36 | "autoload": { 37 | "psr-4": { 38 | "nystudio107\\connect\\": "src/" 39 | } 40 | }, 41 | "extra": { 42 | "class": "nystudio107\\connect\\Connect", 43 | "handle": "connect", 44 | "name": "Connect" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/config.php: -------------------------------------------------------------------------------- 1 | [ 27 | 'remote' => [ 28 | 'driver' => getenv('REMOTE_DB_DRIVER'), 29 | 'server' => getenv('REMOTE_DB_SERVER'), 30 | 'user' => getenv('REMOTE_DB_USER'), 31 | 'password' => getenv('REMOTE_DB_PASSWORD'), 32 | 'database' => getenv('REMOTE_DB_DATABASE'), 33 | 'schema' => getenv('REMOTE_DB_SCHEMA'), 34 | 'tablePrefix' => getenv('REMOTE_DB_TABLE_PREFIX'), 35 | 'port' => getenv('REMOTE_DB_PORT') 36 | ], 37 | ], 38 | ]; 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/) 2 | 3 | # DEPRECATED 4 | 5 | This Craft CMS plugin is no longer supported or maintained, but it is fully functional, and you may continue to use it as you see fit. The license also allows you to fork it and make changes as needed for legacy support reasons. 6 | 7 | Instead, use [Craft's native functionality](https://nystudio107.com/blog/connecting-to-an-external-database-in-craft-cms) for querying an external database. 8 | 9 | # Connect plugin for Craft CMS 10 | 11 | Allows you to connect to external databases and perform db queries 12 | 13 | ![Screenshot](./docs/docs/resources/img/plugin-logo.png) 14 | 15 | ## Requirements 16 | 17 | This plugin requires Craft CMS 3.0.0 or later, or Craft CMS 4.0.0 or later. 18 | 19 | ## Installation 20 | 21 | To install the plugin, follow these instructions. 22 | 23 | 1. Open your terminal and go to your Craft project: 24 | 25 | cd /path/to/project 26 | 27 | 2. Then tell Composer to load the plugin: 28 | 29 | composer require nystudio107/craft-connect 30 | 31 | This will install the latest version. If you wish to install the Craft CMS 3.x compatible version, run this Composer command: 32 | 33 | composer require nystudio107/craft-connect "^1.0.0" 34 | 35 | 3. Install the plugin via `./craft install/plugin connect` via the CLI, or in the Control Panel, go to Settings → Plugins and click the “Install” button for Connect. 36 | 37 | Or you can install the plugin via the **Plugin Store** in the Craft CMS 3 CP. 38 | 39 | ## Documentation 40 | 41 | Click here -> [Connect Documentation](https://nystudio107.com/plugins/connect/documentation) 42 | 43 | ## Connect Roadmap 44 | 45 | Some things to do, and ideas for potential features: 46 | 47 | * Release it 48 | 49 | Brought to you by [nystudio107](https://nystudio107.com/) 50 | -------------------------------------------------------------------------------- /src/models/Settings.php: -------------------------------------------------------------------------------- 1 | connections)) { 38 | $this->connections = [ 39 | 'remote' => [ 40 | 'driver' => getenv('REMOTE_DB_DRIVER'), 41 | 'server' => getenv('REMOTE_DB_SERVER'), 42 | 'user' => getenv('REMOTE_DB_USER'), 43 | 'password' => getenv('REMOTE_DB_PASSWORD'), 44 | 'database' => getenv('REMOTE_DB_DATABASE'), 45 | 'schema' => getenv('REMOTE_DB_SCHEMA'), 46 | 'tablePrefix' => getenv('REMOTE_DB_TABLE_PREFIX'), 47 | 'port' => getenv('REMOTE_DB_PORT') 48 | ], 49 | ]; 50 | } 51 | } 52 | 53 | /** 54 | * @inheritdoc 55 | */ 56 | public function rules(): array 57 | { 58 | return [ 59 | ['connections', ArrayValidator::class], 60 | ]; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Connect.php: -------------------------------------------------------------------------------- 1 | sender; 71 | $variable->set('connect', ConnectVariable::class); 72 | } 73 | ); 74 | Craft::info( 75 | Craft::t( 76 | 'connect', 77 | '{name} plugin loaded', 78 | ['name' => $this->name] 79 | ), 80 | __METHOD__ 81 | ); 82 | } 83 | 84 | // Protected Methods 85 | // ========================================================================= 86 | 87 | /** 88 | * @inheritdoc 89 | */ 90 | protected function createSettingsModel(): Settings 91 | { 92 | return new Settings(); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/variables/ConnectVariable.php: -------------------------------------------------------------------------------- 1 | getDbConfig($configName); 41 | if (!empty($config)) { 42 | // Create a connection to the db 43 | $connection = new Connection($config); 44 | try { 45 | $connection->open(); 46 | } catch (Exception $e) { 47 | Craft::error($e->getMessage(), __METHOD__); 48 | } 49 | } 50 | 51 | return $connection; 52 | } 53 | 54 | /** 55 | * Returns a new generic query. 56 | * 57 | * @param ?Connection $connection 58 | * 59 | * @return Query 60 | */ 61 | public function query(?Connection $connection = null): Query 62 | { 63 | return new Query([ 64 | 'db' => $connection, 65 | ]); 66 | } 67 | 68 | // Protected Methods 69 | // ========================================================================= 70 | 71 | /** 72 | * Return a database config with the key $configName from the Settings 73 | * 74 | * @param string $configName 75 | * 76 | * @return array 77 | */ 78 | protected function getDbConfig(string $configName): array 79 | { 80 | $config = []; 81 | /* @var Settings $settings */ 82 | $settings = Connect::$plugin->getSettings(); 83 | if ($settings !== null && !empty($settings->connections[$configName])) { 84 | $dbConnection = $settings->connections[$configName]; 85 | $config = [ 86 | 'dsn' => "{$dbConnection['driver']}:" 87 | . "host={$dbConnection['server']};" 88 | . "dbname={$dbConnection['database']};" 89 | . "port={$dbConnection['port']};", 90 | 'username' => $dbConnection['user'], 91 | 'password' => $dbConnection['password'], 92 | 'tablePrefix' => $dbConnection['tablePrefix'], 93 | ]; 94 | } 95 | 96 | return $config; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/db/Query.php: -------------------------------------------------------------------------------- 1 | db); 39 | } 40 | 41 | /** 42 | * @inheritdoc 43 | */ 44 | public function batch($batchSize = 100, $db = null) 45 | { 46 | return parent::batch($batchSize, $db ?? $this->db); 47 | } 48 | 49 | /** 50 | * @inheritdoc 51 | */ 52 | public function each($batchSize = 100, $db = null) 53 | { 54 | return parent::each($batchSize, $db ?? $this->db); 55 | } 56 | 57 | /** 58 | * @inheritdoc 59 | */ 60 | public function all($db = null) 61 | { 62 | return parent::all($db ?? $this->db); 63 | } 64 | 65 | /** 66 | * @inheritdoc 67 | */ 68 | public function one($db = null) 69 | { 70 | return parent::one($db ?? $this->db); 71 | } 72 | 73 | /** 74 | * @inheritdoc 75 | */ 76 | public function scalar($db = null) 77 | { 78 | return parent::scalar($db ?? $this->db); 79 | } 80 | 81 | /** 82 | * @inheritdoc 83 | */ 84 | public function column($db = null) 85 | { 86 | return parent::column($db ?? $this->db); 87 | } 88 | 89 | /** 90 | * @inheritdoc 91 | */ 92 | public function count($q = '*', $db = null) 93 | { 94 | return parent::count($q, $db ?? $this->db); 95 | } 96 | 97 | /** 98 | * @inheritdoc 99 | */ 100 | public function sum($q, $db = null) 101 | { 102 | return parent::sum($q, $db ?? $this->db); 103 | } 104 | 105 | /** 106 | * @inheritdoc 107 | */ 108 | public function average($q, $db = null) 109 | { 110 | return parent::average($q, $db ?? $this->db); 111 | } 112 | 113 | /** 114 | * @inheritdoc 115 | */ 116 | public function min($q, $db = null) 117 | { 118 | return parent::min($q, $db ?? $this->db); 119 | } 120 | 121 | /** 122 | * @inheritdoc 123 | */ 124 | public function max($q, $db = null) 125 | { 126 | return parent::max($q, $db ?? $this->db); 127 | } 128 | 129 | /** 130 | * @inheritdoc 131 | */ 132 | public function exists($db = null) 133 | { 134 | return parent::exists($db ?? $this->db); 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 11 | 60 | 61 | 62 | 66 | 114 | 115 | 116 | 120 | 121 | 137 | 150 | 151 | 152 | 153 | 154 | 158 | 176 | 177 | 178 | 179 | 180 | 184 | 190 | 191 | 192 | 193 | 194 | 198 | 199 | 214 | 215 | 216 | 217 | 218 | 219 | 223 | 224 | 225 | 228 | 229 | 230 | 231 | 234 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 247 | 255 | 256 | 257 | 258 | 259 | 263 | 275 | 276 | 277 | 278 | 279 | 283 | 284 | 320 | 323 | 326 | 327 | 328 | 329 | 330 | 331 | 335 | 343 | 344 | 345 | 346 | 347 | 351 | 359 | 360 | 361 | 362 | 363 | 367 | 368 | 370 | 372 | 388 | 392 | 393 | 394 | 395 | 396 | 397 | 401 | 402 | 406 | 412 | 413 | 414 | 415 | 416 | 417 | 421 | 422 | 427 | 432 | 436 | 437 | 438 | 439 | 440 | 441 | 445 | 446 | 451 | 456 | 457 | 458 | 459 | 460 | 461 | 465 | 466 | 487 | 488 | 491 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 507 | 519 | 520 | 521 | 522 | 523 | 527 | 539 | 540 | 541 | 542 | 543 | 547 | 548 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 570 | 571 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 588 | 589 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 607 | 608 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 625 | 626 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 646 | 672 | 673 | 674 | 675 | 676 | 680 | 681 | 684 | 687 | 688 | 689 | 690 | 691 | 692 | 696 | 697 | 698 | 699 | 702 | 703 | 704 | 705 | 708 | 709 | 710 | 712 | 713 | 715 | 716 | 718 | 719 | 730 | 731 | 732 | 733 | 734 | 738 | 746 | 747 | 748 | 749 | 750 | 754 | 759 | 760 | 761 | 762 | 763 | 767 | 775 | 776 | 777 | 778 | 779 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 792 | 793 | 794 | 795 | 796 | 797 | 799 | 800 | 801 | 802 | 803 | 813 | 814 | 815 | 816 | 817 | 821 | 826 | 827 | 828 | 829 | 830 | 834 | 865 | 866 | 868 | 870 | 871 | 872 | 873 | 874 | 875 | 879 | 890 | 891 | 892 | 893 | 894 | 898 | 904 | 905 | 906 | 907 | 908 | 912 | 913 | 917 | 918 | 919 | 920 | 921 | 922 | 926 | 927 | 928 | 931 | 936 | 937 | 942 | 943 | 944 | 945 | 946 | 947 | 951 | 952 | 958 | 964 | 970 | 971 | 972 | 973 | 974 | 975 | 979 | 980 | 985 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 1002 | 1003 | 1004 | 1005 | 1006 | --------------------------------------------------------------------------------