Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 7901b8d

Browse files

File tree

5 files changed

+119
-152
lines changed

5 files changed

+119
-152
lines changed

src/AdapterBroker.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/AdapterLoader.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/AdapterPluginManager.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Zend Framework
4+
*
5+
* LICENSE
6+
*
7+
* This source file is subject to the new BSD license that is bundled
8+
* with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* http://framework.zend.com/license/new-bsd
11+
* If you did not receive a copy of the license and are unable to
12+
* obtain it through the world-wide-web, please send an email
13+
* to license@zend.com so we can send you a copy immediately.
14+
*
15+
* @category Zend
16+
* @package Zend_Serializer
17+
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
18+
* @license http://framework.zend.com/license/new-bsd New BSD License
19+
*/
20+
21+
namespace Zend\Serializer;
22+
23+
use Zend\ServiceManager\AbstractPluginManager;
24+
25+
/**
26+
* Plugin manager implementation for serializer adapters.
27+
*
28+
* Enforces that adapters retrieved are instances of
29+
* Adapter\AdapterInterface. Additionally, it registers a number of default
30+
* adapters available.
31+
*
32+
* @category Zend
33+
* @package Zend_Serializer
34+
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
35+
* @license http://framework.zend.com/license/new-bsd New BSD License
36+
*/
37+
class AdapterPluginManager extends AbstractPluginManager
38+
{
39+
/**
40+
* Default set of adapters
41+
*
42+
* @var array
43+
*/
44+
protected $invokableClasses = array(
45+
'amf0' => 'Zend\Serializer\Adapter\Amf0',
46+
'amf3' => 'Zend\Serializer\Adapter\Amf3',
47+
'igbinary' => 'Zend\Serializer\Adapter\IgBinary',
48+
'json' => 'Zend\Serializer\Adapter\Json',
49+
'phpcode' => 'Zend\Serializer\Adapter\PhpCode',
50+
'phpserialize' => 'Zend\Serializer\Adapter\PhpSerialize',
51+
'pythonpickle' => 'Zend\Serializer\Adapter\PythonPickle',
52+
'wddx' => 'Zend\Serializer\Adapter\Wddx',
53+
);
54+
55+
/**
56+
* Validate the plugin
57+
*
58+
* Checks that the adapter loaded is an instance
59+
* of Adapter\AdapterInterface.
60+
*
61+
* @param mixed $plugin
62+
* @return void
63+
* @throws Exception\RuntimeException if invalid
64+
*/
65+
public function validatePlugin($plugin)
66+
{
67+
if ($plugin instanceof Adapter\AdapterInterface) {
68+
// we're okay
69+
return;
70+
}
71+
72+
throw new Exception\RuntimeException(sprintf(
73+
'Plugin of type %s is invalid; must implement %s\Adapter\AdapterInterface',
74+
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
75+
__NAMESPACE__
76+
));
77+
}
78+
}
79+

src/Serializer.php

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
namespace Zend\Serializer;
2222

23-
use Zend\Loader\Broker,
24-
Zend\Serializer\Adapter\AdapterInterface as Adapter;
23+
use Zend\Serializer\Adapter\AdapterInterface as Adapter;
2524

2625
/**
2726
* @category Zend
@@ -32,11 +31,11 @@
3231
class Serializer
3332
{
3433
/**
35-
* Broker for loading adapters
34+
* Plugin manager for loading adapters
3635
*
37-
* @var null|Zend\Loader\Broker
36+
* @var null|AdapterPluginManager
3837
*/
39-
private static $_adapterBroker = null;
38+
private static $_adapters = null;
4039

