Browse Source

Use Shim plugin

Mark Scherer 11 years ago
parent
commit
91c86e217a

+ 2 - 1
composer.json

@@ -16,7 +16,8 @@
 		"php": ">=5.4",
 		"cakephp/plugin-installer": "*",
 		"cakephp/cakephp": "3.0.*-dev",
-		"yangqi/htmldom": "dev-master"
+		"yangqi/htmldom": "dev-master",
+        "dereuromark/cakephp-shim": "3.0.*-dev"
 	},
 	"autoload": {
 		"psr-4": {

+ 101 - 0
docs/Network/Email.md

@@ -0,0 +1,101 @@
+# Email
+Built on top of core Email class.
+
+An enhanced class to
+- Catches the exception and logs it away. This way most user land code can be kept simple as the try catch block is not needed. It will simply return boolean/success.
+- Allow wrapLength() / priority() adjustment.
+- Auto-set "from" as admin email (no need to do that in the code unless needs overwriting).
+- Enable easier attachment adding (and also from blob).
+- Enable embedded images in HTML mails.
+- Can work with foreign attachments/images (different domains and alike).
+- Auto mimetype detection for attachments (inline or not).
+- Basic validation supported.
+- Quick way to send system emails/reports.
+- Extensive logging and error tracing as well as debugging using.
+- Don't send emails without Configure::write('Email.live'), but log them away verbosely. For testing.
+- Security measure: Don't send emails to actual addresses in debug mode, they will be sent to Configure::read('Config.adminEmail') instead. Same for cc/bcc.
+
+
+## Configs
+First, replace the use statement in the bootstrap:
+```php
+//use Cake\Network\Email\Email;
+use Tools\Network\Email\Email;
+```
+The other two lines can stay as they are:
+```php
+Email::configTransport(Configure::consume('EmailTransport'));
+Email::config(Configure::consume('Email'));
+```
+They will read from Configure what you defined there, e.g for sending SMTP mails.
+```
+'Email' => array(
+	'default' => array(
+		'from' => 'your@email.com'
+	)
+),
+'EmailTransport' => array(
+	'default' => array(
+		'className' => 'Smtp',
+		'host' => 'smtp.hostname.com',
+		'username' => 'your@email.com',
+		'password' => 'yourpwd',
+		'tls' => true,
+		'port' => 587
+	)
+)
+```
+
+That's it.
+
+
+## Usage
+Don't forget the use statement in any class you use it.
+Then instantiate it as
+```php
+$email = new Email();
+
+// Add the rest of the information needed
+$email->to($email, $name);
+$email->subject($subject);
+
+$email->replyTo($adminEmail, $adminName); // Optional reply to header
+$email->template('sometemplate'); // Optional
+$email->layout('somelayout'); // Optional
+
+$email->viewVars(compact('message', 'other'));
+
+// Send it
+if ($email->send()) {
+	// Success
+} else {
+	// Error
+	// You can use $email->getError() and display it in debug mode or log it away
+}
+```
+`from` is already set with your admin email by default, if you configured `Config.adminEmail`.
+
+### Embedded Attachments
+This will automatically set the contentId and mimeType of the file. This is necessary for embedded attachments.
+```php
+$email->addEmbeddedAttachment('/some/file.ext', 'Some custom name');
+```
+
+### Blob Attachments - as real attachment
+This will automatically set the mimeType of the file (using the filename you provide).
+```php
+$email->addBlobAttachment($somePngFileBlobContent, 'my_filename.png');
+```
+
+### Embedded Blob Attachments
+This will also automatically set the mimeType of the file as above. But it will set the contentId, as well. This is necessary for embedded attachments.
+```php
+$email->addEmbeddedBlobAttachment($somePngFileBlobContent, 'my_filename.png');
+```
+
+
+## Advanced config and usage
+You can use `'Config.xMailer'` config to set a custom xMailer header.
+Priority and line length can also be adjusted if needed.
+
+By default it switches to "Debug" transport in debug mode. If you don't want that set Configure value `Email.ive` to true.

+ 6 - 4
docs/Shims.md

@@ -1,11 +1,13 @@
 # Migration from 2.x to 3.x: Shims
 Shims ease migration as complete parts of the code, such as validation and other model property settings
-can be reused immediatelly without refactoring them right away.
+can be reused immediately without refactoring them right away.
+
+See the [Shim plugin](https://github.com/dereuromark/cakephp-shim) for details.
 
 Note: It does not hurt to have them, if you don't use them. The overhead is minimal.
 
 ## Model
-The following can be used in 3.x via shim support:
+The following can be used in 3.x (mainly via Shim plugin support):
 
 ### Table
 - $order property
@@ -31,8 +33,8 @@ use Tools\Controller\Controller;
 
 class AppController extends Controller {
 
-	public $components = array('Tools.Session');
+	public $components = array('Shim.Session');
 
 }
 ```
-It also contains the new consume() method.
+It also contains the new `consume()` method.

+ 2 - 0
docs/TestSuite/Testing.md

@@ -54,6 +54,8 @@ if (!$this->isDebug()) {
 
 See the above trait features.
 
+For details on this see [Shim plugin](https://github.com/dereuromark/cakephp-shim).
+
 ## TestCase
 `assertNotWithinMargin()` as the opposite of `assertWithinMargin()` is available.
 

+ 1 - 1
src/Controller/Component/AuthUserComponent.php

@@ -2,7 +2,7 @@
 
 namespace Tools\Controller\Component;
 
-use Tools\Controller\Component\Component;
+use Shim\Controller\Component\Component;
 use Cake\Event\Event;
 use Tools\Auth\AuthUserTrait;
 

+ 1 - 1
src/Controller/Component/CommonComponent.php

@@ -1,7 +1,7 @@
 <?php
 namespace Tools\Controller\Component;
 
-use Tools\Controller\Component\Component;
+use Shim\Controller\Component\Component;
 use Cake\Core\Configure;
 use Cake\Event\Event;
 use Cake\Routing\Router;

+ 0 - 22
src/Controller/Component/Component.php

@@ -1,22 +0,0 @@
-<?php
-namespace Tools\Controller\Component;
-
-use Cake\Controller\Component as CakeComponent;
-use Cake\Event\Event;
-
-/**
- * Convenience class that automatically provides the component's methods with
- * the controller instance via `$this->Controller`.
- */
-class Component extends CakeComponent {
-
-	/**
-	 * @var \Cake\Controller\Controller
-	 */
-	public $Controller;
-
-	public function initialize(array $config) {
-		$this->Controller = $this->_registry->getController();
-	}
-
-}

+ 1 - 1
src/Controller/Component/FlashComponent.php

@@ -1,7 +1,7 @@
 <?php
 namespace Tools\Controller\Component;
 
-use Tools\Controller\Component\Component;
+use Shim\Controller\Component\Component;
 use Cake\Core\Configure;
 use Cake\Event\Event;
 use Cake\Utility\Inflector;

+ 0 - 154
src/Controller/Component/SessionComponent.php

@@ -1,154 +0,0 @@
-<?php
-namespace Tools\Controller\Component;
-
-use Tools\Controller\Component\Component;
-
-/**
- * The CakePHP SessionComponent provides a way to persist client data between
- * page requests. It acts as a wrapper for the `$_SESSION` as well as providing
- * convenience methods for several `$_SESSION` related functions.
- *
- * This class is here for backwards compatibility with CakePHP 2.x
- */
-class SessionComponent extends Component {
-
-	/**
-	 * The Session object instance
-	 *
-	 * @var \Cake\Network\Session
-	 */
-	protected $_session;
-
-	/**
-	 * Initialize properties.
-	 *
-	 * @param array $config The config data.
-	 * @return void
-	 */
-	public function initialize(array $config) {
-		$this->_session = $this->_registry->getController()->request->session();
-	}
-
-	/**
-	 * Used to write a value to a session key.
-	 *
-	 * In your controller: $this->Session->write('Controller.sessKey', 'session value');
-	 *
-	 * @param string $name The name of the key your are setting in the session.
-	 *    This should be in a Controller.key format for better organizing
-	 * @param string $value The value you want to store in a session.
-	 * @return void
-	 */
-	public function write($name, $value = null) {
-		$this->_session->write($name, $value);
-	}
-
-	/**
-	 * Used to read a session values for a key or return values for all keys.
-	 *
-	 * In your controller: $this->Session->read('Controller.sessKey');
-	 * Calling the method without a param will return all session vars
-	 *
-	 * @param string $name the name of the session key you want to read
-	 * @return mixed value from the session vars
-	 */
-	public function read($name = null) {
-		return $this->_session->read($name);
-	}
-
-	/**
-	 * Used to read and delete a session values for a key.
-	 *
-	 * In your controller: $this->Session->consume('Controller.sessKey');
-	 *
-	 * @param string $name the name of the session key you want to read
-	 * @return mixed value from the session vars
-	 */
-	public function consume($name) {
-		return $this->_session->consume($name);
-	}
-
-	/**
-	 * Wrapper for SessionComponent::del();
-	 *
-	 * In your controller: $this->Session->delete('Controller.sessKey');
-	 *
-	 * @param string $name the name of the session key you want to delete
-	 * @return void
-	 */
-	public function delete($name) {
-		$this->_session->delete($name);
-	}
-
-	/**
-	 * Used to check if a session variable is set
-	 *
-	 * In your controller: $this->Session->check('Controller.sessKey');
-	 *
-	 * @param string $name the name of the session key you want to check
-	 * @return bool true is session variable is set, false if not
-	 */
-	public function check($name) {
-		return $this->_session->check($name);
-	}
-
-	/**
-	 * Used to renew a session id
-	 *
-	 * In your controller: $this->Session->renew();
-	 *
-	 * @return void
-	 */
-	public function renew() {
-		$this->_session->renew();
-	}
-
-	/**
-	 * Used to destroy sessions
-	 *
-	 * In your controller: $this->Session->destroy();
-	 *
-	 * @return void
-	 */
-	public function destroy() {
-		$this->_session->destroy();
-	}
-
-	/**
-	 * Get/Set the session id.
-	 *
-	 * When fetching the session id, the session will be started
-	 * if it has not already been started. When setting the session id,
-	 * the session will not be started.
-	 *
-	 * @param string $id Id to use (optional)
-	 * @return string The current session id.
-	 */
-	public function id($id = null) {
-		if ($id === null) {
-			$session = $this->_session;
-			$session->start();
-			return $session->id();
-		}
-		$this->_session->id($id);
-	}
-
-	/**
-	 * Returns a bool, whether or not the session has been started.
-	 *
-	 * @return bool
-	 */
-	public function started() {
-		return $this->_session->started();
-	}
-
-	/**
-	 * Events supported by this component.
-	 *
-	 * @return array
-	 */
-	public function implementedEvents() {
-		return [];
-	}
-
-}

+ 1 - 1
tests/TestApp/Controller/Component/AppleComponent.php

@@ -1,7 +1,7 @@
 <?php
 namespace TestApp\Controller\Component;
 
-use Tools\Controller\Component\Component;
+use Shim\Controller\Component\Component;
 use Cake\Controller\Controller;
 use Cake\Event\Event;
 

+ 1 - 1
tests/TestApp/Controller/Component/BananaComponent.php

@@ -1,7 +1,7 @@
 <?php
 namespace TestApp\Controller\Component;
 
-use Tools\Controller\Component\Component;
+use Shim\Controller\Component\Component;
 use Cake\Controller\Controller;
 use Cake\Event\Event;
 

+ 1 - 1
tests/TestApp/Controller/Component/TestComponent.php

@@ -1,7 +1,7 @@
 <?php
 namespace TestApp\Controller\Component;
 
-use Tools\Controller\Component\Component;
+use Shim\Controller\Component\Component;
 use Cake\Event\Event;
 
 class TestComponent extends Component {

+ 3 - 3
tests/TestCase/Controller/Component/CommonComponentTest.php

@@ -2,7 +2,7 @@
 namespace Tools\Test\TestCase\Controller\Component;
 
 use Cake\Controller\ComponentRegistry;
-use Tools\Controller\Component\Component;
+use Shim\Controller\Component\Component;
 use Cake\Controller\Component\CommonComponent;
 use Cake\Controller\Controller;
 use Cake\Core\Configure;
@@ -46,9 +46,9 @@ class CommonComponentTest extends TestCase {
 		// with plugin
 		$this->Controller->Session = null;
 		$this->assertTrue(!isset($this->Controller->Session));
-		$this->Controller->Common->loadComponent('Tools.Session', ['foo' => 'bar']);
+		$this->Controller->Common->loadComponent('Shim.Session', ['foo' => 'bar']);
 		$this->Controller->components()->unload('Session');
-		$this->Controller->Common->loadComponent('Tools.Session',['foo' => 'baz']);
+		$this->Controller->Common->loadComponent('Shim.Session',['foo' => 'baz']);
 		$this->assertTrue(isset($this->Controller->Session));
 
 		// with options

+ 0 - 37
tests/TestCase/Controller/Component/ComponentTest.php

@@ -1,37 +0,0 @@
-<?php
-namespace Tools\Test\TestCase\Controller\Component;
-
-use Cake\Controller\ComponentRegistry;
-use Cake\Controller\Controller;
-use Tools\Controller\Component\Component;
-use Cake\Network\Request;
-use Tools\TestSuite\TestCase;
-use Cake\Event\Event;
-/**
- * SessionComponentTest class
- *
- */
-class ComponentTest extends TestCase {
-
-/**
- * setUp method
- *
- * @return void
- */
-	public function setUp() {
-		$this->Controller = new Controller(new Request());
-		$this->ComponentRegistry = new ComponentRegistry($this->Controller);
-	}
-
-/**
- * testBeforeFilter method
- *
- * @return void
- */
-	public function testBeforeFilter() {
-		$Component = new Component($this->ComponentRegistry);
-
-		$this->assertInstanceOf('Cake\Controller\Controller', $Component->Controller);
-	}
-
-}

+ 1 - 1
tests/TestCase/Controller/Component/FlashComponentTest.php

@@ -2,7 +2,7 @@
 namespace Tools\Test\TestCase\Controller\Component;
 
 use Cake\Controller\ComponentRegistry;
-use Tools\Controller\Component\Component;
+use Shim\Controller\Component\Component;
 use Cake\Controller\Controller;
 use Cake\Core\Configure;
 use Cake\Network\Request;

+ 0 - 181
tests/TestCase/Controller/Component/SessionComponentTest.php

@@ -1,181 +0,0 @@
-<?php
-namespace Tools\Test\TestCase\Controller\Component;
-
-use Cake\Controller\ComponentRegistry;
-use Tools\Controller\Component\SessionComponent;
-use Cake\Controller\Controller;
-use Cake\Core\Configure;
-use Cake\Network\Request;
-use Cake\Network\Session;
-use Cake\Routing\DispatcherFactory;
-use Tools\TestSuite\TestCase;
-
-/**
- * SessionComponentTest class
- *
- */
-class SessionComponentTest extends TestCase {
-
-	protected static $_sessionBackup;
-
-/**
- * fixtures
- *
- * @var string
- */
-	public $fixtures = ['core.sessions'];
-
-/**
- * test case startup
- *
- * @return void
- */
-	public static function setupBeforeClass() {
-		DispatcherFactory::add('Routing');
-		DispatcherFactory::add('ControllerFactory');
-	}
-
-/**
- * cleanup after test case.
- *
- * @return void
- */
-	public static function teardownAfterClass() {
-		DispatcherFactory::clear();
-	}
-
-/**
- * setUp method
- *
- * @return void
- */
-	public function setUp() {
-		parent::setUp();
-		$_SESSION = [];
-		Configure::write('App.namespace', 'TestApp');
-		$controller = new Controller(new Request(['session' => new Session()]));
-		$this->ComponentRegistry = new ComponentRegistry($controller);
-	}
-
-/**
- * tearDown method
- *
- * @return void
- */
-	public function tearDown() {
-		parent::tearDown();
-	}
-
-/**
- * testSessionReadWrite method
- *
- * @return void
- */
-	public function testSessionReadWrite() {
-		$Session = new SessionComponent($this->ComponentRegistry);
-
-		$this->assertNull($Session->read('Test'));
-
-		$Session->write('Test', 'some value');
-		$this->assertEquals('some value', $Session->read('Test'));
-		$Session->delete('Test');
-
-		$Session->write('Test.key.path', 'some value');
-		$this->assertEquals('some value', $Session->read('Test.key.path'));
-		$this->assertEquals(['path' => 'some value'], $Session->read('Test.key'));
-		$Session->write('Test.key.path2', 'another value');
-		$this->assertEquals(['path' => 'some value', 'path2' => 'another value'], $Session->read('Test.key'));
-		$Session->delete('Test');
-
-		$array = ['key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3'];
-		$Session->write('Test', $array);
-		$this->assertEquals($Session->read('Test'), $array);
-		$Session->delete('Test');
-
-		$Session->write(['Test'], 'some value');
-		$Session->write(['Test' => 'some value']);
-		$this->assertEquals('some value', $Session->read('Test'));
-		$Session->delete('Test');
-	}
-
-/**
- * Test consuming session data.
- *
- * @return void
- */
-	public function testConsume() {
-		$Session = new SessionComponent($this->ComponentRegistry);
-
-		$Session->write('Some.string', 'value');
-		$Session->write('Some.array', ['key1' => 'value1', 'key2' => 'value2']);
-		$this->assertEquals('value', $Session->read('Some.string'));
-
-		$value = $Session->consume('Some.string');
-		$this->assertEquals('value', $value);
-		$this->assertFalse($Session->check('Some.string'));
-
-		$value = $Session->consume('');
-		$this->assertNull($value);
-
-		$value = $Session->consume(null);
-		$this->assertNull($value);
-
-		$value = $Session->consume('Some.array');
-		$expected = ['key1' => 'value1', 'key2' => 'value2'];
-		$this->assertEquals($expected, $value);
-		$this->assertFalse($Session->check('Some.array'));
-	}
-
-/**
- * testSessionDelete method
- *
- * @return void
- */
-	public function testSessionDelete() {
-		$Session = new SessionComponent($this->ComponentRegistry);
-
-		$Session->write('Test', 'some value');
-		$Session->delete('Test');
-		$this->assertNull($Session->read('Test'));
-	}
-
-/**
- * testSessionCheck method
- *
- * @return void
- */
-	public function testSessionCheck() {
-		$Session = new SessionComponent($this->ComponentRegistry);
-
-		$this->assertFalse($Session->check('Test'));
-
-		$Session->write('Test', 'some value');
-		$this->assertTrue($Session->check('Test'));
-		$Session->delete('Test');
-	}
-
-/**
- * testSessionId method
- *
- * @return void
- */
-	public function testSessionId() {
-		$Session = new SessionComponent($this->ComponentRegistry);
-		$this->assertEquals(session_id(), $Session->id());
-	}
-
-/**
- * testSessionDestroy method
- *
- * @return void
- */
-	public function testSessionDestroy() {
-		$Session = new SessionComponent($this->ComponentRegistry);
-
-		$Session->write('Test', 'some value');
-		$this->assertEquals('some value', $Session->read('Test'));
-		$Session->destroy('Test');
-		$this->assertNull($Session->read('Test'));
-	}
-
-}

+ 1 - 1
tests/TestCase/Controller/ControllerTest.php

@@ -2,7 +2,7 @@
 namespace Tools\Test\TestCase\Controller;
 
 use Cake\Controller\ComponentRegistry;
-use Tools\Controller\Component\Component;
+use Shim\Controller\Component\Component;
 use Cake\Controller\Component\CommonComponent;
 use Cake\Core\Configure;
 use Cake\Network\Request;