Skip to content

Commit

Permalink
Merge pull request #325 from MasoniteFramework/master
Browse files Browse the repository at this point in the history
Next Minor (2.0.24 - 9/23)
  • Loading branch information
josephmancuso committed Sep 30, 2018
2 parents 3782fa9 + 9c9447f commit 489e129
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 23 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ language: python
python:
- '3.4'
- '3.5'
- '3.6'

matrix:
include:
- python: 3.7
dist: xenial
sudo: true
- python: 3.6
script: travis_retry coverage run -m pytest && coveralls

install:
- pip install masonite_cli
Expand All @@ -26,6 +27,3 @@ deploy:
on:
tags: true
python: 3.6

after_success:
coveralls
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Contributing

Contributing to Masonite is simple:
* (optionally) Hop on [Gitter.im](https://gitter.im/masonite-framework/Lobby) to ask any questions you need.
* Hop on [Slack Channel](http://slack.masoniteproject.com/)! to ask any questions you need.
* Read the [How To Contribute](https://masoniteframework.gitbook.io/docs/prologue/how-to-contribute) documentation to see ways to contribtue to the project.
* Read the [Contributing Guide](https://masoniteframework.gitbook.io/docs/prologue/contributing-guide) to learn how to contribute to the core source code development of the project.
* Read the [Installation](https://masoniteframework.gitbook.io/docs/prologue/introduction-and-installaton) documentation on how to get started creating a Masonite project.
Expand Down
48 changes: 42 additions & 6 deletions masonite/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ def make(self, name):
object -- Returns the object that is fetched.
"""

if self.has(name):
if name in self.providers:
obj = self.providers[name]
self.fire_hook('make', name, obj)
return obj
elif inspect.isclass(name):
obj = self._find_obj(name)
self.fire_hook('make', name, obj)
return obj

raise MissingContainerBindingNotFound(
"{0} key was not found in the container".format(name))
Expand All @@ -78,8 +82,14 @@ def has(self, name):
bool
"""

if name in self.providers:
return True
if isinstance(name, str):
return name in self.providers
else:
try:
self._find_obj(name)
return True
except MissingContainerBindingNotFound:
return False

return False

Expand Down Expand Up @@ -191,7 +201,7 @@ def _find_annotated_parameter(self, parameter):
"""

for dummy, provider_class in self.providers.items():

if parameter.annotation == provider_class or parameter.annotation == provider_class.__class__:
obj = provider_class
self.fire_hook('resolve', parameter, obj)
Expand Down Expand Up @@ -265,12 +275,12 @@ def fire_hook(self, action, key, obj):

def _bind_hook(self, hook, key, obj):
"""Internal method used to abstract away the logic for binding an listener to the container hooks.
Arguments:
hook {string} -- The hook you want to listen for (bind|make|resolve)
key {string|object} -- The key to save for the listener
obj {object} -- Should be a function or class method
Returns:
self
"""
Expand All @@ -280,3 +290,29 @@ def _bind_hook(self, hook, key, obj):
else:
self._hooks[hook].update({key: [obj]})
return self

def _find_obj(self, obj):
"""Find an object in the container
Arguments:
obj {object} -- Any object in the container
Raises:
MissingContainerBindingNotFound -- Raised when the object cannot be found.
Returns:
object -- Returns the object in the container
"""

for dummy, provider_class in self.providers.items():
if obj == provider_class or obj == provider_class.__class__:
return_obj = provider_class
self.fire_hook('resolve', obj, return_obj)
return return_obj
elif inspect.isclass(provider_class) and issubclass(provider_class, obj) or issubclass(provider_class.__class__, obj):
return_obj = provider_class
self.fire_hook('resolve', obj, return_obj)
return return_obj

raise MissingContainerBindingNotFound(
'The dependency with the {0} annotation could not be resolved by the container'.format(obj))
2 changes: 1 addition & 1 deletion masonite/info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Module for specifying the Masonite version in a central location.
"""

VERSION = '2.0.23'
VERSION = '2.0.24'
11 changes: 6 additions & 5 deletions masonite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import tldextract
from cryptography.fernet import InvalidToken

from config import application
from masonite.auth.Sign import Sign
from masonite.helpers import dot
from masonite.helpers.Extendable import Extendable
from masonite.helpers.routes import compile_route_to_regex
from masonite.helpers.time import cookie_expire_time
from masonite.helpers import dot


class Request(Extendable):
Expand Down Expand Up @@ -533,8 +534,6 @@ def redirect_to(self, route_name, params={}):
self
"""

web_routes = self.container.make('WebRoutes')

self.redirect_url = self._get_named_route(route_name, params)

return self
Expand Down Expand Up @@ -600,20 +599,22 @@ def url_from_controller(self, controller, params={}):

return self.compile_route_to_url(self._get_route_from_controller(controller).route_url, params)

def route(self, name, params={}):
def route(self, name, params={}, full=False):
"""Gets a route URI by its name.
Arguments:
name {string} -- Name of the route.
Keyword Arguments:
params {dict} -- Dictionary of parameters to pass to the route for compilation. (default: {{}})
full {bool} -- Specifies whether the full application url should be returned or not. (default: {False})
Returns:
masonite.routes.Route|None -- Returns None if the route cannot be found.
"""

web_routes = self.container.make('WebRoutes')
if full:
return application.URL + self._get_named_route(name, params)

return self._get_named_route(name, params)

Expand Down
9 changes: 4 additions & 5 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ def test_get_cache(self):
cache_driver = self.app.make('Cache')

cache_driver.store('key', 'value')
cache_driver.store_for('key_time', 'key value', 2, 'seconds')

assert cache_driver.get('key') == 'value'
assert cache_driver.get('key_time') == 'key value'

cache_driver.store_for('key_time', 'key value', 4, 'seconds')
assert cache_driver.get('key_time') == 'key value'

for cache_file in glob.glob('bootstrap/cache/key*'):
os.remove(cache_file)
cache_driver.delete('key')
cache_driver.delete('key_time')

def test_cache_expired_before_get(self):
cache_driver = self.app.make('Cache')
Expand Down
25 changes: 24 additions & 1 deletion tests/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class GetObject(MockObject):

def find(self):
return 1

class GetAnotherObject(MockObject):

def find(self):
return 2

class MakeObject:
pass

class TestContainer:

def setup_method(self):
Expand Down Expand Up @@ -105,6 +107,27 @@ def test_container_collects_correct_subclasses_of_objects(self):
assert 'GetAnotherObject' in objects
assert 'GetObject' in objects

def test_container_makes_from_class(self):
assert isinstance(self.app.make(Request), Request)

def test_container_can_bind_and_make_from_class_key(self):
self.app.bind(MakeObject, MakeObject)
assert self.app.make(MakeObject) == MakeObject

def test_container_makes_from_base_class(self):
del self.app.providers['MockObject']
assert self.app.make(MockObject) == GetObject

def test_container_has_obj(self):
assert self.app.has('Request')
assert self.app.has(Request)

def test_container_makes_from_contract(self):
self.app.providers = {}

self.app.bind('UploadDriver', UploadDiskDriver)
assert self.app.make(UploadContract) == UploadDiskDriver

def test_strict_container_raises_exception(self):
self.app = App(strict=True)

Expand Down
11 changes: 11 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ def test_request_route_returns_url(self):
assert request.route('test.url') == '/test/url'
assert request.route('test.id', {'id': 1}) == '/test/url/1'

def test_request_route_returns_full_url(self):
app = App()
app.bind('Request', self.request)
app.bind('WebRoutes', [
get('/test/url', None).name('test.url'),
get('/test/url/@id', None).name('test.id')
])
request = app.make('Request').load_app(app)

assert request.route('test.url', full=True) == 'http://localhost/test/url'

def test_redirect_compiles_url_with_multiple_slashes(self):
app = App()
app.bind('Request', self.request)
Expand Down

0 comments on commit 489e129

Please sign in to comment.