4140
/**
4241
* The default adapter.
@@ -58,53 +57,53 @@ public static function factory($adapterName, $opts = array())
5857
return $adapterName; // $adapterName is already an adapter object
5958
}
6059

61-
return self::getAdapterBroker()->load($adapterName, $opts);
60+
return self::getAdapterPluginManager()->get($adapterName, $opts);
6261
}
6362

6463
/**
65-
* Get the adapter broker
64+
* Get the adapter plugin manager
6665
*
67-
* @return Broker
66+
* @return AdapterPluginManager
6867
*/
69-
public static function getAdapterBroker()
68+
public static function getAdapterPluginManager()
7069
{
71-
if (self::$_adapterBroker === null) {
72-
self::$_adapterBroker = self::_getDefaultAdapterBroker();
70+
if (self::$_adapters === null) {
71+
self::$_adapters = self::_getDefaultAdapterPluginManager();
7372
}
74-
return self::$_adapterBroker;
73+
return self::$_adapters;
7574
}
7675

7776
/**
78-
* Change the adapter broker
77+
* Change the adapter plugin manager
7978
*
80-
* @param Broker $broker
79+
* @param AdapterPluginManager $adapters
8180
* @return void
8281
*/
83-
public static function setAdapterBroker(Broker $broker)
82+
public static function setAdapterPluginManager(AdapterPluginManager $adapters)
8483
{
85-
self::$_adapterBroker = $broker;
84+
self::$_adapters = $adapters;
8685
}
8786

8887
/**
89-
* Resets the internal adapter broker
88+
* Resets the internal adapter plugin manager
9089
*
91-
* @return Broker
90+
* @return AdapterPluginManager
9291
*/
93-
public static function resetAdapterBroker()
92+
public static function resetAdapterPluginManager()
9493
{
95-
self::$_adapterBroker = self::_getDefaultAdapterBroker();
96-
return self::$_adapterBroker;
94+
self::$_adapters = self::_getDefaultAdapterPluginManager();
95+
return self::$_adapters;
9796
}
9897

9998
/**
100-
* Returns a default adapter broker
99+
* Returns a default adapter plugin manager
101100
*
102-
* @return Broker
101+
* @return AdapterPluginManager
103102
*/
104-
protected static function _getDefaultAdapterBroker()
103+
protected static function _getDefaultAdapterPluginManager()
105104
{
106-
$broker = new AdapterBroker();
107-
return $broker;
105+
$adapters = new AdapterPluginManager();
106+
return $adapters;
108107
}
109108

110109
/**

test/SerializerTest.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121

2222
namespace ZendTest\Serializer;
2323

24-
use Zend\Serializer\Serializer,
25-
Zend\Serializer\AdapterBroker,
26-
Zend\Serializer\Adapter,
27-
Zend\Loader\Broker,
28-
Zend\Loader\PluginBroker;
24+
use Zend\Serializer\Adapter;
25+
use Zend\Serializer\AdapterPluginManager;
26+
use Zend\Serializer\Serializer;
2927

3028
/**
3129
* @category Zend
@@ -43,19 +41,19 @@ public function setUp()
4341

4442
public function tearDown()
4543
{
46-
Serializer::resetAdapterBroker();
44+
Serializer::resetAdapterPluginManager();
4745
}
4846

49-
public function testGetDefaultAdapterBroker()
47+
public function testGetDefaultAdapterPluginManager()
5048
{
51-
$this->assertTrue(Serializer::getAdapterBroker() instanceof AdapterBroker);
49+
$this->assertTrue(Serializer::getAdapterPluginManager() instanceof AdapterPluginManager);
5250
}
5351

54-
public function testChangeAdapterBroker()
52+
public function testChangeAdapterPluginManager()
5553
{
56-
$newBroker = new PluginBroker();
57-
Serializer::setAdapterBroker($newBroker);
58-
$this->assertTrue(Serializer::getAdapterBroker() === $newBroker);
54+
$newPluginManager = new AdapterPluginManager();
55+
Serializer::setAdapterPluginManager($newPluginManager);
56+
$this->assertTrue(Serializer::getAdapterPluginManager() === $newPluginManager);
5957
}
6058

6159
public function testDefaultAdapter()
@@ -72,16 +70,16 @@ public function testFactoryValidCall()
7270

7371
public function testFactoryUnknownAdapter()
7472
{
75-
$this->setExpectedException('Zend\Loader\Exception\RuntimeException', 'locate class');
73+
$this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotFoundException');
7674
Serializer::factory('unknown');
7775
}
7876

7977
public function testFactoryOnADummyClassAdapter()
8078
{
81-
$this->setExpectedException('Zend\\Serializer\\Exception\\RuntimeException','must implement Zend\\Serializer\\Adapter\\AdapterInterface');
82-
$broker = new AdapterBroker();
83-
$broker->getClassLoader()->registerPlugin('dummy', 'ZendTest\Serializer\TestAsset\Dummy');
84-
Serializer::setAdapterBroker($broker);
79+
$adapters = new AdapterPluginManager();
80+
$adapters->setInvokableClass('dummy', 'ZendTest\Serializer\TestAsset\Dummy');
81+
Serializer::setAdapterPluginManager($adapters);
82+
$this->setExpectedException('Zend\\Serializer\\Exception\\RuntimeException', 'AdapterInterface');
8583
Serializer::factory('dummy');
8684
}
8785

0 commit comments

Comments
 (0)